-
Notifications
You must be signed in to change notification settings - Fork 2
/
OMModelFactory.m
333 lines (243 loc) · 9.08 KB
/
OMModelFactory.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
/*
Copyright (C) 2013 Quentin Mathe
Author: Quentin Mathe <[email protected]>
Date: March 2013
License: Modified BSD (see COPYING)
*/
#import "OMModelFactory.h"
#import "OMConstants.h"
@implementation OMModelFactory
@synthesize editingContext = _editingContext, undoTrack = _undoTrack;
- (id) initWithEditingContext: (COEditingContext *)aContext
undoTrack: (COUndoTrack *)aUndoTrack
{
NILARG_EXCEPTION_TEST(aContext);
SUPERINIT;
ASSIGN(_editingContext, aContext);
ASSIGN(_undoTrack, aUndoTrack);
[self registerAdditionalPropertyDescriptions];
[self registerEntityDescriptionsOfCoreObjectGraphDemo];
return self;
}
- (id) init
{
return [self initWithEditingContext: nil undoTrack: nil];
}
- (void) dealloc
{
DESTROY(_editingContext);
[super dealloc];
}
- (NSSet *) rootObjects
{
return (id)[[[[self editingContext] persistentRoots] mappedCollection] rootObject];
}
- (COSmartGroup *) allObjectGroup
{
COSmartGroup *group = [[OMSmartGroup alloc]
initWithObjectGraphContext: [COObjectGraphContext objectGraphContext]];
[group setName: _(@"All Objects")];
[group setContentBlock: ^ ()
{
return [[self rootObjects] allObjects];
}];
return group;
}
- (NSArray *) libraries
{
return [[[self editingContext] libraryGroup] contentArray];
}
- (COSmartGroup *) whereGroup
{
// TODO: Turn whereGroup into a smart group that dynamically computes the content
COSmartGroup *group = [[OMSmartGroup alloc]
initWithObjectGraphContext: [COObjectGraphContext objectGraphContext]];
id <ETCollection> content =
[A([self allObjectGroup]) arrayByAddingObjectsFromArray: [self libraries]];
[group setName: [_(@"Where") uppercaseString]];
[group setTargetCollection: content];
return group;
}
- (NSArray *) tagGroups
{
return [[[self editingContext] tagLibrary] tagGroups];
}
- (COSmartGroup *) whatGroup
{
COSmartGroup *group = [[OMSmartGroup alloc]
initWithObjectGraphContext: [COObjectGraphContext objectGraphContext]];
[group setName: [_(@"What") uppercaseString]];
[group setTargetCollection: [self tagGroups]];
return group;
}
- (COSmartGroup *) whenGroup
{
COSmartGroup *group = [[OMSmartGroup alloc]
initWithObjectGraphContext: [COObjectGraphContext objectGraphContext]];
[group setName: [_(@"When") uppercaseString]];
return group;
}
- (void) buildCoreObjectGraphDemoIfNeeded
{
COSmartGroup *allObjectGroup = [self allObjectGroup];
if ([[allObjectGroup content] isEmpty] == NO)
return;
[self buildCoreObjectGraphDemo];
[allObjectGroup refresh];
ETAssert([allObjectGroup isEmpty] == NO);
}
- (NSArray *) sourceListGroups
{
[self buildCoreObjectGraphDemoIfNeeded];
return A([self whereGroup], [self whatGroup], [self whenGroup]);
}
- (id) insertNewRootObjectWithEntityName: (NSString *)anEntityName
{
return [[[self editingContext]
insertNewPersistentRootWithEntityName: anEntityName] rootObject];
}
- (void) registerAdditionalPropertyDescriptions
{
ETAssert([self editingContext] != nil);
// NOTE: Could be put in a package description plist or something similar.
ETModelDescriptionRepository *repo = [[self editingContext] modelDescriptionRepository];
ETEntityDescription *dateType = [repo entityDescriptionForClass: [NSDate class]];
ETEntityDescription *stringType = [repo entityDescriptionForClass: [NSString class]];
ETEntityDescription *entity = [repo entityDescriptionForClass: [COObject class]];
ETPropertyDescription *modificationDate =
[ETPropertyDescription descriptionWithName: @"modificationDate" type: dateType];
[modificationDate setReadOnly: YES];
ETPropertyDescription *creationDate =
[ETPropertyDescription descriptionWithName: @"creationDate" type: dateType];
[creationDate setReadOnly: YES];
ETPropertyDescription *sizeDescription =
[ETPropertyDescription descriptionWithName: @"sizeDescription" type: stringType];
[sizeDescription setDerived: YES];
[sizeDescription setDisplayName: _(@"Size")];
[entity addPropertyDescription: modificationDate];
[entity addPropertyDescription: creationDate];
[entity addPropertyDescription: sizeDescription];
[repo addDescription: creationDate];
[repo addDescription: modificationDate];
[repo addDescription: sizeDescription];
NSMutableArray *warnings = [NSMutableArray array];
[repo checkConstraints: warnings];
ETAssert([warnings isEmpty]);
}
- (void) registerEntityDescriptionsOfCoreObjectGraphDemo
{
[self registerEntityDescriptionWithName: @"OMCity" parent: @"COObject"];
[self registerEntityDescriptionWithName: @"OMPicture" parent: @"COObject"];
[self registerEntityDescriptionWithName: @"OMPerson" parent: @"COObject"];
}
- (void) registerEntityDescriptionWithName: (NSString *)aName parent: (NSString *)aParentName
{
ETModelDescriptionRepository *repo = [[self editingContext] modelDescriptionRepository];
ETEntityDescription *entity = [ETEntityDescription descriptionWithName: aName];
[entity setParent: [repo descriptionForName: aParentName]];
[repo addDescription: entity];
ETEntityDescription *libraryEntity =
[COLibrary makeEntityDescriptionWithName: [aName stringByAppendingString: @"Library"]
contentType: aName];
[repo addUnresolvedDescription: libraryEntity];
[repo resolveNamedObjectReferences];
}
- (void) buildCoreObjectGraphDemo
{
// TODO: For every library, the name should be made read-only.
/* Cities */
COObject *a1 = [self insertNewRootObjectWithEntityName: @"OMCity"];
COObject *a2 = [self insertNewRootObjectWithEntityName: @"OMCity"];
COObject *a3 = [self insertNewRootObjectWithEntityName: @"OMCity"];
[a1 setName: @"New York"];
[a2 setName: @"London"];
[a3 setName: @"Tokyo"];
COLibrary *cityLibrary = [self insertNewRootObjectWithEntityName: @"OMCityLibrary"];
[cityLibrary setName: @"Cities"];
[cityLibrary addObjects: A(a1, a2, a3)];
/* Pictures */
COObject *b1 = [self insertNewRootObjectWithEntityName: @"OMPicture"];
COObject *b2 = [self insertNewRootObjectWithEntityName: @"OMPicture"];
COObject *b3 = [self insertNewRootObjectWithEntityName: @"OMPicture"];
COObject *b4 = [self insertNewRootObjectWithEntityName: @"OMPicture"];
[b1 setName: @"Sunset"];
[b2 setName: @"Eagle on a snowy Beach"];
[b3 setName: @"Cloud"];
[b4 setName: @"Fox"];
COLibrary *pictureLibrary = [self insertNewRootObjectWithEntityName: @"OMPictureLibrary"];
[pictureLibrary setName: @"Pictures"];
[pictureLibrary addObjects: A(b1, b2, b3, b4)];
/* Persons */
COObject *c1 = [self insertNewRootObjectWithEntityName: @"OMPerson"];
COObject *c2 = [self insertNewRootObjectWithEntityName: @"OMPerson"];
[c1 setName: @"Ann"];
[c2 setName: @"John"];
COLibrary *personLibrary = [self insertNewRootObjectWithEntityName: @"OMPersonLibrary"];
[personLibrary setName: @"Persons"];
[personLibrary addObjects: A(c1, c2)];
/* Scenery Tag */
COTag *sceneryTag = [self insertNewRootObjectWithEntityName: @"Anonymous.COTag"];
[sceneryTag setName: _(@"scenery")];
[sceneryTag addObjects: A(b1, b2)];
/* Animal Tag */
COTag *animalTag = [self insertNewRootObjectWithEntityName: @"Anonymous.COTag"];
[animalTag setName: _(@"animal")];
[animalTag addObjects: A(b4, b2)];
/* Rain Tag */
COTag *rainTag = [self insertNewRootObjectWithEntityName: @"Anonymous.COTag"];
[rainTag setName: _(@"rain")];
[rainTag addObjects: A(a2, a3, b3, b4)];
/* Snow Tag */
COTag *snowTag = [self insertNewRootObjectWithEntityName: @"Anonymous.COTag"];
[snowTag setName: _(@"snow")];
[rainTag addObjects: A(a1, b2)];
/* Tag Groups */
COTagGroup *natureTagGroup = [self insertNewRootObjectWithEntityName: @"Anonymous.COTagGroup"];
[natureTagGroup setName: _(@"Nature")];
[natureTagGroup addObjects: A(sceneryTag, animalTag, rainTag, snowTag)];
COTagGroup *weatherTagGroup = [self insertNewRootObjectWithEntityName: @"Anonymous.COTagGroup"];
[weatherTagGroup setName: _(@"Weather")];
[weatherTagGroup addObjects: A(rainTag, snowTag)];
COTagGroup *unclassifiedTagGroup = [self insertNewRootObjectWithEntityName: @"Anonymous.COTagGroup"];
[unclassifiedTagGroup setName: _(@"Unclassified")];
/* Declare the groups used as tags and commit */
[[[self editingContext] tagLibrary] addObjects: A(rainTag, sceneryTag, animalTag, snowTag)];
[[[self editingContext] tagLibrary] setTagGroups: A(natureTagGroup, weatherTagGroup, unclassifiedTagGroup)];
NSError *error = nil;
[[self editingContext] commitWithIdentifier: kOMCommitBuildDemo
undoTrack: [self undoTrack]
error: &error];
// TODO: Error handling
ETAssert(error == nil);
}
@end
@implementation OMGroup
- (NSImage *) icon
{
return nil;
}
@end
@implementation OMSmartGroup
- (NSImage *) icon
{
return nil;
}
@end
@implementation COContainer (OMNote)
+ (NSMenuItem *) noteMenuItem
{
NSMenuItem *menuItem = [NSMenuItem menuItemWithTitle: _(@"Note")
tag: 0
action: NULL];
[menuItem setRepresentedObject: self];
NSMenu *menu = [menuItem submenu];
[menu addItemWithTitle: _(@"New List…")
action: @selector(addNewList:)
keyEquivalent: @""];
return menuItem;
}
+ (NSArray *) menuItems
{
return A([self noteMenuItem]);
}
@end