forked from erkyrath/Inform7-IDE-Mac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIFNewProject.m
558 lines (459 loc) · 18.4 KB
/
IFNewProject.m
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
//
// IFNewProject.m
// Inform
//
// Created by Andrew Hunter on Fri Sep 12 2003.
// Copyright (c) 2003 Andrew Hunter. All rights reserved.
//
#import "IFNewProject.h"
#import "IFProjectFile.h"
#import "IFProject.h"
#import "IFEmptyProject.h"
#import "IFStandardProject.h"
#import "IFEmptyNaturalProject.h"
#import "IFNaturalExtensionProject.h"
@implementation IFNewProject
// = Initialisation =
static NSArray* projects = nil;
static NSMutableArray* projectClasses;
static NSMutableDictionary* projectDictionary = nil;
+ (void) initialize {
// List of projects
projects = [[NSArray arrayWithObjects:
[[IFEmptyNaturalProject alloc] init],
[[IFNaturalExtensionProject alloc] init],
[[IFEmptyProject alloc] init],
[[IFStandardProject alloc] init],
nil] retain];
// Project dictionary
projectClasses = [[NSMutableArray array] retain];
projectDictionary = [[NSMutableDictionary dictionary] retain];
NSEnumerator* projectEnum = [projects objectEnumerator];
NSObject<IFProjectType>* project;
while (project = [projectEnum nextObject]) {
NSMutableArray* curList = [projectDictionary objectForKey:
[project projectHeading]];
if (curList != nil) {
[curList addObject: project];
} else {
curList = [NSMutableArray arrayWithObject: project];
[projectDictionary setObject: curList
forKey: [project projectHeading]];
[projectClasses addObject: [project projectHeading]];
}
}
}
+ (void) finalize {
[projects release];
[projectClasses release];
[projectDictionary release];
}
- (id) init {
self = [super initWithWindowNibName: @"NewProject"];
if (self) {
[self setShouldCascadeWindows: NO];
projectType = nil;
projectView = nil;
}
return self;
}
- (void) dealloc {
if (projectType) [projectType release];
if (projectView) [projectView release];
[super dealloc];
}
- (void) windowDidLoad {
// Center the window
NSRect screenFrame = [[[self window] deepestScreen] frame];
NSRect winFrame = [[self window] frame];
winFrame.origin.x = screenFrame.origin.x + (screenFrame.size.width/2) -
(winFrame.size.width/2);
winFrame.origin.y = screenFrame.origin.y + (screenFrame.size.height/2) -
(winFrame.size.height/2);
[[self window] setFrame: winFrame
display: YES];
// Expand the project list
NSEnumerator* classEnum = [projectClasses objectEnumerator];
NSString* projectClass;
while (projectClass = [classEnum nextObject]) {
[projectTypes expandItem: projectClass];
}
// Select the first project type
[projectTypes selectRow: [projectTypes rowForItem: [projects objectAtIndex: 0]]
byExtendingSelection: NO];
// Display the first pane
[projectTypeView setFrame: [projectPaneView bounds]];
[projectPaneView addSubview: projectTypeView];
[previousButton setEnabled: NO];
[nextButton setTitle: [[NSBundle mainBundle] localizedStringForKey: @"Next"
value: @"Next"
table: nil]];
currentView = projectTypeView;
}
// = Interface =
- (void) manuallyCreateProject {
if ([projectType respondsToSelector: @selector(createAndOpenDocument:)]) {
if ([projectType createAndOpenDocument: [projectType saveFilename]]) {
// Success
[self close];
return;
} else {
[projectPaneView addSubview: currentView];
NSBeginAlertSheet([[NSBundle mainBundle] localizedStringForKey: @"Unable to create project"
value: @"Unable to create project"
table: nil],
[[NSBundle mainBundle] localizedStringForKey: @"Cancel"
value: @"Cancel"
table: nil],
nil, nil,
[self window],
nil,
nil,
nil,
nil,
[[NSBundle mainBundle] localizedStringForKey: @"Inform was unable to save the project file"
value: @"Inform was unable to save the project file"
table: nil]);
}
return;
}
// Use information from the project type to create the project
IFProjectFile* theFile = [[IFProjectFile alloc] initDirectoryWithFileWrappers: [NSDictionary dictionary]];
BOOL success;
[theFile setFilename: [projectType saveFilename]];
[projectType setupFile: theFile
fromView: projectView];
success = [theFile writeToFile: [projectType saveFilename]
atomically: YES
updateFilenames: YES];
if (success) {
[self close];
// Owing to a bug in NSFileWrapper (I think), we can't set the
// 'hidden extension' attribute there, so we use NSFileManager
// to do this instead.
if (hideExtension) {
NSMutableDictionary* attributes = [[[NSFileManager defaultManager] fileAttributesAtPath: [projectLocation stringValue] traverseLink: YES] mutableCopy];
[attributes setObject: [NSNumber numberWithBool: YES]
forKey: NSFileExtensionHidden];
[[NSFileManager defaultManager] changeFileAttributes: attributes
atPath: [projectLocation stringValue]];
[attributes release];
}
NSDocument* newDoc = [[IFProject alloc] initWithContentsOfFile: [projectType saveFilename]
ofType: [projectType openAsType]];
[[NSDocumentController sharedDocumentController] addDocument: [newDoc autorelease]];
[newDoc makeWindowControllers];
[newDoc showWindows];
[theFile release];
} else {
[projectPaneView addSubview: currentView];
NSBeginAlertSheet([[NSBundle mainBundle] localizedStringForKey: @"Unable to create project"
value: @"Unable to create project"
table: nil],
[[NSBundle mainBundle] localizedStringForKey: @"Cancel"
value: @"Cancel"
table: nil],
nil, nil,
[self window],
nil,
nil,
nil,
nil,
[[NSBundle mainBundle] localizedStringForKey: @"Inform was unable to save the project file"
value: @"Inform was unable to save the project file"
table: nil]);
}
}
- (void) confirmDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo {
if (returnCode == NSAlertAlternateReturn) {
// Only happens when we're manually creating the project
[self manuallyCreateProject];
}
}
- (IBAction) nextView: (id) sender {
[currentView removeFromSuperview];
BOOL shouldUseFinalPage = YES;
if (projectType && [projectType respondsToSelector: @selector(showFinalPage)]) {
shouldUseFinalPage = [projectType showFinalPage];
}
if ((currentView == projectTypeView && projectView == nil) ||
(projectView != nil && currentView == [projectView view] && shouldUseFinalPage)) {
// Change to the file location view
currentView = projectLocationView;
if ([[projectLocation stringValue] isEqualTo: @""]) {
// Pop up the save dialog
[self chooseLocation: self];
}
[previousButton setEnabled: YES];
[nextButton setEnabled: ![[projectLocation stringValue] isEqualTo: @""]];
[nextButton setTitle: [[NSBundle mainBundle] localizedStringForKey: @"Finished"
value: @"Finished"
table: nil]];
} else if (currentView == projectTypeView) {
// Change to the project type view
currentView = [projectView view];
[previousButton setEnabled: YES];
[nextButton setEnabled: YES];
[nextButton setTitle: [[NSBundle mainBundle] localizedStringForKey: @"Next"
value: @"Next"
table: nil]];
if (!shouldUseFinalPage) {
[nextButton setTitle: [[NSBundle mainBundle] localizedStringForKey: @"Finished"
value: @"Finished"
table: nil]];
}
} else if (currentView == [projectView view] && !shouldUseFinalPage) {
[projectPaneView addSubview: currentView];
// Get the file to save and finish up (must implement these selectors now)
NSString* confirm = nil;
NSString* error = nil;
if ([projectType respondsToSelector: @selector(errorMessage)]) {
error = [projectType errorMessage];
}
if (!error && [projectType respondsToSelector: @selector(confirmationMessage)]) {
confirm = [projectType confirmationMessage];
}
if (error) {
// Display an error message
NSBeginAlertSheet([[NSBundle mainBundle] localizedStringForKey: @"Unable to create project"
value: @"Unable to create project"
table: nil],
[[NSBundle mainBundle] localizedStringForKey: @"Cancel"
value: @"Cancel"
table: nil],
nil, nil,
[self window],
nil,
nil,
nil,
nil,
error);
return;
}
if (confirm) {
// Display a confirmation message (FIXME: do something about the result)
NSBeginAlertSheet([[NSBundle mainBundle] localizedStringForKey: @"Are you sure you wish to create this project?"
value: @"Are you sure you wish to create this project?"
table: nil],
[[NSBundle mainBundle] localizedStringForKey: @"Cancel"
value: @"Cancel"
table: nil],
[[NSBundle mainBundle] localizedStringForKey: @"Create"
value: @"Create"
table: nil],
nil,
[self window],
self,
@selector(confirmDidEnd:returnCode:contextInfo:),
nil,
nil,
confirm);
return;
}
[self manuallyCreateProject];
} else if (currentView == projectLocationView) {
// Save and finish up
IFProjectFile* theFile = [[IFProjectFile alloc] initWithEmptyProject];
BOOL success;
[theFile setFilename: [projectLocation stringValue]];
[projectType setupFile: theFile
fromView: projectView];
success = [theFile writeToFile: [projectLocation stringValue]
atomically: YES
updateFilenames: YES];
if (success) {
[self close];
// Owing to a bug in NSFileWrapper (I think), we can't set the
// 'hidden extension' attribute there, so we use NSFileManager
// to do this instead.
if (hideExtension) {
NSMutableDictionary* attributes = [[[NSFileManager defaultManager] fileAttributesAtPath: [projectLocation stringValue] traverseLink: YES] mutableCopy];
[attributes setObject: [NSNumber numberWithBool: YES]
forKey: NSFileExtensionHidden];
[[NSFileManager defaultManager] changeFileAttributes: attributes
atPath: [projectLocation stringValue]];
[attributes release];
}
/*
IFProject* openDoc;
openDoc = [[IFProject alloc] initWithContentsOfFile: [projectLocation stringValue]
ofType: @"Inform project file"];
if (openDoc) {
[openDoc makeWindowControllers];
[[openDoc windowControllers] makeObjectsPerformSelector: @selector(showWindow:)
withObject: self];
}
*/
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfFile: [projectLocation stringValue]
display: YES];
[theFile release];
} else {
[projectPaneView addSubview: currentView];
NSBeginAlertSheet([[NSBundle mainBundle] localizedStringForKey: @"Unable to create project"
value: @"Unable to create project"
table: nil],
[[NSBundle mainBundle] localizedStringForKey: @"Cancel"
value: @"Cancel"
table: nil],
nil, nil,
[self window],
nil,
nil,
nil,
nil,
[[NSBundle mainBundle] localizedStringForKey: @"Inform was unable to save the project file"
value: @"Inform was unable to save the project file"
table: nil]);
}
} else {
// Change to the project type view
[previousButton setEnabled: NO];
[nextButton setTitle: [[NSBundle mainBundle] localizedStringForKey: @"Next"
value: @"Next"
table: nil]];
[nextButton setEnabled: YES];
currentView = projectTypeView;
}
[currentView setFrame: [projectPaneView bounds]];
[projectPaneView addSubview: currentView];
}
- (IBAction) previousView: (id) sender {
[currentView removeFromSuperview];
if (currentView == projectLocationView &&
projectView != nil) {
currentView = [projectView view];
} else {
// Change to the project type view
[previousButton setEnabled: NO];
currentView = projectTypeView;
}
[nextButton setTitle: [[NSBundle mainBundle] localizedStringForKey: @"Next"
value: @"Next"
table: nil]];
[nextButton setEnabled: YES];
[currentView setFrame: [projectPaneView bounds]];
[projectPaneView addSubview: currentView];
}
- (IBAction) cancel: (id) sender {
[self close];
}
- (IBAction) chooseLocation: (id) sender {
// Setup a save panel
NSSavePanel* panel = [NSSavePanel savePanel];
[panel setAccessoryView: nil];
[panel setRequiredFileType: @"inform"];
[panel setCanSelectHiddenExtension: YES];
[panel setDelegate: self];
[panel setPrompt: [[NSBundle mainBundle] localizedStringForKey: @"Create"
value: @"Create"
table: nil]];
[panel setTreatsFilePackagesAsDirectories: NO];
// Show it
[panel beginSheetForDirectory: @"~"
file: nil
modalForWindow: [self window]
modalDelegate: self
didEndSelector: @selector(savePanelDidEnd:returnCode:contextInfo:)
contextInfo: NULL];
}
- (void) close {
[[self window] orderOut: self];
// (Allowing a delay ensures that there is no crash after the window closes: the file selector can be a bit tetchy)
[self performSelector: @selector(autorelease)
withObject: nil
afterDelay: 30.0];
}
- (void)savePanelDidEnd:(NSSavePanel *) sheet
returnCode:(int) returnCode
contextInfo:(void *) contextInfo {
if (returnCode == NSOKButton) {
hideExtension = [sheet isExtensionHidden];
[projectLocation setStringValue: [sheet filename]];
[nextButton setEnabled: YES];
if (![[projectLocation stringValue] isEqualTo: @""]) {
// Act as if we finished
[self nextView: self];
}
} else {
// Change nothing - return to the previous view
[self previousView: self];
}
}
// = Outline view =
- (int) outlineView:(NSOutlineView *)outlineView
numberOfChildrenOfItem:(id)item {
if (item == nil) {
return [projectClasses count];
}
NSInteger classNum = [projectClasses indexOfObjectIdenticalTo: item];
if (classNum == NSNotFound) {
return 0;
}
return [[projectDictionary objectForKey:
[projectClasses objectAtIndex: classNum]] count];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView
isItemExpandable:(id)item {
if (item == nil) return YES;
return [projectClasses indexOfObjectIdenticalTo: item] != NSNotFound;
}
- (id)outlineView:(NSOutlineView *)outlineView
child:(int)index
ofItem:(id)item {
if (item == nil) {
return [projectClasses objectAtIndex: index];
}
NSInteger classNum = [projectClasses indexOfObjectIdenticalTo: item];
if (classNum == NSNotFound) {
return nil;
}
return [[projectDictionary objectForKey:
[projectClasses objectAtIndex: classNum]] objectAtIndex: index];
}
- (id) outlineView:(NSOutlineView *)outlineView
objectValueForTableColumn:(NSTableColumn *)tableColumn
byItem:(id)item {
if ([item isKindOfClass: [NSString class]]) {
// Must be a project class heading
NSFontManager* mgr = [NSFontManager sharedFontManager];
NSFont* boldFont = [mgr convertFont: [NSFont systemFontOfSize: 12]
toHaveTrait: NSBoldFontMask];
NSAttributedString* str = [[NSAttributedString alloc] initWithString: item
attributes: [NSDictionary dictionaryWithObjectsAndKeys: boldFont, NSFontAttributeName, nil]];
return [str autorelease];
}
// Must be a NSObject<IFProjectType> object
NSAttributedString* str = [[NSAttributedString alloc] initWithString: [item projectName]];
return [str autorelease];
}
- (BOOL) outlineView: (NSOutlineView*) outlineView
shouldSelectItem: (id) item {
if (item == nil)
return NO;
NSInteger classNum = [projectClasses indexOfObjectIdenticalTo: item];
if (classNum == NSNotFound) {
return YES;
}
return NO;
}
- (void)outlineViewSelectionDidChange:(NSNotification *)notification {
NSObject<IFProjectType>* obj = [projectTypes itemAtRow: [projectTypes selectedRow]];
if (obj == nil ||
![obj conformsToProtocol: @protocol(IFProjectType)]) {
[nextButton setEnabled: NO];
[[[projectDescriptionView textStorage] mutableString] setString: [[NSBundle mainBundle] localizedStringForKey: @"Please choose a project type"
value: @"Please choose a project type"
table: nil]];
return;
}
if (projectType) [projectType release];
if (projectView) [projectView release];
projectType = [obj retain];
projectView = [obj configView];
if (projectView) {
[projectView retain];
}
[[projectDescriptionView textStorage] setAttributedString: [obj projectDescription]];
[nextButton setEnabled: YES];
}
@end