-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYEDNodeView.j
312 lines (261 loc) · 9.68 KB
/
YEDNodeView.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
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
@import <AppKit/CPView.j>
@import <AppKit/CPTextField.j>
@import <AppKit/CPBox.j>
@import <Foundation/CPNotificationCenter.j>
@import "CPBox+CPCoding.j"
@import "CPView+OffsetCorners.j"
@import "YEDEditorView.j"
@import "YEDNode.j"
@import "YEDNodeViewConnector.j"
YEDNodeViewDragType = "YEDNodeViewDragType";
@implementation YEDNodeView : CPView
{
YEDNode representedObject @accessors;
CPView contentView;
CPTextField nameField;
CPView decorator;
CGPoint dragLocation;
BOOL isSelected @accessors;
YEDEditorView editorView;
YEDNodeViewConnector connector;
}
- (id)initWithFrame:aFrame
{
self = [super initWithFrame:aFrame];
if(self)
{
editorView = [[YEDEditorView alloc] initWithFrame:CPRectMakeZero()];
var bounds = [self bounds],
connectorSize = 9;
decorator = [[CPBox alloc] initWithFrame:CPRectMake(0,0,
CPRectGetWidth(aFrame),
CPRectGetHeight(aFrame))];
[decorator setAutoresizingMask:(CPViewWidthSizable | CPViewHeightSizable)];
[decorator setBorderType:CPLineBorder];
[self addSubview:decorator];
contentView = [[CPView alloc] initWithFrame:(CPRectMake(0,0,
CPRectGetWidth(aFrame),
CPRectGetHeight(aFrame)))];
[contentView setAutoresizingMask:(CPViewWidthSizable | CPViewHeightSizable)];
[decorator setContentView:contentView];
connector = [[YEDNodeViewConnector alloc] initWithFrame:CPRectMake(CGRectGetMaxX(bounds)-connectorSize,
CGRectGetMaxY(bounds)-connectorSize,
connectorSize,
connectorSize)];
[connector setAutoresizingMask:(CPViewMinXMargin | CPViewMinYMargin)];
[connector setNodeView:self];
[self addSubview:connector]
nameField = [CPTextField labelWithTitle:[self hash]];
[nameField setValue:CPCenterTextAlignment forThemeAttribute:@"alignment"];
[nameField setCenter:[self convertPoint:[self center] fromView:nil]];
[nameField setAutoresizingMask:(CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin)];
// //console.log("NodeView Center:");
// //console.log([self center]);
[nameField setCenter:[contentView convertPoint:[contentView center] fromView:nil]];
[contentView addSubview:nameField];
[self setPostsFrameChangedNotifications:YES];
[self registerForDraggedTypes:[YEDNodeViewConnectorDragType]];
}
return self;
}
- (id)initWithNode:(YEDNode)aNode
{
self = [self initWithFrame:CPRectMakeZero()];
if(self)
{
[self setRepresentedObject:aNode];
}
return self;
}
- (BOOL)isEqual:(id)other
{
if(other === self)
return YES;
if(!other || ![other isKindOfClass:[self class]])
return NO
return [self isEqualToNodeView:other];
}
- (BOOL)isEqualToNodeView:(YEDNodeView)otherView
{
if(otherView === self)
return YES;
if(![[self representedObject] isEqual:[otherView representedObject]])
return NO;
return YES;
}
- (CPString)name
{
return [representedObject name];
}
- (void)setRepresentedObject:(id)object
{
if(representedObject === object)
return;
[self willChangeValueForKey:@"representedObject"];
[representedObject removeObserver:self forKeyPath:@"name"];
representedObject = object;
[self syncWithRepresentedObject];
[representedObject addObserver:self forKeyPath:@"name" options:CPKeyValueObservingOptionNew context:nil];
[self didChangeValueForKey:@"representedObject"];
}
- (void)syncWithRepresentedObject
{
CPLog.trace("NodeView is updating from representedObject");
CPLog.trace("NodeView nameField was: " + [nameField stringValue]);
CPLog.trace("Node name is: " + [representedObject name]);
//console.debug(self);
[nameField setStringValue:[representedObject name]];
[nameField sizeToFit];
[nameField setCenter:[contentView convertPoint:[contentView center] fromView:decorator]];
[self setNeedsDisplay:YES];
}
- (void)observeValueForKeyPath:(CPString) path ofObject:(id)object change:(CPDictionary)change context:(id)context
{
if(object === representedObject)
{
[self syncWithRepresentedObject];
}
}
- (void)setIsSelected:(BOOL)selected
{
if(isSelected === selected)
return;
[self willChangeValueForKey:@"isSelected"];
isSelected = selected;
[self didChangeValueForKey:@"isSelected"];
if(isSelected)
{
CPLog.trace("Activating Editor View");
[editorView setNodeView:self];
[connector setHidden:NO];
}
else
{
[editorView setNodeView:nil];
[connector setHidden:YES];
}
}
- (void)removeFromSuperview
{
[self setIsSelected:NO];
[super removeFromSuperview];
}
- (void)mouseDown:(CPEvent)anEvent
{
dragLocation = [anEvent locationInWindow];
CPLog.trace("Selecting Node");
[[CPNotificationCenter defaultCenter]
postNotificationName:"YEDSelectedItemNotification"
object:self
userInfo:[CPDictionary dictionaryWithJSObject:{
"mouseDown":anEvent
}]];
}
- (void)mouseDragged:(CPEvent)anEvent
{
var location = [anEvent locationInWindow],
origin = [self frame].origin;
[self setFrameOrigin:CGPointMake(origin.x + location.x - dragLocation.x,
origin.y + location.y - dragLocation.y)];
dragLocation = location;
}
- (void)mouseUp:(CPEvent)anEvent
{
// Handle double-click
if([anEvent clickCount] == 2)
{
var newName = prompt("Node Name: ", [[self representedObject] name]);
if(newName && newName !== [[self representedObject] name])
{
[[self representedObject] setName:newName];
[self syncWithRepresentedObject];
}
}
}
- (void)performDragOperation:(CPDraggingInfo)aSender
{
CPLog.trace("YEDNodeView: performDragOperation:");
//console.debug([aSender draggingSource]);
var source = [aSender draggingSource];
// var nodeView = [CPKeyedUnarchiver unarchiveObjectWithData:[[aSender draggingPasteboard] dataForType:YEDNodeViewConnectorDragType]];
var nodeView = [[aSender draggingSource] nodeView];
[[self superview] connectNodeView:nodeView toNodeView:self];
}
@end
var YEDNodeViewContentViewKey = @"YEDNodeViewContentViewKey",
YEDNodeViewNameFieldKey = @"YEDNodeViewNameFieldKey",
YEDNodeViewDecoratorKey = @"YEDNodeViewDecoratorKey",
YEDNodeViewEditorViewKey = @"YEDNodeViewEditorViewKey",
YEDNodeViewConnectorKey = @"YEDNodeViewConnectorKey",
YEDNodeViewRepresentedObject = @"YEDNodeViewRepresentedObject";
@implementation YEDNodeView (CPCoding)
- (id)initWithCoder:(CPCoder)coder
{
self = [super initWithCoder:coder];
if(self)
{
contentView = [coder decodeObjectForKey:YEDNodeViewContentViewKey];
nameField = [coder decodeObjectForKey:YEDNodeViewNameFieldKey];
decorator = [coder decodeObjectForKey:YEDNodeViewDecoratorKey];
editorView = [coder decodeObjectForKey:YEDNodeViewEditorViewKey];
connector = [coder decodeObjectForKey:YEDNodeViewConnectorKey];
[connector setNodeView:self];
representedObject = [coder decodeObjectForKey:YEDNodeViewRepresentedObject];
[self setPostsFrameChangedNotifications:YES];
[self registerForDraggedTypes:[YEDNodeViewConnectorDragType]];
}
return self;
}
- (void)encodeWithCoder:(CPCoder)coder
{
[super encodeWithCoder:coder];
[coder encodeObject:contentView forKey:YEDNodeViewContentViewKey];
[coder encodeObject:nameField forKey:YEDNodeViewNameFieldKey];
[coder encodeObject:decorator forKey:YEDNodeViewDecoratorKey];
[coder encodeObject:editorView forKey:YEDNodeViewEditorViewKey];
[coder encodeObject:connector forKey:YEDNodeViewConnectorKey];
[coder encodeObject:representedObject forKey:YEDNodeViewRepresentedObject];
}
@end
@implementation YEDNodeView (DecoratorProtocol)
- (CPColor)borderColor
{
if([decorator respondsToSelector:@selector(borderColor)])
return [decorator borderColor];
}
- (void)setBorderColor:(CPColor)color
{
if([decorator respondsToSelector:@selector(setBorderColor:)])
[decorator setBorderColor:color];
}
- (float)borderWidth
{
if([decorator respondsToSelector:@selector(borderWidth)])
return [decorator borderWidth];
}
- (void)setBorderWidth:(float)width
{
if([decorator respondsToSelector:@selector(setBorderWidth:)])
[decorator setBorderWidth:width];
}
- (float)cornerRadius
{
if([decorator respondsToSelector:@selector(cornerRadius)])
return [decorator cornerRadius];
}
- (void)setCornerRadius:(float)radius
{
if([decorator respondsToSelector:@selector(setCornerRadius:)])
[decorator setCornerRadius:radius];
}
- (CPColor)fillColor
{
if([decorator respondsToSelector:@selector(fillColor)])
return [decorator fillColor];
}
- (void)setFillColor:(CPColor)color
{
if([decorator respondsToSelector:@selector(setFillColor:)])
[decorator setFillColor:color];
}
@end