File: ULOutlineViewDelegate.m

package info (click to toggle)
adun.app 0.81-9
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,212 kB
  • sloc: objc: 71,035; ansic: 6,681; yacc: 394; python: 75; makefile: 41; cpp: 36; xml: 15; awk: 3
file content (718 lines) | stat: -rw-r--r-- 16,967 bytes parent folder | download | duplicates (6)
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
/*
   Project: UL

   Copyright (C) 2006 Michael Johnston & Jordi Villa-Freixa

   Author: Michael Johnston

   This application is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This application is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/

#include "ULOutlineViewDelegate.h"


@class ULSelectionWrapper;
@class ULNodeWrapper;
@class ULOptionsLeaf;

/**
Base class used to display
an Adun options dictionary, as created using ULMenuExtensions,
in an NSOutlineView.
*/
@interface ULOutlineViewWrapper: NSObject
{
	int noChildren;
	NSMutableArray* children;
	id parent;
	id identifier;
	id wrappedObject;
	BOOL isExpandable;
	id value;
}

- (id) initWithObject: (id) object parent: (id) parent identifier: (id) aName;
- (int) numberChildren;
- (id) parent;
- (id) identifier;
- (id) childAtIndex: (int) index;
- (id) value;
- (id) wrappedObject;
- (void) childWasSelected: (id) child;

@end


@implementation ULOutlineViewWrapper

+ (id) wrapperForObject: (id) object parent: (id) par identifier: (id) aName
{
	NSDebugLLog(@"ULOutlineViewDelegate", @"Wrapper for object %@ with id %@", object, aName);

	if([object isKindOfClass: [NSDictionary class]])
	{
		if([object objectForKey: @"Selection"] != nil)
		{
			return [[[ULSelectionWrapper alloc] 
				initWithObject: object
				parent: par
				identifier: aName] autorelease];
		}
		else
		{
			 return [[[ULNodeWrapper alloc] 
				initWithObject: object 
				parent: par
				identifier: aName] autorelease];
		}
	}
	else
		return [[[ULOptionsLeaf alloc] 
				initWithObject: object 
				parent: par
				identifier: aName] autorelease];
}

- (id) initWithObject: (id) object parent: (id) parent identifier: (id) aName;
{
	return self;
}

- (int) numberChildren 
{ 
	return noChildren; 
}

- (id) parent 
{ 
	return parent; 
}

- (id) identifier 
{ 
	return identifier; 
}

- (id) childAtIndex: (int) index 
{ 
	return [children objectAtIndex: index]; 
}

- (BOOL) isExpandable 
{ 
	return isExpandable; 
}

- (id) value 
{ 
	return value;
}

- (void) setValue: (id) aValue
{
	[value release];
	value = aValue;
	[value retain];
}

- (id) wrappedObject 
{ 
	return wrappedObject;
}

- (void) childWasSelected: (id) child
{
	//does nothing
}

- (void) dealloc
{
	[children release];
	[parent release];
	[identifier release];
	[value release];
	[super dealloc];
}

@end

/**
ULNodeWrapper wraps  non-selection NSDictionaries
of an Adun options file
*/

@interface ULNodeWrapper: ULOutlineViewWrapper
- (void) setValue: (id) aValue forChild: (id) child;
- (NSString*) childForIdentifier: (NSString*)anId;
@end

@implementation ULNodeWrapper

- (void) _setChildren
{
	id anEnum, key;
	NSArray* array;
	
	noChildren = [wrappedObject count];
	array = [wrappedObject allKeys];
	array = [array sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
	anEnum = [array objectEnumerator];
	while(key = [anEnum nextObject])
	{
		[children addObject: 
			[ULOutlineViewWrapper wrapperForObject: [wrappedObject valueForKey: key] 
				parent: self
				identifier: key]];
	}

}

- (id) initWithObject: (id) object parent: (id) par identifier: (id) aName
{
	children = [NSMutableArray new];
	isExpandable = YES;
	value = @"";
	wrappedObject = object;
	parent = [par retain];
	identifier = [aName retain];
	[value retain];
	[self _setChildren];	

	return self;
}

- (void) setValue: (id) aValue forChild: (id) child
{
	NSString* key;

	if(![child isKindOfClass: [NSDictionary class]])
	{
		key = [child identifier];
		[wrappedObject setValue: aValue forKey: key];
		[child setValue: aValue];
	}
}

- (NSString*) childForIdentifier: (NSString*) anId
{
	NSEnumerator* childEnum;
	id child;

	childEnum = [children objectEnumerator];
	while(child = [childEnum nextObject])
		if([[child identifier] isEqual: anId])
			return child;

	return nil;
}

- (void) dealloc
{
	[super dealloc];
}

@end

/**
ULSelectionWrapper wraps  selection NSDictionaries
of an Adun options file. These are dictionaries that contain
the key @"Selection" and @"Type"
*/

@interface ULSelectionWrapper: ULNodeWrapper
{
	BOOL isMultiple;
}
- (void) setValue: (id) aValue forChild: (id) child;
- (BOOL) isMultiple;
@end

@implementation ULSelectionWrapper

- (id) _setChildrenForArray
{
	int i;
	id anEnum, key;
	id object, selectedValue, choiceArray;
	NSArray* defaultSelection, *array;
	NSEnumerator* selectionEnum;

	defaultSelection = nil;
	
	//the keys are not in a defined order
	//therefore find the choice array first
	//then deal with the default selection
	array = [wrappedObject allKeys];
	array = [array sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
	anEnum = [array objectEnumerator];
	while(key = [anEnum nextObject])
		if(![key isEqual: @"Selection"] && ![key isEqual: @"Type"])
			choiceArray = [wrappedObject valueForKey: key];
			for(i=0; i<[choiceArray count]; i++)
				[children addObject: 
					[ULOutlineViewWrapper wrapperForObject: @""
						parent: self
						identifier: [choiceArray objectAtIndex: i]]];
			
	
	anEnum = [wrappedObject keyEnumerator];
	while(key = [anEnum nextObject])
	{
		if([key isEqual: @"Selection"])
		{
			object = [wrappedObject valueForKey: @"Selection"];
			NSDebugLLog(@"ULOutlineViewDelegate", @"Selection value is %@", object);
			if(![object isKindOfClass: [NSArray class]])
				[NSException raise: NSInternalInconsistencyException
					format: @"Invalid structure for options selection object. Must be an array"];
			if(![object count] == 0)
			{
				//check the selection(s) is/are in the array 
				
				selectionEnum = [object objectEnumerator];
				while(selectedValue = [selectionEnum nextObject])
				{
					if(![choiceArray containsObject: selectedValue])
					{
						[NSException raise: NSInternalInconsistencyException
							format: [NSString stringWithFormat:
							 @"Selected value %@ is not in object", selectedValue]];
					}
					else
					{		
						defaultSelection = object;
					}
				}	
			}
		}
		else if([key isEqual: @"Type"])
			if([[wrappedObject valueForKey: @"Type"] isEqual: @"Single"])
				isMultiple = NO;
	}

	return defaultSelection;
}

- (id) _setChildrenForDictionary
{
	id anEnum, key;
	id object, selectedValue;
	NSArray* defaultSelection, *array;
	NSEnumerator* selectionEnum;

	defaultSelection = nil;
	array = [wrappedObject allKeys];
	array = [array sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
	anEnum = [array objectEnumerator];
	while(key = [anEnum nextObject])
	{
		if(![key isEqual: @"Selection"] && ![key isEqual: @"Type"])
		{
			object = [wrappedObject valueForKey: key];
			[children addObject: 
				[ULOutlineViewWrapper wrapperForObject: object 
					parent: self
					identifier: key]];
		}
		else if([key isEqual: @"Selection"])
		{
			object = [wrappedObject valueForKey: @"Selection"];
			NSDebugLLog(@"ULOutlineViewDelegate", @"Selection value is %@", object);
			if(![object isKindOfClass: [NSArray class]])
				[NSException raise: NSInternalInconsistencyException
					format: @"Invalid structure for options selection object. Must be an array"];
			if(![object count] == 0)
			{
				//check the selection(s) is/are in the dictionary
				
				selectionEnum = [object objectEnumerator];
				while(selectedValue = [selectionEnum nextObject])
					if([wrappedObject objectForKey: selectedValue] == nil)
					{
						[NSException raise: NSInternalInconsistencyException
							format: [NSString stringWithFormat:
							 @"Selected value %@ is not in object", selectedValue]];
					}
					else
					{		
						//FIXME: no support for multiple selections!
						defaultSelection = object;
					}
			}
		}
		else if([key isEqual: @"Type"])
			if([[wrappedObject valueForKey: @"Type"] isEqual: @"Single"])
				isMultiple = NO;
	}
	
	return defaultSelection;
}

- (void) _setChildren
{
	id anEnum, key;
	id object, optionsType;
	id defaultSelection;

	//first check is it an array or dictionary based selection
	//array based options can only contain 3 object (Type, Selection, and the array)

	if([wrappedObject count] <= 3)
	{
		anEnum = [wrappedObject keyEnumerator];
		while(key = [anEnum nextObject])
			if(![key isEqual: @"Selection"] && ![key isEqual: @"Type"])
			{
				object = [wrappedObject valueForKey: key];
				if([object isKindOfClass: [NSArray class]])
					optionsType = @"Array";
				else
					optionsType = @"Dict";
			}
	}
	else
		optionsType = @"Dict";

	NSDebugLLog(@"ULOutlineViewDelegate", @"Selection type is %@", optionsType);

	if([optionsType isEqual:  @"Dict"])
		defaultSelection = [self _setChildrenForDictionary];
	else
		defaultSelection = [self _setChildrenForArray];
	
	noChildren = [children count];
	if(defaultSelection != nil)
	{
		anEnum = [children objectEnumerator];
		while(object = [anEnum nextObject])
			if([defaultSelection containsObject: [object identifier]])
				[object setValue: @"Selected"];
	}
}

- (id) initWithObject: (id) object parent: (id) par identifier: (id) aName
{
	if(self = [super init])
	{
		children = [NSMutableArray new];
		isExpandable = YES;
		value = @"";
		wrappedObject = object;
		parent = [par retain];
		identifier = [aName retain];
		[value retain];
		isMultiple = YES;
		[self _setChildren];
	}	

	return self;
}

- (void) childWasSelected: (id) child
{
	id key;
	id previousSelection;

	NSDebugLLog(@"ULOutlineViewDelegate",
		@"Child %@ with value %@ was selected", child, [child value]);

	key = [child identifier];

	if([[child value] isEqual: @"Selected"])
	{
		NSDebugLLog(@"ULOutlineViewDelegate", 
			@"Child is Yes - Changing to no and removing from %@",
				 [[wrappedObject valueForKey: @"Selection"] description]);
		[child setValue: @""];
		[[wrappedObject valueForKey:@"Selection"] removeObject: key];
		NSDebugLLog(@"ULOptionsViewControlle", @"Selection is now %@",
				 [[wrappedObject valueForKey: @"Selection"] description]);
	}
	else
	{
		NSDebugLLog(@"ULOutlineViewDelegate", 
			@"Child is NO - Changing to Yes and adding to selection %@", 
				[[wrappedObject valueForKey: @"Selection"] description]);
		[child setValue: @"Selected"];
		if(!isMultiple)	
		{	
			//catch when nothing has been selected
			NS_DURING	
			{
				previousSelection = [self childForIdentifier: 
							[[wrappedObject valueForKey:@"Selection"] 
								objectAtIndex: 0]];
				[previousSelection setValue: @""];
				[[wrappedObject valueForKey:@"Selection"] removeAllObjects];
			}
			NS_HANDLER
			{
				if(![[localException name] isEqual: NSRangeException])
					[localException raise];
			}
			NS_ENDHANDLER
		}
		[[wrappedObject valueForKey:@"Selection"] addObject: [child identifier]];
		NSDebugLLog(@"ULOutlineViewDelegate", @"Selection is now %@", 
				[[wrappedObject valueForKey: @"Selection"] description]);
	}		
}

- (void) setValue: (id) aValue forChild: (id) child
{

}

- (void) dealloc
{
	[super dealloc];
}

- (BOOL) isMultiple
{
	return isMultiple;
}

@end

/**
ULOptionsLeaf wraps the final key:value option
pairs in an Adun options file i.e. where value
is a NSString. They are also used to display
the selection state of the elements of an ULSelectionWrapper
(YES, NO)
*/

@interface ULOptionsLeaf: ULOutlineViewWrapper
@end

@implementation ULOptionsLeaf

- (id) initWithObject: (id) object parent: (id) par identifier: (id) aName
{
	noChildren = 0;
	value = object;
	parent = [par retain];
	identifier = [aName retain];
	[value retain];
	return self;
}

- (void) dealloc
{
	[super dealloc];
}

@end

/******

The Main Class

*******/

@implementation ULOutlineViewDelegate

- (id) initWithProperties: (id) properties allowEditing: (BOOL) value
{
	if(self = [super init])
	{
		wrappedOptions = [ULOutlineViewWrapper 
					wrapperForObject: properties
					parent: nil
					identifier: @"root"];
		valueEditing = value;
		isProperties = YES;
		[wrappedOptions retain];
		outlineColumnIdentifier = @"Options";
		[outlineColumnIdentifier retain];	
	}

	return self;
}

- (id) initWithOptions: (id) options
{
	return [self initWithOptions: options
			outlineColumnIdentifier: @"Options"];
}

- (id) initWithOptions: (id) options outlineColumnIdentifier: (NSString*) aString
{
	if(self = [super init])
	{
		wrappedOptions = [ULOutlineViewWrapper 
					wrapperForObject: options
					parent: nil
					identifier: @"root"];
		valueEditing = YES;
		isProperties = NO;
		[wrappedOptions retain];
		if(aString == nil)
			outlineColumnIdentifier = @"Options";
		else
			outlineColumnIdentifier = aString;

		[outlineColumnIdentifier retain];	
	}

	
	return self;
}

- (void) dealloc
{
	[wrappedOptions release];
	[outlineColumnIdentifier release];
	[super dealloc];
}

/*************

OutlineView Data Source Methods

*************/

- (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
	return (item == nil) ? [wrappedOptions numberChildren] : [item numberChildren];
}

- (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
{
	return (item == nil) ? [wrappedOptions childAtIndex: index] : [item childAtIndex: index];
}

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

- (id)outlineView:(NSOutlineView *)outlineView 
	objectValueForTableColumn:(NSTableColumn *)tableColumn 
	byItem:(id)item
{
	if([[tableColumn identifier] isEqual:outlineColumnIdentifier])
	{
		return (item == nil) ? [wrappedOptions identifier] : [item identifier];
	}
	else
	{	
		//Return the item - the outline view will use its description.
#ifdef GNUSTEP
		return [item value];
#else	
		//Cocoa has multiline descriptions which can't be properly displayed
		//by the outline view. The "\n" have to removed from the description string.
		item = [[[item value] description] stringByReplacingString: @"\n" withString: @""];
		item = [item stringByReplacingString: @" " withString: @""];
		return [item stringByReplacingString: @"," withString: @", "];
#endif		
	}
}

- (void)outlineView:(NSOutlineView *)outlineView 
		setObjectValue:(id)object 
		forTableColumn:(NSTableColumn *)tableColumn 
		byItem:(id)item
{
	NSDebugLLog(@"ULOutlineViewDelegate", @"Object is %@. Item is %@", object, item);

	if(![item isExpandable])
		[[item parent] setValue: object forChild: item];
}

/***********

OutlineView Delegate Methods

************/

- (void) outlineViewSelectionDidChange: (NSNotification*) aNotification
{
	int selectedRow, i;
	id item, parent;
	id outlineView;

	if(isProperties)
		return;

	outlineView = [aNotification object];
	selectedRow = [outlineView selectedRow];
	if(selectedRow != -1)
	{
		item = [outlineView itemAtRow: [outlineView selectedRow]];
		parent = [item parent];
		NSDebugLLog(@"ULOutlineViewDelegate", @"Selection changed to %@ (%@). Parent %@",
				item, [item identifier], parent);
		[parent childWasSelected: item];
		//If this is a selection menu item we have to update the selection
		if([parent isKindOfClass: [ULSelectionWrapper class]])
		{
#ifdef GNUSTEP
			//This is the original code which works on gnustep
			//for single selection. i.e. when one child is selected the
			//other is deselected. However looking at the code now Im not
			//sure how this is possible! 
			//Possibly reloadItem calls reloadData or Im missing something.
			//However it definitely doesnt work on cocoa.
			//We will keep this however since if reloadItem: on gnustep does
			//involve calling reloadData then using the cocoa code there will
			//result in reloadData being called multiple redundant times.
			[outlineView reloadItem: item];
#else
			if([parent isMultiple])
				[outlineView reloadItem: item];
			else	
			{
				//Have to reload all children to ensure the previously selected
				//item appears deselected in the view
				for(i=0; i<[(ULOutlineViewWrapper*)parent numberChildren]; i++)
					[outlineView reloadItem:
						[parent childAtIndex: i]];
			}
#endif		
		}
	}	
}

- (BOOL) outlineView: (NSOutlineView*) outlineView shouldExpandItem: (id) item
{
	return YES;
}

- (BOOL) outlineView: (NSOutlineView*) outlineView 
		shouldEditTableColumn: (NSTableColumn*) tableColumn 
		item: (id) item
{
	//dont allow editing of the outline column

	if([[tableColumn identifier] isEqual: outlineColumnIdentifier])
		return NO;

	if(![item isExpandable] && isProperties)
	{
		if(valueEditing && [[(ULOutlineViewWrapper*)[item parent] identifier] isEqual: @"User Metadata"])
			return YES;
		else
			return NO;
	}	
	else if(![item isExpandable] && valueEditing)
		return YES;
	else
		return NO;
}

@end