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.HashSet;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.Set;
24
25 import pl.edu.agh.cast.common.HexColor;
26 import pl.edu.agh.cast.model.attributes.AttributeManager;
27 import pl.edu.agh.cast.model.attributes.AttributeValue;
28 import pl.edu.agh.cast.model.attributes.ConnectionGroupAttributeManager;
29
30
31
32
33
34
35 public class ConnectionGroup extends ModelElement {
36
37
38
39
40
41
42 private static final long serialVersionUID = -3863944666693937115L;
43
44
45
46
47
48
49 public static final String CONNECTION_COUNT_CHANGED = "ConnectionGroup_ConnCountChanged";
50
51
52
53
54 public static final String SINGLE_CONNECTION_LABEL = "ConnectionGroup_SingleLineLabel";
55
56
57
58
59 public static final String DOUBLE_CONNECTION_SOURCE_LABEL = "ConnectionGroup_DoubleLineSourceToTargetLabel";
60
61
62
63
64 public static final String DOUBLE_CONNECTION_TARGET_LABEL = "ConnectionGroup_DoubleLineTargetToSourceLabel";
65
66
67
68
69 public static final String VARIABLE_THICKNESS_CONNECTION_LABEL = "ConnectionGroup_VariableThicknessLineLabel";
70
71
72
73
74 public static final String LINE_THICKNESS = "ConnectionGroup_LineThickness";
75
76
77
78
79 public static final String LINE_COLOR = "ConnectionGroup_LineColor";
80
81
82
83 private static final String NO_SUCH_NODE_MESSAGE = "Node does not belong to this connection group";
84
85 private Node left;
86
87 private Node right;
88
89
90
91
92 private List<Connection> leftConnections = new LinkedList<Connection>();
93
94
95
96
97 private List<Connection> rightConnections = new LinkedList<Connection>();
98
99 private ConnectionGroupAttributeManager attributeManager;
100
101
102
103
104
105
106
107
108
109
110
111 public ConnectionGroup(Node left, Node right, ConnectionGroupAttributeManager attributeManager) {
112 super();
113 setSource(left);
114 setTarget(right);
115 this.attributeManager = attributeManager;
116
117 bindToAttributeManager();
118 setMandatoryProperties();
119 }
120
121 private void setMandatoryProperties() {
122 setAttributeValue(SINGLE_CONNECTION_LABEL, Integer.toString(leftConnections.size() + rightConnections.size()));
123 setAttributeValue(DOUBLE_CONNECTION_SOURCE_LABEL, Integer.toString(leftConnections.size()));
124 setAttributeValue(DOUBLE_CONNECTION_TARGET_LABEL, Integer.toString(rightConnections.size()));
125 setAttributeValue(LINE_THICKNESS, 1);
126 setAttributeValue(LINE_COLOR, new HexColor(0, 0, 0));
127 }
128
129 public int getConnectionCount() {
130 return leftConnections.size() + rightConnections.size();
131 }
132
133
134
135
136
137
138
139
140 public int getTargetConnectionCountFor(Node node) {
141 if (right.equals(node)) {
142 return leftConnections.size();
143 } else if (left.equals(node)) {
144 return rightConnections.size();
145 }
146 throw new IllegalArgumentException(NO_SUCH_NODE_MESSAGE);
147 }
148
149
150
151
152
153
154
155
156 public int getSourceConnectionCountFor(Node node) {
157 if (left.equals(node)) {
158 return leftConnections.size();
159 } else if (right.equals(node)) {
160 return rightConnections.size();
161 }
162 throw new IllegalArgumentException(NO_SUCH_NODE_MESSAGE);
163 }
164
165
166
167
168
169
170
171
172 @Deprecated
173 public List<Connection> getLeftConnections() {
174 return leftConnections;
175 }
176
177
178
179
180
181
182
183
184 @Deprecated
185 public List<Connection> getRightConnections() {
186 return rightConnections;
187 }
188
189
190
191
192
193
194
195
196 public List<Connection> getSourceConnectionsFor(Node node) {
197 if (left.equals(node)) {
198 return leftConnections;
199 } else if (right.equals(node)) {
200 return rightConnections;
201 }
202 throw new IllegalArgumentException(NO_SUCH_NODE_MESSAGE);
203
204 }
205
206
207
208
209
210
211
212
213 public List<Connection> getTargetConnectionsFor(Node node) {
214 if (right.equals(node)) {
215 return leftConnections;
216 } else if (left.equals(node)) {
217 return rightConnections;
218 }
219 throw new IllegalArgumentException(NO_SUCH_NODE_MESSAGE);
220
221 }
222
223
224
225
226
227
228 public List<Connection> getAllConnections() {
229 Set<Connection> connections = new HashSet<Connection>();
230 connections.addAll(leftConnections);
231 connections.addAll(rightConnections);
232 List<Connection> result = new LinkedList<Connection>();
233 result.addAll(connections);
234 return result;
235 }
236
237 public Node getSource() {
238 return left;
239 }
240
241
242
243
244
245
246
247
248
249 public void setSource(Node node) {
250 if (node == null) {
251 throw new IllegalArgumentException("source may not be null");
252 }
253 left = node;
254 }
255
256 public Node getTarget() {
257 return right;
258 }
259
260
261
262
263
264
265
266
267
268 public void setTarget(Node node) {
269 if (node == null) {
270 throw new IllegalArgumentException("target may not be null");
271 }
272 right = node;
273 }
274
275
276
277
278
279
280
281
282
283
284 private void updateProperty(String name, int newValue) {
285 AttributeValue property = getAttributeValue(name);
286
287 try {
288 Object value = property.getValue();
289 if (value instanceof String) {
290 Integer.parseInt((String)value);
291 }
292
293 setAttributeValue(name, Integer.toString(newValue));
294 } catch (NumberFormatException ex) {
295
296 }
297 }
298
299 private void updateProperties() {
300
301 updateProperty(SINGLE_CONNECTION_LABEL, leftConnections.size() + rightConnections.size());
302 updateProperty(DOUBLE_CONNECTION_SOURCE_LABEL, leftConnections.size());
303 updateProperty(DOUBLE_CONNECTION_TARGET_LABEL, rightConnections.size());
304 }
305
306
307
308
309
310
311
312 public void addLeftConnection(Connection connection) {
313 int oldSourceConnectionCount = leftConnections.size();
314 leftConnections.add(connection);
315
316 updateProperties();
317
318 firePropertyChange(CONNECTION_COUNT_CHANGED, oldSourceConnectionCount, leftConnections.size());
319 }
320
321
322
323
324
325
326
327 public void addRightConnection(Connection connection) {
328 int oldTargetConnectionCount = rightConnections.size();
329 rightConnections.add(connection);
330
331 updateProperties();
332
333 firePropertyChange(CONNECTION_COUNT_CHANGED, oldTargetConnectionCount, rightConnections.size());
334 }
335
336
337
338
339
340
341
342 public void removeLeftConnection(Connection connection) {
343 if (!leftConnections.contains(connection)) {
344 return;
345 }
346 int oldSourceConnectionCount = leftConnections.size();
347 leftConnections.remove(connection);
348
349 updateProperties();
350
351 firePropertyChange(CONNECTION_COUNT_CHANGED, oldSourceConnectionCount, leftConnections.size());
352 }
353
354
355
356
357
358
359
360 public void removeRightConnection(Connection connection) {
361 if (!rightConnections.contains(connection)) {
362 return;
363 }
364 int oldTargetConnectionCount = rightConnections.size();
365 rightConnections.remove(connection);
366
367 updateProperties();
368
369 firePropertyChange(CONNECTION_COUNT_CHANGED, oldTargetConnectionCount, rightConnections.size());
370 }
371
372
373
374
375 public void addConnectionGroupToNodes() {
376 left.addConnectionGroup(this);
377 right.addConnectionGroup(this);
378 }
379
380
381
382
383 public void removeConnectionGroupFromNodes() {
384 left.removeConnectionGroup(this);
385 right.removeConnectionGroup(this);
386 }
387
388
389
390
391
392
393
394
395
396
397 public boolean isBetween(Node node1, Node node2) {
398 return (node1.equals(left) && node2.equals(right)) || (node1.equals(right) && node2.equals(left));
399 }
400
401
402
403
404
405
406
407 public void addConnection(Connection connection) {
408 if (connection.getSourceNode().equals(left) || connection.getSourceNode().equals(right)) {
409 if (connection.getSourceNode().equals(left)) {
410 addLeftConnection(connection);
411 }
412 if (connection.getSourceNode().equals(right)) {
413 addRightConnection(connection);
414 }
415 } else {
416 throw new IllegalArgumentException(NO_SUCH_NODE_MESSAGE);
417 }
418 }
419
420
421
422
423
424
425
426 public void removeConnection(Connection connection) {
427 if (connection.getSourceNode().equals(left)) {
428 removeLeftConnection(connection);
429 } else if (connection.getSourceNode().equals(right)) {
430 removeRightConnection(connection);
431 } else {
432 throw new IllegalArgumentException(NO_SUCH_NODE_MESSAGE);
433 }
434 }
435
436
437
438
439
440
441 @Override
442 public String toString() {
443 return "CG " + left.getLabel() + ":" + right.getLabel();
444 }
445
446 public ConnectionGroupAttributeManager getPropertyManager() {
447 return attributeManager;
448 }
449
450
451
452
453
454
455
456
457 @Override
458 public boolean equals(Object obj) {
459 if (obj == this) {
460 return true;
461 }
462 if (!(obj instanceof ConnectionGroup)) {
463 return false;
464 }
465 ConnectionGroup that = (ConnectionGroup)obj;
466
467 if (!this.left.equals(that.left)) {
468 return false;
469 }
470 if (!this.right.equals(that.right)) {
471 return false;
472 }
473
474 return true;
475 }
476
477
478
479
480
481
482 @Override
483 public int hashCode() {
484 return (left.getId() + right.getId()).hashCode();
485 }
486
487
488
489
490
491
492
493 public void copyTo(ConnectionGroup that) {
494
495 that.attributeManager = attributeManager;
496 copyAttributes(that);
497 }
498
499
500
501
502
503
504 @Override
505 public AttributeManager getAttributeManager() {
506 return attributeManager;
507 }
508 }