1 package org.kite9.diagram.primitives;
2
3 import java.util.Collection;
4 import java.util.LinkedHashSet;
5
6 import org.kite9.diagram.position.RenderingInformation;
7 import org.kite9.framework.logging.LogicException;
8
9 import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
10 import com.thoughtworks.xstream.annotations.XStreamOmitField;
11
12 /***
13 * This is the base class for most {@link Connected} elements within the diagram.
14 * e.g. Arrows, Glyphs, Contexts.
15 *
16 * @author robmoffat
17 *
18 */
19 public abstract class AbstractConnectedContained implements Connected, Contained {
20
21 public AbstractConnectedContained() {
22 super();
23 }
24
25 public AbstractConnectedContained(String id) {
26 if (id==null) {
27 throw new LogicException("Cannot create diagram element with null id");
28 }
29 this.id = id;
30 }
31
32 private static final long serialVersionUID = 8856547625295452633L;
33
34 private LinkedHashSet<Connection> links = new LinkedHashSet<Connection>();
35
36 public void addLink(Connection l) {
37 links.add(l);
38 }
39
40 @XStreamOmitField
41 Container c;
42
43 public Container getContainer() {
44 return c;
45 }
46
47 public void setContainer(Container c) {
48 this.c = c;
49 }
50
51 public Collection<Connection> getLinks() {
52 return links;
53 }
54
55 public void removeLink(Connection l) {
56 links.remove(l);
57 }
58
59 public int compareTo(DiagramElement o) {
60 if (o instanceof IdentifiableDiagramElement) {
61 return id.compareTo(((IdentifiableDiagramElement)o).getID());
62 } else if (o!=null) {
63 return this.toString().compareTo(o.toString());
64 } else {
65 return -1;
66 }
67 }
68
69 @XStreamAsAttribute
70 private String id;
71
72
73 @Override
74 public int hashCode() {
75
76
77 if (id==null) {
78 throw new LogicException("This diagram element should have an id: "+this);
79 }
80 return id.hashCode();
81 }
82
83 public final String getID() {
84 return id;
85 }
86
87 public void setID(String id) {
88 this.id = id;
89 }
90
91 /***
92 * This is used by layout engines to set the position of the elements in the
93 * diagram
94 */
95 protected RenderingInformation renderingInformation = null;
96
97 public boolean isConnectedDirectlyTo(Connected c) {
98 for (Connection link : links) {
99 if (link.meets(c)) {
100 return true;
101 }
102 }
103
104 return false;
105 }
106
107 private static int counter = 0;
108
109 protected static String createID() {
110 return "auto:"+counter++;
111 }
112
113 }