View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
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 }