File: MGRTTreeDataSource.m

package info (click to toggle)
mysql-admin 1.2.5rc-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 80,944 kB
  • ctags: 43,103
  • sloc: sql: 295,916; pascal: 256,535; cpp: 74,487; ansic: 68,881; objc: 26,417; sh: 16,867; yacc: 10,755; java: 9,917; xml: 8,453; php: 2,806; python: 2,068; makefile: 1,252; perl: 3
file content (357 lines) | stat: -rw-r--r-- 7,108 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
//
//  MGRTTreeDataSource.m
//  MySQL GRT
//
//  Created by Alfredo Kojima on 05/7/16.
//  Copyright 2005 MySQL AB. All rights reserved.
//

#import "MGRTTreeDataSource.h"
#include <myx_grt_public_interface.h>
#import <MySQLToolsCommon/myxutil.h>

NSString *MGRTTreeNodePboardType= @"MGRTTreeNodePboardType";

@interface MGRTNodeData : NSObject
{
  MYX_GRT_VALUE *_value;
  NSString *_name;
  NSImage *_icon;
  NSArray *_dictKeysToDisplay;
}
- (id)initWithValue:(MYX_GRT_VALUE*)value;
- (void)setDictKeysToDisplay:(NSArray*)keys;
- (int)count;
- (void)setTitle:(NSString*)name;
- (NSString*)description;
- (MYX_GRT_VALUE*)value;
- (MYX_GRT_VALUE*)childAtIndex:(int)index;
- (NSString*)childNameAtIndex:(int)index;
- (const char*)grtId;
- (BOOL)isExpandable;
- (void)setIcon:(NSImage*)icon;
- (NSImage*)icon;
- (BOOL)isList;
- (NSString*)dictStructName;
@end;


@implementation MGRTNodeData
- (id)initWithValue:(MYX_GRT_VALUE*)value
{
  self= [super init];
  if (self)
    _value= myx_grt_value_retain(value);
  return self;
}

- (void)dealloc
{
  [_icon release];
  [_name release];
  [_dictKeysToDisplay release];
  myx_grt_value_release(_value);
  [super dealloc];
}

- (void)setTitle:(NSString*)name
{
  [_name release];
  _name= [name retain];
}

- (void)setDictKeysToDisplay:(NSArray*)keys
{
  if (_dictKeysToDisplay != keys)
  {
    [_dictKeysToDisplay release];
    _dictKeysToDisplay= [keys retain];
  }
}


- (BOOL)isList
{
  if (myx_grt_value_get_type(_value) == MYX_LIST_VALUE)
    return YES;
  return NO;
}

- (int)count
{
  switch (myx_grt_value_get_type(_value))
  {
    case MYX_LIST_VALUE:
      return myx_grt_list_item_count(_value);
    case MYX_DICT_VALUE:
    {
      int i;
      int count= 0;
      for (i= 0; i < [_dictKeysToDisplay count]; i++)
      {
        NSString *key= [_dictKeysToDisplay objectAtIndex:i];
        MYX_GRT_VALUE *value;

        value= myx_grt_dict_item_get_value(_value, [key UTF8String]);
        if (value)
          count++;
      }
      return count;
    }
  }
  return 0;
}


- (NSString*)childNameAtIndex:(int)index
{
  return [_dictKeysToDisplay objectAtIndex:index];
}


- (MYX_GRT_VALUE*)childAtIndex:(int)index
{
  if (myx_grt_value_get_type(_value) == MYX_LIST_VALUE)
    return myx_grt_list_item_get(_value, index);
  else
  {
    int i;
    int count= 0;
    for (i= 0; i < [_dictKeysToDisplay count]; i++)
    {
      NSString *key= [_dictKeysToDisplay objectAtIndex:i];
      MYX_GRT_VALUE *value;
      
      value= myx_grt_dict_item_get_value(_value, [key UTF8String]);
      if (value)
      {
        if (count == index)
          return value;
        count++;
      }
    }
  }
  return NULL;
}


- (const char*)grtId
{
  return myx_grt_dict_id_item_as_string(_value);
}


- (MYX_GRT_VALUE*)value
{
  return _value;
}


- (NSString*)dictStructName
{
  if (myx_grt_value_get_type(_value) == MYX_DICT_VALUE)
    return NSStr(myx_grt_dict_struct_get_name(_value));
  else
    return nil;
}

- (NSString*)description
{
  if (myx_grt_value_get_type(_value) == MYX_DICT_VALUE)
    return NSStr(myx_grt_dict_name_item_as_string(_value));
  else
    return _name;
}


- (void)setIcon:(NSImage*)icon
{
  if (_icon != icon)
  {
    [_icon release];
    _icon= [icon retain];
  }
}

- (NSImage*)icon
{
  return _icon;
}

- (BOOL)isExpandable
{
  if (myx_grt_value_get_type(_value) == MYX_LIST_VALUE)
    return YES;
  else if (myx_grt_value_get_type(_value) == MYX_DICT_VALUE)
    return _dictKeysToDisplay ? YES : NO;
  else
    return NO;
}
@end





@implementation MGRTTreeDataSource


static void releaseObject(gpointer object)
{
  [(id)object release];
}


- (id)init
{
  self= [super init];
  if (self)
  {
    _nodeTable= g_hash_table_new_full(g_direct_hash, g_direct_equal,
                                      NULL, releaseObject);
    
    _listIcon= [[NSImage alloc] initWithContentsOfFile:[[NSBundle bundleForClass:[self class]] pathForImageResource:@"grt_value_list"]];
  }
  return self;
}


- (void)setDisplayInfo:(NSDictionary*)displayInfo
{
  if (_displayInfo != displayInfo)
  {
    [_displayInfo release];
    _displayInfo= [displayInfo retain];
  }
}


- (void)setIconInfo:(NSDictionary*)iconInfo
{
  if (_iconInfo != iconInfo)
  {
    [_iconInfo release];
    _iconInfo= [iconInfo retain];
  }  
}


- (void)setMGRT:(MGRT*)grt
{
  [_grt release];
  _grt= [grt retain];
}



- (void)dealloc
{
  g_hash_table_destroy(_nodeTable);
  [_grt release];
  [_listIcon release];
  [_iconInfo release];
  [_displayInfo release];
  [super dealloc];
}


- (void)reset
{
  g_hash_table_destroy(_nodeTable);
  _nodeTable= g_hash_table_new_full(g_direct_hash, g_direct_equal,
                                    NULL, releaseObject);  
}


- (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
  int c;
  if (!item)
    c= [_rootNode count];
  else
    c= [item count];
  return c;
}


- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
  return [item description];
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
  return item==nil || [item isExpandable];
}


- (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)parentItem
{
  if (!parentItem)
    parentItem= _rootNode;
  
  MYX_GRT_VALUE *child= [parentItem childAtIndex:index];
  id item= g_hash_table_lookup(_nodeTable, child);
  
  if (!item)
  {
    item= [[MGRTNodeData alloc] initWithValue:child];
    if ([item isList])
      [item setTitle:[parentItem childNameAtIndex:index]];

    [item setDictKeysToDisplay:[_displayInfo objectForKey:[item dictStructName]]];
    g_hash_table_insert(_nodeTable, child, item);
  }
  return item;
}

- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
  if ([[tableColumn identifier] isEqualToString:@"description"])
  {
    if ([item isList])
      [cell setImage:_listIcon];
    else
      [cell setImage:[_iconInfo objectForKey:[item dictStructName]]];
  }
}


- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
{
  NSMutableArray *data= [NSMutableArray arrayWithCapacity:[items count]];
  int i;
  for (i= 0; i < [items count]; i++)
  {
    id item= [items objectAtIndex:i];
    if (![item isList])
      [data addObject:NSStr([item grtId])];
  }
  
  [pboard declareTypes:[NSArray arrayWithObject:MGRTTreeNodePboardType] owner:nil];
  [pboard setPropertyList:data forType:MGRTTreeNodePboardType];
  
  return YES;
}

- (void)setRootValue:(MYX_GRT_VALUE*)root
{
  _rootValue= root;
  
  g_hash_table_destroy(_nodeTable);
  _nodeTable= g_hash_table_new_full(g_direct_hash, g_direct_equal,
                                    NULL, releaseObject);

  _rootNode= [[MGRTNodeData alloc] initWithValue:root];
//  [_rootNode setDictKeysToDisplay:[_displayInfo objectForKey:[_rootNode dictStructName]]];
  g_hash_table_insert(_nodeTable, root, _rootNode);
  
}


- (MYX_GRT_VALUE*)objectAtNode:(id)node
{
  return [(MGRTNodeData*)node value];
}

@end