KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > webapp > model > TreeNode


1 /* NeXt:recursion tutorial
2  * next@keyboardmonkey.com
3  */

4 package org.oddjob.webapp.model;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.oddjob.Structural;
10 import org.oddjob.monitor.model.ExplorerContext;
11 import org.oddjob.monitor.model.ExplorerModel;
12 import org.oddjob.structural.StructuralEvent;
13 import org.oddjob.structural.StructuralListener;
14
15 /**
16  * A model for a node in the job tree.
17  *
18  * @author Rob Gordon.
19  */

20 class TreeNode
21         implements StructuralListener {
22
23     /** Used to create a unique reference number for each node. */
24     private static int lastRefId = 0;
25
26     /** Reference Id */
27     private final String JavaDoc refId;
28
29     /** The originating object */
30     private final Object JavaDoc node;
31
32     /** And keep track of children. */
33     private final List JavaDoc/*<String>*/ children = new ArrayList JavaDoc();
34
35     /** So we can add and remove children. */
36     private final JobInfoLookup lookup;
37     
38     private final ExplorerContext explorerContext;
39     
40     /**
41      * Constructor for the top level node.
42      *
43      * @param explorerModel The ExplorerModel.
44      * @param lookup The lookup to add children to.
45      */

46     TreeNode(ExplorerModel explorerModel, JobInfoLookup lookup) {
47         this.node = explorerModel.getRoot();
48         this.lookup = lookup;
49         synchronized (TreeNode.class) {
50             this.refId = Integer.toString(lastRefId++);
51         }
52         explorerContext = new ExplorerContext(explorerModel);
53     }
54
55     /**
56      * Constructor for child nodes.
57      *
58      * @param o The job.
59      * @param lookup The lookup to add children to.
60      */

61     TreeNode(TreeNode parent, Object JavaDoc node) {
62         this.node = node;
63         this.lookup = parent.lookup;
64         synchronized (TreeNode.class) {
65             this.refId = Integer.toString(lastRefId++);
66         }
67         explorerContext = new ExplorerContext(node, parent.explorerContext);
68     }
69     
70     /**
71      * Get the underlying job for this node.
72      *
73      * @return The job.
74      */

75     Object JavaDoc getComponent() {
76         return node;
77     }
78     
79     /**
80      * Get the refIds of any children.
81      *
82      * @return A String array of child refIds.
83      */

84     String JavaDoc[] getChildRefIds() {
85         return (String JavaDoc[]) this.children.toArray(new String JavaDoc[0]);
86     }
87
88     /**
89      * Get the refId of this node.
90      *
91      * @return The refId.
92      */

93     String JavaDoc getRefId() {
94         return refId;
95     }
96     
97     /**
98      * Get the node name for this node.
99      *
100      * @return The node name.
101      */

102     String JavaDoc getNodeName() {
103         return node.toString();
104     }
105
106     /*
107      * (non-Javadoc)
108      * @see org.oddjob.structural.StructuralListener#childAdded(org.oddjob.structural.StructuralEvent)
109      */

110     public void childAdded(StructuralEvent event) {
111
112         Object JavaDoc child = event.getChild();
113         int index = event.getIndex();
114         TreeNode childNode = new TreeNode(this, child);
115         if (child instanceof Structural) {
116             ((Structural) child).addStructuralListener(childNode);
117         }
118         
119         synchronized (children) {
120             children.add(index, childNode.refId);
121         }
122         lookup.addChild(childNode);
123     }
124
125     /*
126      * (non-Javadoc)
127      * @see org.oddjob.structural.StructuralListener#childRemoved(org.oddjob.structural.StructuralEvent)
128      */

129     public void childRemoved(StructuralEvent event) {
130         int index = event.getIndex();
131         String JavaDoc childRefId = null;
132         synchronized (children) {
133             childRefId = (String JavaDoc) children.remove(index);
134         }
135
136         lookup.removeChild(childRefId);
137     }
138     
139     /**
140      * @return Returns the context.
141      */

142     public ExplorerContext getExplorerContext() {
143         return explorerContext;
144     }
145     
146 }
Popular Tags