KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > TreeControl


1 /*
2  * Copyright 2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.webapp.admin;
19
20
21 import java.io.Serializable JavaDoc;
22 import java.util.HashMap JavaDoc;
23
24
25 /**
26  * <p>The overall data structure representing a <em>tree control</em>
27  * that can be rendered by the <code>TreeControlTag</code> custom tag.
28  * Each node of the tree is represented by an instance of
29  * <code>TreeControlNode</code>.</p>
30  *
31  * @author Jazmin Jonson
32  * @author Craig R. McClanahan
33  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:59:01 $
34  */

35
36 public class TreeControl implements Serializable JavaDoc {
37
38
39     // ----------------------------------------------------------- Constructors
40

41
42     /**
43      * Construct a new instance with no predefined root node.
44      */

45     public TreeControl() {
46
47         super();
48         setRoot(null);
49
50     }
51
52
53     /**
54      * Construct a new instance with the specified root node.
55      *
56      * @param root The new root node
57      */

58     public TreeControl(TreeControlNode root) {
59
60         super();
61         setRoot(root);
62
63     }
64
65
66     // ----------------------------------------------------- Instance Variables
67

68
69     /**
70      * The collection of nodes that represent this tree, keyed by name.
71      */

72     protected HashMap JavaDoc registry = new HashMap JavaDoc();
73
74
75     /**
76      * The most recently selected node.
77      */

78     protected TreeControlNode selected = null;
79
80
81     // ------------------------------------------------------------- Properties
82

83
84     /**
85      * The root node of the entire tree.
86      */

87     protected TreeControlNode root = null;
88
89     public TreeControlNode getRoot() {
90         return (this.root);
91     }
92
93     protected void setRoot(TreeControlNode root) {
94         if (this.root != null)
95             removeNode(this.root);
96         if (root != null)
97             addNode(root);
98         root.setLast(true);
99         this.root = root;
100     }
101
102
103     /**
104      * The current displayable "width" of this tree (that is, the maximum
105      * depth of the visible part of the tree).
106      */

107     public int getWidth() {
108
109         if (root == null)
110             return (0);
111         else
112             return (getWidth(root));
113
114     }
115
116
117     // --------------------------------------------------------- Public Methods
118

119
120     /**
121      * Find and return the <code>TreeControlNode</code> for the specified
122      * node name, if it exists; otherwise, return <code>null</code>.
123      *
124      * @param name Name of the <code>TreeControlNode</code> to be returned
125      */

126     public TreeControlNode findNode(String JavaDoc name) {
127
128         synchronized (registry) {
129             return ((TreeControlNode) registry.get(name));
130         }
131
132     }
133
134
135     /**
136      * Mark the specified node as the one-and-only currently selected one,
137      * deselecting any previous node that was so marked.
138      *
139      * @param node Name of the node to mark as selected, or <code>null</code>
140      * if there should be no currently selected node
141      */

142     public void selectNode(String JavaDoc name) {
143
144         if (selected != null) {
145             selected.setSelected(false);
146             selected = null;
147         }
148         selected = findNode(name);
149         if (selected != null)
150             selected.setSelected(true);
151
152     }
153
154
155     // -------------------------------------------------------- Package Methods
156

157
158     /**
159      * Register the specified node in our registry of the complete tree.
160      *
161      * @param node The <code>TreeControlNode</code> to be registered
162      *
163      * @exception IllegalArgumentException if the name of this node
164      * is not unique
165      */

166     void addNode(TreeControlNode node) throws IllegalArgumentException JavaDoc {
167
168         synchronized (registry) {
169             String JavaDoc name = node.getName();
170             if (registry.containsKey(name))
171                 throw new IllegalArgumentException JavaDoc("Name '" + name +
172                                                    "' is not unique");
173             node.setTree(this);
174             registry.put(name, node);
175         }
176
177     }
178
179
180     /**
181      * Calculate the width of the subtree below the specified node.
182      *
183      * @param node The node for which to calculate the width
184      */

185     int getWidth(TreeControlNode node) {
186
187         int width = node.getWidth();
188         if (!node.isExpanded())
189             return (width);
190         TreeControlNode children[] = node.findChildren();
191         for (int i = 0; i < children.length; i++) {
192             int current = getWidth(children[i]);
193             if (current > width)
194                 width = current;
195         }
196         return (width);
197
198     }
199
200
201     /**
202      * Deregister the specified node, as well as all child nodes of this
203      * node, from our registry of the complete tree. If this node is not
204      * present, no action is taken.
205      *
206      * @param node The <code>TreeControlNode</code> to be deregistered
207      */

208     void removeNode(TreeControlNode node) {
209
210         synchronized (registry) {
211             TreeControlNode children[] = node.findChildren();
212             for (int i = 0; i < children.length; i++)
213                 removeNode(children[i]);
214             TreeControlNode parent = node.getParent();
215             if (parent != null) {
216                 parent.removeChild(node);
217             }
218             node.setParent(null);
219             node.setTree(null);
220             if (node == this.root) {
221                 this.root = null;
222             }
223             registry.remove(node.getName());
224         }
225
226     }
227
228
229 }
230
Popular Tags