-
Notifications
You must be signed in to change notification settings - Fork 2
/
OMAppController.m
165 lines (132 loc) · 4.28 KB
/
OMAppController.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
/*
Copyright (C) 2011 Quentin Mathe
Author: Quentin Mathe <[email protected]>
Date: November 2011
License: Modified BSD (see COPYING)
*/
#import "OMAppController.h"
#import "OMLayoutItemFactory.h"
#import "OMModelFactory.h"
// FIXME: Remove
@interface ETWidgetLayout (Private)
- (NSView *) layoutViewWithoutScrollView;
@end
@implementation OMAppController
@synthesize currentPresentationTitle, editingContext, mainUndoTrack;
- (void) dealloc
{
DESTROY(editingContext);
DESTROY(itemFactory);
DESTROY(openedGroups);
DESTROY(mainUndoTrack);
DESTROY(currentPresentationTitle);
[super dealloc];
}
- (id) initWithObjectGraphContext: (COObjectGraphContext *)aContext
{
self = [super initWithNibName: nil bundle: nil objectGraphContext: aContext];
ASSIGN(itemFactory, [OMLayoutItemFactory factoryWithObjectGraphContext: aContext]);
openedGroups = [[NSMutableSet alloc] init];
return self;
}
- (void) setUpMenus
{
[[ETApp mainMenu] addItem: [ETApp objectMenuItem]];
[[ETApp mainMenu] addItem: [ETApp editMenuItem]];
[[ETApp mainMenu] addItem: [ETApp viewMenuItem]];
[[[[ETApp editMenuItem] submenu] itemWithTitle: _(@"Select All")]
setAction: @selector(selectAllExceptInSourceList:)];
[[[[ETApp viewMenuItem] submenu] itemWithTitle: _(@"List")] setState: NSOnState];
ASSIGN(currentPresentationTitle, _(@"List"));
}
- (void) setUpUndoTrack: (BOOL)clear
{
ETAssert([self editingContext] != nil);
ETUUID *trackUUID = [[NSUserDefaults standardUserDefaults] UUIDForKey: @"OMMainUndoTrackUUID"];
if (trackUUID == nil)
{
trackUUID = [ETUUID UUID];
[[NSUserDefaults standardUserDefaults] setUUID: trackUUID
forKey: @"OMMainUndoTrackUUID"];
}
ASSIGN(mainUndoTrack, [COUndoTrack trackForName: [trackUUID stringValue]
withEditingContext: [self editingContext]]);
if (clear)
{
[mainUndoTrack clear];
}
}
- (NSString *) storePath
{
return [@"~/TestObjectStore" stringByExpandingTildeInPath];
}
- (void) setUpEditingContext: (BOOL)clear
{
if (clear && [[NSFileManager defaultManager] fileExistsAtPath: [self storePath]])
{
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath: [self storePath]
error: &error];
ETAssert(error == nil);
}
COEditingContext *ctxt =
[COEditingContext contextWithURL: [NSURL fileURLWithPath: [self storePath]]];
ETAssert(ctxt != nil);
ASSIGN(editingContext, ctxt);
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(didCommit:)
name: COEditingContextDidChangeNotification
object: editingContext];
}
- (void) setUpAndShowBrowserUI
{
[[itemFactory windowGroup] setController: self];
[self browseMainGroup: nil];
}
- (void) applicationDidFinishLaunching: (NSNotification *)notif
{
BOOL clear = YES;
[self setUpMenus];
[self setUpEditingContext: clear];
[self setUpUndoTrack: clear];
[self setUpAndShowBrowserUI];
}
- (void) didCommit: (NSNotification *)notif
{
COCommand *command = [[notif userInfo] objectForKey: kCOCommandKey];
BOOL isUndoOrRedo = (command == nil);
if (isUndoOrRedo)
return;
ETLog(@"Recording command %@ on %@", command, mainUndoTrack);
ETAssert([mainUndoTrack currentNode] != nil);
}
- (IBAction) browseMainGroup: (id)sender
{
OMModelFactory *modelFactory =
[[[OMModelFactory alloc] initWithEditingContext: [self editingContext]
undoTrack: mainUndoTrack] autorelease];
ETLayoutItemGroup *browser = [itemFactory browserWithGroup: [modelFactory sourceListGroups]
editingContext: [self editingContext]];
[[itemFactory windowGroup] addItem: browser];
[openedGroups addObject: [modelFactory allObjectGroup]];
// FIXME: Call -prepareInitialFocusedItem: with some delay since
// the view are not backed by windows yet (i.e. the layout update is run just
// before the next event)
}
- (IBAction) undo: (id)sender
{
[mainUndoTrack undo];
}
- (IBAction) redo: (id)sender
{
[mainUndoTrack redo];
}
- (IBAction) browseUndoHistory: (id)sender
{
ETAssert(mainUndoTrack != nil);
ETLayoutItemGroup *browser = [[ETLayoutItemFactory factory]
historyBrowserWithRepresentedObject: mainUndoTrack title: nil];
[[[ETLayoutItemFactory factory] windowGroup] addItem: browser];
}
@end