/
YEDNodeViewRegistry.j
63 lines (53 loc) · 1.54 KB
/
YEDNodeViewRegistry.j
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
@import <Foundation/CPObject.j>
@import <Foundation/CPDictionary.j>
@import <Foundation/CPKeyedArchiver.j>
@import <Foundation/CPKeyedUnarchiver.j>
@import "YEDNode.j"
var DefaultRegistry = nil;
@implementation YEDNodeViewRegistry : CPObject
{
CPDictionary nodeViewPrototypes;
}
- (id)init
{
self = [super init];
if(self)
{
nodeViewPrototypes = [CPDictionary dictionary];
}
return self;
}
+ (id)registry
{
return [[self alloc] init];
}
+ (id)defaultRegistry
{
if(!DefaultRegistry)
DefaultRegistry = [[self alloc] init];
return DefaultRegistry;
}
- (void)registerPrototype:(CPView)aView for:(id)aNodeOrClass
{
var data = [CPKeyedArchiver archivedDataWithRootObject:aView];
[nodeViewPrototypes setObject:data forKey:[aNodeOrClass className]];
}
- (CPView)viewFor:(id)aNodeOrClass
{
CPLog.trace("YEDNodeViewRegistry: finding view for " + [aNodeOrClass className]);
var data = [nodeViewPrototypes objectForKey:[aNodeOrClass className]];
if(data)
{
var view = [CPKeyedUnarchiver unarchiveObjectWithData:data];
if([aNodeOrClass isKindOfClass:YEDNode] && [aNodeOrClass respondsToSelector:@selector(isNode)] && [aNodeOrClass isNode])
{
[view setRepresentedObject:aNodeOrClass];
}
else if([aNodeOrClass isKindOfClass:YEDNode] && [aNodeOrClass respondsToSelector:@selector(nodeWithName:)])
{
[view setRepresentedObject:[aNodeOrClass nodeWithName:[aNodeOrClass className]]];
}
return view;
}
}
@end