1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.model.visual.backward;
19
20 import java.util.Collections;
21 import java.util.LinkedList;
22 import java.util.List;
23
24 import org.eclipse.draw2d.geometry.Dimension;
25 import org.eclipse.draw2d.geometry.Point;
26
27 import pl.edu.agh.cast.Activator;
28 import pl.edu.agh.cast.backward.figure.icons.NodeIconProvider;
29 import pl.edu.agh.cast.model.attributes.Attribute;
30 import pl.edu.agh.cast.model.attributes.AttributeManager;
31 import pl.edu.agh.cast.model.attributes.AttributeValue;
32 import pl.edu.agh.cast.model.attributes.NodeAttributeManager;
33
34 import com.thoughtworks.xstream.annotations.XStreamAlias;
35 import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
36 import com.thoughtworks.xstream.annotations.XStreamOmitField;
37
38
39
40
41
42
43 @XStreamAlias("node")
44 public class Node extends ModelElement implements Cloneable, IMoveable {
45
46
47
48
49
50 private static boolean suppressLocationChangeEvents = false;
51
52 private static final long serialVersionUID = -7236469860157722758L;
53
54
55
56
57
58
59 public static final String LOCATION = "Node.location";
60
61
62
63
64 public static final String CONNECTIONS = "Node.Connections";
65
66
67
68
69 public static final String SELECTED = "Node.selected";
70
71
72
73
74 public static final String SELECTED_PRIMARY = "Node.selectedPrimary";
75
76
77
78
79
80
81 public static final String DEFAULT_NODE_TYPE = "default";
82
83
84
85
86 @XStreamAlias("location")
87 protected Point location = new Point(-1, -1);
88
89
90
91
92 @XStreamAlias("dimensions")
93 protected Dimension dimensions = new Dimension(60, 60);
94
95
96
97
98 @XStreamAlias("type")
99 @XStreamAsAttribute
100 private String type = DEFAULT_NODE_TYPE;
101
102
103
104
105 @XStreamAlias("imageId")
106 @XStreamAsAttribute
107 protected String imageId;
108
109
110
111
112 @XStreamOmitField
113 protected List<ConnectionGroup> connectionGroups;
114
115
116
117
118 @XStreamAlias("attributeManager")
119 private NodeAttributeManager attributeManager;
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public Node(String id, boolean isMainNode, NodeAttributeManager attributeManager) {
134 super();
135
136 this.attributeManager = attributeManager;
137
138 if (id == null) {
139 throw new IllegalArgumentException("Node id cannot be null");
140 }
141
142
143 setAttributeValue(NodeAttributeManager.PERMANENT_ID, id);
144 setAttributeValue(NodeAttributeManager.PERMANENT_LABEL, id);
145 setAttributeValue(NodeAttributeManager.PERMANENT_ISMAIN, Boolean.valueOf(isMainNode));
146 connectionGroups = new LinkedList<ConnectionGroup>();
147
148 bindToAttributeManager();
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163 protected Node(String id, boolean isMainNode, NodeAttributeManager attributeManager, Point location) {
164 this(id, isMainNode, attributeManager);
165 this.location = location;
166 }
167
168
169
170
171
172
173
174
175 @Override
176 protected Object readResolve() {
177 this.connectionGroups = new LinkedList<ConnectionGroup>();
178 if (this.attributeManager == null) {
179 Activator.getLogger().debug("Creating new NodeAttributeManager");
180 this.attributeManager = new NodeAttributeManager();
181 }
182 super.readResolve();
183 return this;
184 }
185
186
187
188
189
190
191
192 public void addConnectionGroup(ConnectionGroup connectionGroup) {
193 if (!connectionGroups.contains(connectionGroup)) {
194 connectionGroups.add(connectionGroup);
195 firePropertyChange(CONNECTIONS, null, connectionGroup);
196 }
197 }
198
199
200
201
202
203
204
205
206 public boolean removeConnectionGroup(ConnectionGroup connectionGroup) {
207 boolean result = connectionGroups.remove(connectionGroup);
208 firePropertyChange(CONNECTIONS, connectionGroup, null);
209 return result;
210 }
211
212
213
214
215
216
217 public List<ConnectionGroup> getConnectionGroups() {
218 return Collections.unmodifiableList(connectionGroups);
219 }
220
221
222
223
224
225
226 public String getId() {
227 AttributeValue attributeValue = getAttributeValue(NodeAttributeManager.PERMANENT_ID);
228 return (String)attributeValue.getValue();
229 }
230
231
232
233
234
235
236
237
238
239 public String getLabel() {
240 StringBuilder part1 = new StringBuilder(60);
241 StringBuilder part2 = new StringBuilder(60);
242
243 for (Attribute a : getAttributeManager().getAttributes()) {
244 if (a.isShowAsLabel()) {
245 AttributeValue attributeValue = getAttributeValue(a.getName());
246 if (null != attributeValue.getValue()) {
247 String s = attributeValue.format();
248 if (attributeValue.getAttribute().getType().isList()) {
249 s = s.replace(';', '\n');
250 }
251 (a.isPermanent() ? part1 : part2).append(s).append('\n');
252 }
253 }
254 }
255 if (part1.length() > 0 || part2.length() > 0) {
256
257 if (part1.length() > 0 && part2.length() == 0) {
258 part1.delete(part1.length() - 1, part1.length());
259 }
260 if (part2.length() > 0) {
261 part2.delete(part2.length() - 1, part2.length());
262 }
263 return part1.append(part2).toString();
264 } else {
265 return "";
266 }
267 }
268
269
270
271
272
273
274 public boolean isMainNode() {
275 return ((Boolean)getAttributeValue(NodeAttributeManager.PERMANENT_ISMAIN).getValue()).booleanValue();
276 }
277
278
279
280
281
282
283
284 public void setType(String type) {
285 this.type = type;
286 }
287
288
289
290
291
292
293 public String getType() {
294 return type;
295 }
296
297
298
299
300
301
302 public Point getLocation() {
303 return location;
304 }
305
306
307
308
309
310
311 public void setLocation(Point newLocation) {
312 Point oldLocation = location;
313 location = newLocation;
314 if (!Node.isSuppressLocationChangeEvents()) {
315 firePropertyChange(Node.LOCATION, oldLocation, newLocation);
316 }
317 }
318
319
320
321
322
323
324 public Dimension getDimensions() {
325 return dimensions;
326 }
327
328
329
330
331
332
333
334 public void setDimensions(Dimension dimensions) {
335 this.dimensions = dimensions;
336 }
337
338
339
340
341
342
343
344 public void copyTo(Node that) {
345 that.attributeManager = attributeManager;
346 copyAttributes(that);
347
348 that.setDimensions(this.getDimensions());
349 that.setLocation(this.getLocation());
350 that.setImageId(this.getImageId());
351 that.setType(this.getType());
352
353 }
354
355
356
357
358
359
360 public String getImageId() {
361 if (imageId == null) {
362 return NodeIconProvider.getInstance().getIconId(getType());
363 } else {
364 return imageId;
365 }
366 }
367
368 public void setImageId(String id) {
369 imageId = id;
370 }
371
372
373
374
375
376
377 public int getTotalTargetConnectionsCount() {
378 int count = 0;
379 for (ConnectionGroup group : connectionGroups) {
380 count += group.getTargetConnectionCountFor(this);
381 }
382 return count;
383 }
384
385
386
387
388
389
390 public int getTotalSourceConnectionsCount() {
391 int count = 0;
392 for (ConnectionGroup group : connectionGroups) {
393 count += group.getSourceConnectionCountFor(this);
394 }
395 return count;
396 }
397
398
399
400
401
402
403 public int getTotalConnectionsCount() {
404 return getTotalTargetConnectionsCount() + getTotalSourceConnectionsCount();
405 }
406
407
408
409
410
411
412
413
414 public boolean isConnected(Node node) {
415 for (ConnectionGroup group : getConnectionGroups()) {
416 if (group.isBetween(this, node) && (group.getConnectionCount() > 0)) {
417 return true;
418 }
419 }
420 return false;
421 }
422
423
424
425
426
427
428 @Override
429 public String toString() {
430 return "PN " + getLabel();
431 }
432
433
434
435
436
437
438
439
440 @Override
441 public boolean equals(Object obj) {
442 if (obj == this) {
443 return true;
444 }
445 if (!(obj instanceof Node)) {
446 return false;
447 }
448
449 Node that = (Node)obj;
450 if (!getId().equals(that.getId())) {
451 return false;
452 }
453 return true;
454 }
455
456
457
458
459
460
461 @Override
462 public int hashCode() {
463 return getId().hashCode();
464 }
465
466
467
468
469
470
471
472 public void select(boolean primary) {
473
474 if (primary) {
475 firePropertyChange(SELECTED_PRIMARY, false, true);
476 } else {
477 firePropertyChange(SELECTED, false, true);
478 }
479 }
480
481
482
483
484
485
486 @Override
487 public AttributeManager getAttributeManager() {
488 return attributeManager;
489 }
490
491 public static void setSuppressLocationChangeEvents(boolean suppressLocationChangeEvents) {
492 Node.suppressLocationChangeEvents = suppressLocationChangeEvents;
493 }
494
495 public static boolean isSuppressLocationChangeEvents() {
496 return suppressLocationChangeEvents;
497 }
498
499 }