File: ULTemplateViewController.m

package info (click to toggle)
adun.app 0.81-15
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,384 kB
  • sloc: objc: 70,952; ansic: 6,668; yacc: 394; python: 75; cpp: 36; makefile: 33; xml: 15; awk: 3
file content (591 lines) | stat: -rw-r--r-- 15,822 bytes parent folder | download
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
/* 
   Project: UL

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

   Author: Michael Johnston

   Created: 2006-06-01 10:15:49 +0200 by 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 <AppKit/AppKit.h>
#include "ULTemplateViewController.h"

static id templateViewController;

@implementation ULTemplateViewController

- (void) _clearSectionMenus
{	
	NSEnumerator* sectionEnum;
	NSMutableDictionary* sectionMenu;

	sectionEnum = [sections objectEnumerator];
	while((sectionMenu = [sectionEnum nextObject]))
		[sectionMenu removeAllObjects];
}

- (void) _addComponentWithName: (NSString*) name toSectionMenu: (NSMutableDictionary*) menu
{
	NSMutableDictionary* newMenuItem;
	NSDictionary *values;
	NSEnumerator* keyEnum;
	NSString *key;
	
	//Create a menu item for this template which we will
	//display in the interface
	newMenuItem = [NSMutableDictionary newNodeMenu: NO];
	[menu addMenuItem: name withValue: newMenuItem];	
	
	values = [simulationTemplate valuesForObjectTemplateWithName: name];
	keyEnum = [values keyEnumerator];
	while((key = [keyEnum nextObject]))
		[newMenuItem addMenuItem: key
			withValue: [values objectForKey: key]];
}

- (void) _updateTemplateView
{
	NSMutableDictionary* menu;
	NSString* section;
	
	section = [popUpList titleOfSelectedItem];
	menu = [sections objectForKey: section];

	[outlineDelegate release];
	outlineDelegate = nil;
	outlineDelegate  = [[ULOutlineViewDelegate alloc]
				initWithOptions: menu
				outlineColumnIdentifier: @"Component"];
	[currentTemplateView setDelegate: outlineDelegate];
	[currentTemplateView setDataSource: outlineDelegate];
	[currentTemplateView reloadData];
	[currentTemplateView expandAllItems];
}

- (void) _updateTemplateWithMenuValues
{
	NSEnumerator* sectionEnum, *componentEnum;
	NSMutableDictionary* sectionMenu;
	NSString *componentName;
	id values;

	sectionEnum = [sections objectEnumerator];
	while((sectionMenu = [sectionEnum nextObject]))
	{
		componentEnum = [sectionMenu keyEnumerator];
		while((componentName = [componentEnum nextObject]))
		{
			values = [sectionMenu objectForKey: componentName];
			[simulationTemplate setValues: values
				forObjectTemplateWithName: componentName];
		}
	}
}

+ (void) initialize
{
	templateViewController = nil;
}

+ (id) templateViewController
{
	if(templateViewController == nil)
		templateViewController = [ULTemplateViewController new];
	
	return templateViewController;
}

- (id) init
{
	NSArray* names;
	NSMutableDictionary* newSection;

	if(templateViewController != nil)
		return templateViewController;

	if((self = [super init]))
	{
		sectionDescriptions = [NSDictionary dictionaryWithObjectsAndKeys:
					@"Specify the systems in your simulation", 
					@"System",
					@"Create the force field to be used", 
					@"ForceField",
					@"Specify the configuration generation method", 
					@"Configuration Generation",
					@"Choose a controller", 
					@"Controller", nil];
		[sectionDescriptions retain];

		displayNames = [NSMutableDictionary new];
		names = [[ULTemplate systemObjectTemplates]
				valueForKey: @"DisplayName"];
		names = [names sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
		[displayNames setObject: names forKey: @"System"];
		names = [[ULTemplate configurationGenerationObjectTemplates]
				valueForKey: @"DisplayName"];
		names = [names sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
		[displayNames setObject: names forKey: @"Configuration Generation"];
		names = [[ULTemplate forceFieldObjectTemplates] 
				valueForKey: @"DisplayName"];
		names = [names sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
		[displayNames setObject: names forKey: @"ForceField"];
		names = [[ULTemplate controllerTemplates] 
				valueForKey: @"Class"];
		names = [names sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
		[displayNames setObject: names forKey: @"Controller"];

		if([NSBundle loadNibNamed: @"Template" owner: self] == NO)
		{
			NSWarnLog(@"Problem loading interface");
			return nil;
		}

		sections = [NSMutableDictionary new];
		newSection = [NSMutableDictionary newNodeMenu: NO];
		[sections setObject: newSection forKey: @"System"];
		newSection = [NSMutableDictionary newNodeMenu: NO];
		[sections setObject: newSection forKey: @"ForceField"];
		newSection = [NSMutableDictionary newNodeMenu: NO];
		[sections setObject: newSection forKey: @"Configuration Generation"];
		newSection = [NSMutableDictionary newNodeMenu: NO];
		[sections setObject: newSection forKey: @"Controller"];

		outlineDelegate = nil;

		//Maps the view section names to the ULTemplate section names
		viewToTemplateSectionMap = [NSDictionary dictionaryWithObjectsAndKeys:
						@"forceField", @"ForceField",
						@"system", @"System",
						@"configurationGeneration", @"Configuration Generation",
						@"controller", @"Controller", nil];
		[viewToTemplateSectionMap retain];				
		simulationTemplate = nil;

		templateViewController = self;
	}	

	return self;
}

- (void) dealloc
{
	[[NSNotificationCenter defaultCenter]
		removeObserver: self];
	[viewToTemplateSectionMap release];
	[simulationTemplate release];
	[outlineDelegate release];
	[sections release];
	[sectionDescriptions release];
	[displayNames release];
	[super dealloc];
}

- (void) _outlineViewSelectionDidChange: (NSNotification*) aNotifcation
{
	int row, level;
	id item;
	NSDictionary* template;
	NSString* info;

	row = [currentTemplateView selectedRow];
	if(row == -1)
	{
		template = nil;
		info = @"";
	}
	else
	{
		level = [currentTemplateView levelForRow: row];
		item =  [currentTemplateView itemAtRow: row];
		
		if(level == 0)
		{
			template = [simulationTemplate objectTemplateWithName: 
					[item identifier]];
			info = [template templateDescription];
		}
		else
		{
			template = [simulationTemplate objectTemplateWithName: 
					[[item parent] identifier]];
			info = [template descriptionForKey: [item identifier]];
		}
	}	

	if(info == nil)
		info = @"No description available";

	[componentDescriptionField setStringValue: info];
}

- (void) _updateObservation
{
	[[NSNotificationCenter defaultCenter]
		removeObserver: self
		name: NSOutlineViewSelectionDidChangeNotification
		object: nil];
	[[NSNotificationCenter defaultCenter]
		addObserver: self
		selector: @selector(_outlineViewSelectionDidChange:)
		name: NSOutlineViewSelectionDidChangeNotification
		object: currentTemplateView];
}

- (void) awakeFromNib
{
	id tableColumn;

	currentTemplateView = templateView;
	[self _updateObservation];
	[popUpList removeAllItems];
	[popUpList addItemWithTitle: @"System"];
	[popUpList addItemWithTitle: @"ForceField"];
	[popUpList addItemWithTitle: @"Configuration Generation"];
	[popUpList addItemWithTitle: @"Controller"];
	[popUpList selectItemWithTitle: @"System"];
	[componentTable setDataSource: self];
	[componentTable setDelegate: self];
	[templateWindow setDelegate: self];

	//Allow scrolling of the value column of the template views
	tableColumn = [[templateView tableColumns] objectAtIndex: 1]; 
	[[tableColumn dataCell] setScrollable: YES];
	tableColumn = [[templateDisplayView tableColumns] objectAtIndex: 1]; 
	[[tableColumn dataCell] setScrollable: YES];
}

- (void) open: (id) sender
{
	currentTemplateView = templateView;
	[self _updateObservation];
	[tabView selectTabViewItemAtIndex: 1];
	propertyViewController = [ULPropertiesPanel propertiesPanel];
	[simulationTemplate release];
	simulationTemplate = [ULMutableTemplate new];
	[popUpList selectItemWithTitle: @"System"];
	[self changeSection: self];
	[templateWindow center];
	[templateWindow setTitle: @"New Template"];
	[templateWindow makeKeyAndOrderFront: self];
}

- (void) display: (id) sender
{
	ULPasteboard* pasteboard;
	id aTemplate;

	pasteboard = [ULPasteboard appPasteboard];
	aTemplate = [pasteboard objectForType: @"ULTemplate"]; 

	[self displayTemplate: aTemplate];
}

- (void) displayTemplate: (id) aTemplate
{
	NSString* section, *name;
	NSEnumerator *sectionEnum, *templateNameEnum;
	NSDictionary* templates;
	NSMutableDictionary* menu;

	[tabView selectTabViewItemAtIndex: 2];
	currentTemplateView = templateDisplayView;
	[self _updateObservation];
	propertyViewController = [ULPropertiesPanel propertiesPanel];
	
	[simulationTemplate release];
	simulationTemplate = aTemplate; 
	[simulationTemplate retain];

	//Create the menus
	sectionEnum = [sections keyEnumerator];
	while((section = [sectionEnum nextObject]))
	{
		menu = [sections objectForKey: section];
		//We have to get the template section name
		//corresponding to the view section name
		templates = [simulationTemplate objectTemplatesInSection: 
				[viewToTemplateSectionMap objectForKey: section]];
		templateNameEnum = [templates keyEnumerator];
		while((name = [templateNameEnum nextObject]))
			[self _addComponentWithName: name toSectionMenu: menu];
	}

	[popUpList selectItemWithTitle: @"System"];
	[self changeSection: self];
	[self _updateTemplateView];

	[templateWindow center];
	[templateWindow setTitle: 
		[NSString stringWithFormat: @"Template %@", 
			[simulationTemplate name]]];
	[templateWindow makeKeyAndOrderFront: self];
}

- (void) _cleanUp
{
	[componentTable deselectAll: self];
	[componentTable setNeedsDisplay: YES];
	[templateView setDelegate: nil];
	[templateView setDataSource: nil];
	[templateView reloadData];
	[templateDisplayView setDelegate: nil];
	[templateDisplayView setDataSource: nil];
	[templateDisplayView reloadData];
	[self _clearSectionMenus];
}

- (void) close: (id) sender
{
	[templateWindow orderOut: self];
	[templateWindow close];
}

- (void) validate: (id) sender
{
	NSError* error;
	NSDictionary* userInfo;
	NSString* suggestion;

	[self _updateTemplateWithMenuValues];

	error = nil;
	if(![simulationTemplate validateTemplate: &error])
	{
		userInfo = [error userInfo];
		suggestion = [NSString stringWithFormat: @"%@\n%@",
				[userInfo objectForKey: @"AdDetailedDescriptionKey"],
				[userInfo objectForKey: @"NSRecoverySuggestionKey"]];
		NSRunAlertPanel(
			[userInfo objectForKey: NSLocalizedDescriptionKey],
			suggestion,
			@"Dismiss", 
			nil,
			nil);
	}
	else
	{
		NSRunAlertPanel(@"Validation successful",
			@"",
			@"Dismiss", 
			nil,
			nil);

	}
}

- (void) save: (id) sender
{
	BOOL result;
	id newTemplate;

	[self _updateTemplateWithMenuValues];
	if(![simulationTemplate validateTemplate: NULL])
	{
		NSRunAlertPanel(@"Template not valid",
			@"Run validate on it to find the problem",
			@"Dismiss", 
			nil,
			nil);
		return;	
	}

	result = NSRunAlertPanel(@"External References",
			[[simulationTemplate externalReferences] description],
			@"Continue",
			@"Cancel",
			nil);
	if(result != NSOKButton)
		return;

	/*
	 * If the template is mutable it must be a new template. 
	 * Therefore we make a immutable copy of it before saving.
	 * We then allow the user to edit the metadata of the new template.
	 * If its not then it must be an already saved template.
	 * In this case we save it directly so any values modified
	 * by the user will be saved.
	 */
	if([simulationTemplate isKindOfClass: [ULMutableTemplate class]])
	{
		newTemplate = [[simulationTemplate copy] autorelease];
		[propertyViewController displayMetadataForModelObject: newTemplate	
			allowEditing: YES
			runModal: YES];
		//Return YES or NO depending on which button was pushed.	
		result = [propertyViewController result];	
	}	
	else
	{
		result = YES;
		newTemplate = simulationTemplate;
	}	

	[self close: self];
	if(result)
		[[ULDatabaseInterface databaseInterface]
			addObjectToFileSystemDatabase: newTemplate];
}

- (void) editAsNew: (id) sender
{
	id oldTemplate;

	currentTemplateView = templateView;
	[self _updateObservation];
	oldTemplate = simulationTemplate;
	simulationTemplate = [simulationTemplate mutableCopy];
	[oldTemplate release];
	[tabView selectTabViewItemAtIndex: 1];
	[popUpList selectItemWithTitle: @"System"];
	[self changeSection: self];
	[self _updateTemplateView];
	[templateWindow center];
	[templateWindow setTitle: @"New Template"];
}

- (void) addComponent: (id) sender
{
	int selectedRow;
	NSString* section, *displayName;
	NSDictionary* template;
	NSMutableDictionary *menu;
	NSString *name;

	//Find which component was selected and
	//retrieve the corresponding template
	selectedRow = [componentTable selectedRow];

	if(selectedRow == -1)
		return;
	
	section = [popUpList titleOfSelectedItem];
	displayName = [[displayNames objectForKey: section] 
			objectAtIndex: selectedRow];

	if([section isEqual: @"Controller"])
		template = [NSDictionary templateForClass: 
				displayName];
	else	
		template = [NSDictionary templateForDisplayName: 
				displayName];

	name = [simulationTemplate addObjectTemplate: template];
	menu = [sections objectForKey: section];
	[self _addComponentWithName: name toSectionMenu: menu];
	[self _updateTemplateView];
}

- (void) removeComponent: (id) sender
{
	int itemLevel, row;
	id item;
	NSString* section, *componentName;
	NSMutableDictionary* menu;

	row = [currentTemplateView selectedRow];
	if(row == -1)
		return;

	//FIXME Add a method to ULOutlineViewDelegate to do this
	item = [currentTemplateView itemAtRow: [currentTemplateView selectedRow]];
	itemLevel = [currentTemplateView levelForItem: item];
	if(itemLevel != 0)
		return;

	componentName = [item identifier];

	section = [popUpList titleOfSelectedItem];
	menu = [sections objectForKey: section];
	[menu removeObjectForKey: componentName];
	[simulationTemplate removeObjectTemplateWithName: componentName];
	[self _updateTemplateView];
}

- (void) changeSection: (id) sender
{
	NSString* section, *description;

	section = [popUpList titleOfSelectedItem];
	description = [sectionDescriptions objectForKey: section];
	[sectionDescriptionField setStringValue: description];
	[componentTable reloadData];
	[componentTable deselectAll: self];

	[componentDescriptionField setStringValue: @""];
	[self _updateTemplateView];
}

/*
 * Component Table data source methods
 */

- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{	
	NSString* section;

	section = [popUpList titleOfSelectedItem];
	return [[displayNames objectForKey: section] count];
}

- (id)tableView:(NSTableView *)aTableView
	 objectValueForTableColumn:(NSTableColumn *)aTableColumn 
	row:(int)rowIndex
{
	NSString* section;

	section = [popUpList titleOfSelectedItem];
	return [[displayNames objectForKey: section] objectAtIndex: rowIndex];
}

/*
 * Component Table delegate methods
 */

- (void) tableViewSelectionDidChange: (NSNotification*) aNotification
{
	int selectedRow;
	NSString* section, *displayName;
	NSDictionary* template;

	selectedRow = [componentTable selectedRow];
	if(selectedRow == -1)
	{
		[componentDescriptionField setStringValue: @""];
	}
	else
	{
		section = [popUpList titleOfSelectedItem];
		displayName = [[displayNames objectForKey: section] 
				objectAtIndex: selectedRow];

		template = [NSDictionary templateForDisplayName: displayName];
		[componentDescriptionField setStringValue: 
			[template templateDescription]];
	}		
}
 
/*
 * Window delegate methods
 */

- (void) windowWillClose: (NSNotification*) aNotification
{
	[self _cleanUp];
}
 
@end