File: MSchemaDataSource.m

package info (click to toggle)
mysql-gui-tools 5.0r12-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 105,540 kB
  • ctags: 50,897
  • sloc: sql: 348,439; pascal: 285,780; cpp: 94,578; ansic: 90,768; objc: 33,761; sh: 25,629; xml: 10,924; yacc: 10,755; java: 9,986; php: 2,806; python: 2,068; makefile: 1,945; perl: 3
file content (1006 lines) | stat: -rw-r--r-- 24,473 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
//
//  MSchemaDataSource.m
//  MySQLGUICommon
//
//  Created by Alfredo Kojima on Sun Jun 27 2004.
//  Copyright (c) 2004 MySQL AB. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "MSchemaDataSource.h"
#import "mxUtils.h"

NSString *MSchemaItemPboardType= @"MSchemaItemPboardType";


@implementation MSchemaItem

- (id)initWithCatalog: (MYX_CATALOG*)catalog
                 icon: (NSImage*)aicon
{
  if (!(self= [super init]))
    return nil;
  
  type= MCatalogItemType;
  _object.catalog= catalog;
  _repr= nil;
  icon= [aicon retain];
  
  return self;
}

- (id)initWithSchema: (MYX_SCHEMA*)schema
       parentCatalog: (MSchemaItem*)parentItem
                icon: (NSImage*)aicon
{
  if (!(self= [super init]))
    return nil;
  
  type= MSchemaItemType;
  _object.schema= schema;
  parent= parentItem;
  _repr= nil;
  icon= [aicon retain];
  
  return self;
}


- (id)initWithSP: (MYX_SCHEMA_STORED_PROCEDURE*)sp
    parentSchema: (MSchemaItem*)parentItem
            icon: (NSImage*)aicon
{
  if (!(self= [super init]))
    return nil;
  
  type= MSPItemType;
  _object.sp= sp;
  parent= parentItem;
  _repr= nil;
  icon= [aicon retain];
  
  return self;
}


- (id)initWithTable: (MYX_SCHEMA_TABLE*)table
       parentSchema: (MSchemaItem*)parentItem
               icon: (NSImage*)aicon
{
  if (!(self= [super init]))
    return nil;
  
  if (table->table_type == MSTT_VIEW)
    type= MViewItemType;
  else
    type= MTableItemType;
  _object.table= table;
  parent= parentItem;
  _repr= nil;
  icon= [aicon retain];
  
  return self;
}

- (id)initWithColumn: (MYX_SCHEMA_TABLE_COLUMN*)column
         parentTable: (MSchemaItem*)parentItem
                icon: (NSImage*)aicon
{
  if (!(self= [super init]))
    return nil;
  
  type= MColumnItemType;
  _object.column= column;
  _repr= nil;
  parent= parentItem;
  icon= [aicon retain];
  
  return self;
}

- (void)dealloc
{
  [_identifier release];
  [_repr release];
  [icon release];
  [super dealloc];
}

- (NSImage*)icon
{
  return icon;
}

- (MSchemaDSItemType)type
{
  return type;
}

- (id)identifier
{
  if (!_identifier)
    _identifier= [[NSString stringWithFormat:@"%p",self] retain];
  return _identifier;
}


- (BOOL)isExpandableForType:(MSchemaDSItemType)aType
{ 
  if (aType == MObjectItemType && (type == MTableItemType || 
                                   type == MSchemaItemType ||
                                   type == MCatalogItemType))
    return YES;
  
  if (aType == MSchemaItemType && (type == MCatalogItemType))
    return YES;

  if (aType == MTableItemType && (type == MSchemaItemType ||
                                  type == MCatalogItemType))
    return YES;

  if (aType == MViewItemType && (type == MSchemaItemType ||
                                  type == MCatalogItemType))
    return YES;

  if (aType == MSPItemType && (type == MSchemaItemType ||
                               type == MCatalogItemType))
    return YES;
  
  if (aType == MColumnItemType && (type == MTableItemType || 
                                   type == MViewItemType ||
                                   type == MSchemaItemType ||
                                   type == MCatalogItemType))
    return YES;
  
  return NO;
}

- (NSString*)repr
{
  if (!_repr)
  {
    switch (type)
    {
      case MCatalogItemType:
        _repr= [NSString stringWithUTF8String: _object.catalog->catalog_name?:"?"];
        break;
      case MSchemaItemType:
        _repr= [NSString stringWithUTF8String: _object.schema->schema_name?:""];
        break;
      case MTableItemType:
        _repr= [NSString stringWithUTF8String: _object.table->table_name?:""];
        break;
      case MColumnItemType:
        _repr= [NSString stringWithUTF8String: _object.column->column_name?:""];
        break;
      case MViewItemType:
        _repr= [NSString stringWithUTF8String: _object.table->table_name?:""];
        break;
      case MSPItemType:
        _repr= [NSString stringWithUTF8String: _object.sp->name?:""];
        break;
      case MObjectItemType:
        NSLog(@"Invalid object type in %@", self);
        break;
    }
    [_repr retain];
  }
  return _repr;
}

- (MYX_CATALOG*)catalog
{
  MSchemaItem *item= nil;
  switch (type)
  {
    case MCatalogItemType:
      item= self;
      break;
    case MSchemaItemType:
      item= self->parent;
      break;
    case MSPItemType:
    case MViewItemType:
    case MTableItemType:
      item= self->parent->parent;
      break;
    case MColumnItemType:
      item= self->parent->parent->parent;
      break;
    case MObjectItemType:
      break;
  }
  return item ? item->_object.catalog : NULL;
}

- (MYX_SCHEMA*)schema
{
  MSchemaItem *item= nil;
  switch (type)
  {
    case MCatalogItemType:
      return NULL;
      break;
    case MSchemaItemType:
      item= self;
      break;
    case MSPItemType:  
    case MViewItemType:
    case MTableItemType:
      item= self->parent;
      break;
    case MColumnItemType:
      item= self->parent->parent;
      break;
    default:
      NSAssert(0,@"invalid type (table)");
      break;      
  }
  return item ? item->_object.schema : NULL;
}

- (MYX_SCHEMA_TABLE*)table
{
  MSchemaItem *item= nil;
  switch (type)
  {
    case MViewItemType:
    case MTableItemType:
      item= self;
      break;
    case MColumnItemType:
      item= self->parent;
      break;
    default:
      NSAssert(0,@"invalid type (table)");
      break;
  }
  return item ? item->_object.table : NULL;  
}

- (MYX_SCHEMA_TABLE_COLUMN*)column
{
  MSchemaItem *item= nil;
  switch (type)
  {
    case MColumnItemType:
      item= self;
      break;
    default:
      NSAssert(0,@"invalid type (column)");
      break;
  }
  return item ? item->_object.column : NULL;  
}


- (MYX_SCHEMA_STORED_PROCEDURE*)sp
{
  MSchemaItem *item= nil;
  switch (type)
  {
    case MSPItemType:
      item= self;
      break;
    default:
      NSAssert(0,@"invalid type (sp)");
      break;
  }
  return item ? item->_object.sp : NULL;  
}


- (void)resetCatalog:(MYX_CATALOG*)catalog
{
  NSAssert(type == MCatalogItemType, @"Invalid type");
  
  _object.catalog= catalog;
}


- (void)resetSchema:(MYX_SCHEMA*)schema
{
  NSAssert(type == MSchemaItemType, @"Invalid type");
  
  _object.schema= schema;
}


- (void)resetTable:(MYX_SCHEMA_TABLE*)table
{
  NSAssert(type == MTableItemType || type == MViewItemType, @"Invalid type");
  
  _object.table= table;
}


- (void)resetColumn:(MYX_SCHEMA_TABLE_COLUMN*)column
{
  NSAssert(type == MColumnItemType, @"Invalid type");
  
  _object.column= column;
}


- (void)resetSP:(MYX_SCHEMA_STORED_PROCEDURE*)sp
{
  NSAssert(type == MSPItemType, @"Invalid type");
  
  _object.sp= sp;
}

@end


//==============================================================================

@implementation MSchemaDataSource

- (MSchemaItem*)findItem: (NSString*)name
{
  NSArray *rootList= [_children objectForKey:@"root"];
  int i, c= [rootList count];
  for (i= 0; i < c; i++)
  {
    id item= [_items objectForKey:[rootList objectAtIndex:i]];
    if ([[item repr] isEqualToString:name])
      return item;
  }
  
  return nil;
}

- (MSchemaItem*)findChild: (NSString*)name
                   ofItem: (MSchemaItem*)item
{
  NSArray *array= [_children objectForKey:[item identifier]];
  int i, c= [array count];
  for (i= 0; i < c; i++)
  {
    id item= [_items objectForKey:[array objectAtIndex:i]];
    if ([[item repr] isEqualToString:name])
      return item;
  }
  return nil;
}


- (id)initWithRoot: (MSchemaDSItemType)root
              leaf: (MSchemaDSItemType)leaf
{
  self= [super init];
  if (self)
  {
    _rootType= root;
    _leafType= leaf;
    _children= [[NSMutableDictionary alloc] init];
    _items= [[NSMutableDictionary alloc] init];  
    
    _catalogIcon= [MXGetImageFromBundle([NSBundle bundleForClass:[self class]], @"16x16_Catalog.png") retain];
    _schemaIcon= [MXGetImageFromBundle([NSBundle bundleForClass:[self class]], @"myx_schema_16x16.png") retain];
    _tableIcon= [MXGetImageFromBundle([NSBundle bundleForClass:[self class]], @"myx_schema_table_16x16.png") retain];
    _viewIcon= [MXGetImageFromBundle([NSBundle bundleForClass:[self class]], @"myx_schema_view_16x16.png") retain];
    _spIcon= [MXGetImageFromBundle([NSBundle bundleForClass:[self class]], @"myx_schema_sp_16x16.png") retain];
    _columnIcon= [MXGetImageFromBundle([NSBundle bundleForClass:[self class]], @"16x16_Field.png") retain];
    _keyIcon= [MXGetImageFromBundle([NSBundle bundleForClass:[self class]], @"16x16_KeyColumn.png") retain];	
  }    
  return self;
}

- (id)initWithRoot: (MSchemaDSItemType)root
              leaf: (MSchemaDSItemType)leaf
             icons: (NSDictionary*)icons
{
  self= [super init];
  if (self)
  {
    _rootType= root;
    _leafType= leaf;
    _children= [[NSMutableDictionary alloc] init];
    _items= [[NSMutableDictionary alloc] init];  
    
    _catalogIcon= [[icons objectForKey:@"catalog"] retain];
    _schemaIcon= [[icons objectForKey:@"schema"] retain];
    _tableIcon= [[icons objectForKey:@"table"] retain];
    _viewIcon= [[icons objectForKey:@"view"] retain];
    _columnIcon= [[icons objectForKey:@"column"] retain];
    _spIcon= [[icons objectForKey:@"sp"] retain];
    _keyIcon= [[icons objectForKey:@"key"] retain];
  }  
  return self;
}


- (void)dealloc
{
  [_children release];
  [_items release];
  
  [_catalogIcon release];
  [_schemaIcon release];
  [_tableIcon release];
  [_viewIcon release];
  [_columnIcon release];
  [_keyIcon release];
  [_spIcon release];
  
  [super dealloc];
}


- (void)setTableFetcher:(id)target selector:(SEL)sel
{
  _tableFetcher= target;
  _tableFetcherAction= sel;
}


- (void)resetTree
{
  [_children removeAllObjects];
  [_items removeAllObjects];
}


- (void)addColumn:(MYX_SCHEMA_TABLE_COLUMN*)column
            table:(MSchemaItem*)table
          toArray:(NSMutableArray*)tabArray
{
  MSchemaItem *citem= [[MSchemaItem alloc] initWithColumn: column
                                              parentTable: table
                                                     icon: column->primary_key ? _keyIcon : _columnIcon];
  
  [_items setObject:citem forKey:[citem identifier]];
  
  [tabArray addObject: [citem identifier]];
  [citem release];  
}


- (void)addSP:(MYX_SCHEMA_STORED_PROCEDURE*)sp
       schema:(MSchemaItem*)schema
      toArray:(NSMutableArray*)spArray
{
  MSchemaItem *citem= [[MSchemaItem alloc] initWithSP: sp
                                         parentSchema: schema
                                                 icon: _spIcon];
  
  [_items setObject:citem forKey:[citem identifier]];
  
  [spArray addObject: [citem identifier]];
  [citem release];  
}


- (void)addTable:(MYX_SCHEMA_TABLE*)table
          schema:(MSchemaItem*)schema
         toArray:(NSMutableArray*)schArray
{
  NSMutableArray *tabArray= [NSMutableArray arrayWithCapacity: table->columns_num];
  MSchemaItem *titem= [[MSchemaItem alloc] initWithTable: table
                                            parentSchema: schema
                                                    icon: table->table_type==MSTT_BASE_TABLE?_tableIcon:_viewIcon];
  unsigned int j;

  for (j= 0; j < table->columns_num; j++)
  {
    [self addColumn:table->columns+j
              table:titem
            toArray:tabArray];
  }
  
  [_items setObject:titem forKey:[titem identifier]];
  [_children setObject:tabArray forKey:[titem identifier]];
  
  [schArray addObject: [titem identifier]];
  [titem release];
}


- (void)addSchema:(MYX_SCHEMA*)schema
          catalog:(MSchemaItem*)catalog
          toArray:(NSMutableArray*)schArray
{ 
  MSchemaItem *item= [[MSchemaItem alloc] initWithSchema: schema
                                           parentCatalog: catalog
                                                    icon: _schemaIcon];
  [_items setObject:item forKey:[item identifier]];
  
  [schArray addObject: [item identifier]];
  [item release];
}


- (void)addCatalog:(MYX_CATALOG*)catalog
           toArray:(NSMutableArray*)catArray
{
  NSMutableArray *schArray= [NSMutableArray arrayWithCapacity: catalog->schemata_num];
  MSchemaItem *citem= [[MSchemaItem alloc] initWithCatalog: catalog
                                                      icon: _catalogIcon];
  unsigned int j;
  
  for (j= 0; j < catalog->schemata_num; j++)
  {
    [self addSchema:catalog->schemata+j 
            catalog:citem
            toArray:schArray];
  }
  
  [_items setObject:citem forKey:[citem identifier]];
  [_children setObject:schArray forKey:[citem identifier]];
  
  [catArray addObject: [citem identifier]];
  [citem release];
}


- (MSchemaItem*)search:(NSMutableArray*)array
       forItemWithName:(const char*)name
{
  unsigned int i, c= [array count];
  
  for (i= 0; i < c; i++)
  {
    id ident= [array objectAtIndex:i];
    MSchemaItem *item= [_items objectForKey:ident];
    if (strcmp([[item repr] UTF8String], name)==0)
      return item;
  }
  return nil;
}


- (void)removeSubtree:(id)identifier
{
  NSMutableArray *children= [_children objectForKey:identifier];
  
  if (children)
  {
    unsigned int i, c= [children count];
    for (i= 0; i < c; i++)
    {
      id childid= [children objectAtIndex:i];
      [self removeSubtree:childid];
    }
  }
  [_children removeObjectForKey:identifier];
  [_items removeObjectForKey:identifier];
}


- (MYX_CATALOGS*)catalogs
{
  return _catalogs;
}

- (void)updateWithCatalogs:(MYX_CATALOGS*)catalogs
{
  int i;
  NSMutableArray *catArray;
  
  _catalogs= catalogs;
  
  catArray= [_children objectForKey:@"root"];
  if (catArray)
  {
    unsigned int sc, c= [catArray count];
    int s;
    // traverse catalogs and delete any missing items in the old list
    for (i= c-1; i>=0; i--)
    {
      unsigned int ci;
      MSchemaItem *item= [_items objectForKey: [catArray objectAtIndex:i]];
      const char *name= [[item repr] UTF8String];
      NSMutableArray *schArray= [_children objectForKey: [item identifier]];
      
      // first try catalog itself
      for (ci= 0; ci < catalogs->catalogs_num; ci++)
      {
        if (strcmp(catalogs->catalogs[ci].catalog_name, name)==0)
          break;
      }
      if (ci == catalogs->catalogs_num) // old item not found in new list
      {
        // delete everything related
        [self removeSubtree:[item identifier]];
        [catArray removeObjectAtIndex: i];
      }
      
      // then try schemas inside each catalog
      sc= [schArray count];
      for (s= sc-1; s>=0; s--)
      {
        MSchemaItem *sitem= [_items objectForKey: [schArray objectAtIndex:s]];
        unsigned int si;
        const char *sname= [[sitem repr] UTF8String];
        
        for (si= 0; si < catalogs->catalogs[ci].schemata_num; si++)
        {
          if (strcmp(catalogs->catalogs[ci].schemata[si].schema_name, sname)==0)
            break;
        }
        if (si == catalogs->catalogs[ci].schemata_num) // old item not found in new list
        {
          [self removeSubtree:[sitem identifier]];
          [schArray removeObjectAtIndex: s];
        }
      }
    }
    
    // traverse all catalogs and add anything new	
    for (i= 0; i < catalogs->catalogs_num; i++)
    {
      MSchemaItem *citem;
      
      if (!(citem= [self search:catArray
                forItemWithName:catalogs->catalogs[i].catalog_name]))
      {
        // add the catalog here
        [self addCatalog:catalogs->catalogs+i toArray:catArray];
      }
      else
      {
        NSMutableArray *schArray= [_children objectForKey:[citem identifier]];
        
        // traverse all schemas in the catalog
        for (s= 0; s < catalogs->catalogs[i].schemata_num; s++)
        {
          MSchemaItem *sitem;
          
          if (!(sitem= [self search:schArray
                    forItemWithName:catalogs->catalogs[i].schemata[s].schema_name]))
          {
            // add the schema here
            [self addSchema:catalogs->catalogs[i].schemata+s
                    catalog:citem
                    toArray:schArray];
          }
          else
          {
            MYX_SCHEMA *schema= [sitem schema];
            catalogs->catalogs[i].schemata[s].schema_tables= schema->schema_tables;
            schema->schema_tables= NULL;
            [sitem resetSchema:catalogs->catalogs[i].schemata+s];
          }
        }
      }
    }
  }
  else
  {
    catArray= [NSMutableArray arrayWithCapacity:catalogs->catalogs_num];
    
    for (i= 0; i < catalogs->catalogs_num; i++)
    {
      [self addCatalog:catalogs->catalogs+i 
               toArray:catArray];
    }
    [_children setObject:catArray forKey:@"root"];
  }
}


- (void)resetSchemaChildren:(MSchemaItem*)schema
{
  NSMutableArray *array= [_children objectForKey:[schema identifier]];
  
  if (array)
    [array removeAllObjects];
  else
  {
    array= [NSMutableArray arrayWithCapacity:0];
    [_children setObject:array forKey:[schema identifier]];
  }  
}

- (void)updateSchema:(MSchemaItem*)schema
          withTables:(MYX_SCHEMA_TABLES*)tables
{
  NSMutableArray *tabArray;
  unsigned int i;
  
  tabArray= [_children objectForKey:[schema identifier]];
  NSAssert(tabArray != nil, @"Array for schema does not exist. Did you call resetSchemaChildren:?");
  
  //XXX optimize so that it doesn't recreate the whole thing
  // whenever the list is updated (as it's done on schema/catalog level)
  
  for (i= 0; i < tables->schema_tables_num; i++)
  {
    [self addTable:tables->schema_tables+i
            schema:schema
           toArray:tabArray];
  }
}


- (void)updateSchema:(MSchemaItem*)schema
             withSPs:(MYX_SCHEMA_STORED_PROCEDURES*)sps
{
  NSMutableArray *spArray;
  unsigned int i;
  
  spArray= [_children objectForKey:[schema identifier]];
  NSAssert(spArray != nil, @"Array for schema does not exist. Did you call resetSchemaChildren:?");
  
  if (sps)
  {
    for (i= 0; i < sps->schema_sps_num; i++)
    {
      [self addSP:sps->schema_sps+i
           schema:schema
          toArray:spArray];
    }
  }
}


- (id)outlineView:(NSOutlineView *)outlineView 
            child:(int)index 
           ofItem:(id)item
{
  if (item == nil)
    return [_items objectForKey:[[_children objectForKey:@"root"] objectAtIndex: index]];
  else
  {
    id tmp= [_children objectForKey:[item identifier]];
    if (!tmp)
    {
      [_tableFetcher performSelector:_tableFetcherAction
                          withObject:self
                          withObject:item];
      return @"Loading...";
    }
    else
    {
      return [_items objectForKey:[tmp objectAtIndex:index]];
    }
  }
}

- (BOOL)outlineView:(NSOutlineView *)outlineView 
   isItemExpandable:(id)item
{
  if ([item isMemberOfClass:[MSchemaItem class]] && [item isExpandableForType:_leafType])
    return YES;
  else
    return NO;
}

- (int)outlineView:(NSOutlineView *)outlineView 
numberOfChildrenOfItem:(id)item
{
  if (item == nil)
    return [[_children objectForKey:@"root"] count];
  else if ([item isKindOfClass: [NSString class]]) // the Loading... string
    return 0;
  else
  {
    id tmp= [_children objectForKey:[item identifier]];
    if (!tmp)
      return 1; // for Loading...
    else
      return [tmp count];
  }
}

- (id)outlineView:(NSOutlineView *)outlineView 
objectValueForTableColumn:(NSTableColumn *)tableColumn 
           byItem:(id)item
{
  if ([item isMemberOfClass: [MSchemaItem class]])
    return [item repr];
  else
    return item; // NSString for Loading...
}


- (MSchemaItem*)draggedItem
{
  return _draggedItem;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView
         writeItems:(NSArray *)items
       toPasteboard:(NSPasteboard *)pboard
{
  MSchemaItem *item= [items lastObject];
  
  [pboard declareTypes:[NSArray arrayWithObjects:MSchemaItemPboardType,nil] owner:nil];
  
  _draggedItem= item;
  
  [pboard setData:[NSData data] forType:MSchemaItemPboardType];
  return YES;
}

@end


//==============================================================================

@implementation MFilteredSchemaDataSource

- (id)initWithDataSource:(MSchemaDataSource*)parent
{
  self= [super initWithRoot:parent->_rootType leaf:parent->_leafType];
  if (self)
  {
    _parentDS= parent;
  }
  return self;
}


- (void)setRootList: (NSMutableArray*)array
{
  NSLog(@"NSFilteredSchemaDataSource should not have its rootList changed");
}


- (id)outlineView:(NSOutlineView *)outlineView 
            child:(int)index 
           ofItem:(id)item
{
  if (!_filter)
    return [_parentDS outlineView:outlineView child:index ofItem:item];
  else
    return [super outlineView:outlineView child:index ofItem:item];
  
}

- (BOOL)outlineView:(NSOutlineView *)outlineView 
   isItemExpandable:(id)item
{
  if (!_filter)
    return [_parentDS outlineView:outlineView isItemExpandable:item];
  else
    return [super outlineView:outlineView isItemExpandable:item];
}

- (int)outlineView:(NSOutlineView *)outlineView 
numberOfChildrenOfItem:(id)item
{
  if (!_filter)
    return [_parentDS outlineView:outlineView numberOfChildrenOfItem:item];
  else
    return [super outlineView:outlineView numberOfChildrenOfItem:item];
}

- (id)outlineView:(NSOutlineView *)outlineView 
objectValueForTableColumn:(NSTableColumn *)tableColumn 
           byItem:(id)item
{
  if (!_filter)
    return [_parentDS outlineView:outlineView
	objectValueForTableColumn:tableColumn
                           byItem:item];
  else
    return [super outlineView:outlineView
    objectValueForTableColumn:tableColumn
                       byItem:item];
}


- (BOOL)filterChildren:(NSArray*)children
                ofItem:(MSchemaItem*)item
              matching:(NSString*)filter
{
  unsigned int i, c= [children count];
  NSMutableArray *array= [NSMutableArray arrayWithCapacity:1];
  
  for (i= 0; i < c; i++)
  {
    id ident= [children objectAtIndex:i];
    id obj= [_parentDS->_items objectForKey:ident];
    NSArray *ch= [_parentDS->_children objectForKey:ident];
    BOOL ok= NO;
    
    if ([[obj repr] rangeOfString:filter].location!=NSNotFound)
      ok= YES;
    
    if (ch && [self filterChildren:ch ofItem:obj matching:filter])
      ok= YES;
    
    if (ok)
      [array addObject:ident];
  }
  
  [_children setObject:array forKey:[item identifier]];
  
  return [array count]>0;
}


- (BOOL)removeChildren:(NSMutableArray*)children
                ofItem:(MSchemaItem*)item
           notMatching:(NSString*)filter
{
  int i, c= [children count];
  int matches= 0;
  
  for (i= c-1; i >= 0; i--)
  {
    id ident= [children objectAtIndex:i];
    id obj= [_parentDS->_items objectForKey:ident];
    NSMutableArray *ch= [_parentDS->_children objectForKey:ident];
    
    if (ch && [self removeChildren:ch ofItem:obj notMatching:filter])
    {
      matches++;
    }
    else if ([[obj repr] rangeOfString:filter].location!=NSNotFound)
    {
      matches++;
    }
    else
    {
      [children removeObject:ident];
    }
  }
  
  return matches>0;
}


- (void)performSearch:(NSString*)filter
{
  int i, c;
  
  //  NSLog(@"filter by %@", filter);
  if (!filter || [filter length] == 0)
  {
    [_filter release];
    _filter= nil;
  }
  else if (!_filter || [filter rangeOfString:_filter].location == NSNotFound)
  {
    NSArray *array;
    NSMutableArray *rootArray= nil;
    
    [_filter release];
    _filter= [filter retain];
    
    [_children removeAllObjects];
    array= [_parentDS->_children objectForKey:@"root"];
    c= [array count];
    for (i= 0; i < c; i++)
    {
      if ([self filterChildren:[_parentDS->_children objectForKey:[array objectAtIndex:i]]
                        ofItem:[_parentDS->_items objectForKey:[array objectAtIndex:i]]
                      matching:filter])
      {
        if (!rootArray)
          rootArray= [NSMutableArray arrayWithCapacity:1];
        
        [rootArray addObject:[array objectAtIndex:i]];
      }
    }
    if (rootArray)
    {
      [_children setObject:rootArray forKey:@"root"];
      _items= _parentDS->_items;
    }
  }
  else // incremental search
  {
    NSMutableArray *array;
    
    [_filter release];
    _filter= [filter retain];
    
    array= [_children objectForKey:@"root"];
    c= [array count];
    for (i= c-1; i >= 0; i--)
    {
      if (![self removeChildren:[_children objectForKey:[array objectAtIndex:i]]
                         ofItem:[_parentDS->_items objectForKey:[array objectAtIndex:i]]
                    notMatching:filter])
      {
        [array removeObject:[array objectAtIndex:i]];
      }
    }
  }
}

@end