1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package zeus.gui.graph;
25
26 import java.util.*;
27 import zeus.util.*;
28
29 public class GraphNode {
30 public static final int PARENT = 0;
31 public static final int CHILD = 1;
32
33 protected Vector children = new Vector();
34 protected Vector parents = new Vector();
35 protected Vector siblings = new Vector();
36 protected int nodeType = PARENT;
37 protected Object user_object;
38
39 public GraphNode(Object user_object) {
40 this.user_object = user_object;
41 }
42
43 public void setUserObject(Object user_object) {
44 this.user_object = user_object;
45 }
46
47 public Object getUserObject() {
48 return user_object;
49 }
50
51 public void setNodeType(int type) {
52 Assert.notFalse( type == PARENT || type == CHILD );
53 nodeType = type;
54 }
55
56 public int getNodeType() {
57 return nodeType;
58 }
59
60 public void initialize() {
61 children.removeAllElements();
62 parents.removeAllElements();
63 siblings.removeAllElements();
64 nodeType = PARENT;
65 }
66
67 protected boolean addItem(GraphNode node, Vector nodeList) {
68 if ( nodeList.contains(node) )
69 return false;
70 nodeList.addElement(node);
71 return true;
72 }
73
74 public void addSibling(GraphNode node) {
75 addItem(node,siblings);
76 }
77 public void addChild(GraphNode node) {
78 addItem(node,children);
79 }
80 public void addParent(GraphNode node) {
81 if ( addItem(node,parents) )
82 nodeType = CHILD;
83 }
84 public boolean hasSibling(GraphNode node) {
85 return siblings.contains(node);
86 }
87 public boolean hasChild(GraphNode node) {
88 return children.contains(node);
89 }
90 public boolean hasParent(GraphNode node) {
91 return parents.contains(node);
92 }
93 public void removeSibling(GraphNode node) {
94 siblings.removeElement(node);
95 }
96 public void removeChild(GraphNode node) {
97 children.removeElement(node);
98 }
99 public void removeParent(GraphNode node) {
100 parents.removeElement(node);
101 if ( parents.isEmpty() )
102 nodeType = PARENT;
103 }
104 public GraphNode[] getChildren() {
105 GraphNode[] out = new GraphNode[children.size()];
106 for(int i = 0; i < children.size(); i++ )
107 out[i] = (GraphNode)children.elementAt(i);
108 return out;
109 }
110 public GraphNode[] getSiblings() {
111 GraphNode[] out = new GraphNode[siblings.size()];
112 for(int i = 0; i < siblings.size(); i++ )
113 out[i] = (GraphNode)siblings.elementAt(i);
114 return out;
115 }
116 public GraphNode[] getParents() {
117 GraphNode[] out = new GraphNode[parents.size()];
118 for(int i = 0; i < parents.size(); i++ )
119 out[i] = (GraphNode)parents.elementAt(i);
120 return out;
121 }
122 }