KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nextime > ion > backoffice > tree > TreeControl


1 /*
2  * $Header: /home/cvs/jakarta-tomcat-4.0/webapps/admin/WEB-INF/classes/org/apache/webapp/admin/TreeControl.java,v 1.3 2002/03/22 00:58:17 manveen Exp $
3  * $Revision: 1.3 $
4  * $Date: 2002/03/22 00:58:17 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 2001 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowlegement may appear in the software itself,
30  * if and wherever such third-party acknowlegements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Group.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  */

61
62
63 package org.nextime.ion.backoffice.tree;
64
65
66 import java.io.Serializable JavaDoc;
67 import java.util.HashMap JavaDoc;
68
69
70 /**
71  * <p>The overall data structure representing a <em>tree control</em>
72  * that can be rendered by the <code>TreeControlTag</code> custom tag.
73  * Each node of the tree is represented by an instance of
74  * <code>TreeControlNode</code>.</p>
75  *
76  * @author Jazmin Jonson
77  * @author Craig R. McClanahan
78  * @version $Revision: 1.3 $ $Date: 2002/03/22 00:58:17 $
79  */

80
81 public class TreeControl implements Serializable JavaDoc {
82
83
84     // ----------------------------------------------------------- Constructors
85

86
87     /**
88      * Construct a new instance with no predefined root node.
89      */

90     public TreeControl() {
91
92         super();
93         setRoot(null);
94
95     }
96
97
98     /**
99      * Construct a new instance with the specified root node.
100      *
101      * @param root The new root node
102      */

103     public TreeControl(TreeControlNode root) {
104
105         super();
106         setRoot(root);
107
108     }
109
110
111     // ----------------------------------------------------- Instance Variables
112

113
114     /**
115      * The collection of nodes that represent this tree, keyed by name.
116      */

117     protected HashMap JavaDoc registry = new HashMap JavaDoc();
118
119
120     /**
121      * The most recently selected node.
122      */

123     protected TreeControlNode selected = null;
124
125
126     // ------------------------------------------------------------- Properties
127

128
129     /**
130      * The root node of the entire tree.
131      */

132     protected TreeControlNode root = null;
133
134     public TreeControlNode getRoot() {
135         return (this.root);
136     }
137
138     protected void setRoot(TreeControlNode root) {
139         if (this.root != null)
140             removeNode(this.root);
141         if (root != null)
142             addNode(root);
143         root.setLast(true);
144         this.root = root;
145     }
146
147
148     /**
149      * The current displayable "width" of this tree (that is, the maximum
150      * depth of the visible part of the tree).
151      */

152     public int getWidth() {
153
154         if (root == null)
155             return (0);
156         else
157             return (getWidth(root));
158
159     }
160
161
162     // --------------------------------------------------------- Public Methods
163

164
165     /**
166      * Find and return the <code>TreeControlNode</code> for the specified
167      * node name, if it exists; otherwise, return <code>null</code>.
168      *
169      * @param name Name of the <code>TreeControlNode</code> to be returned
170      */

171     public TreeControlNode findNode(String JavaDoc name) {
172
173         synchronized (registry) {
174             return ((TreeControlNode) registry.get(name));
175         }
176
177     }
178     
179     public TreeControlNode getSelected() {
180             return selected;
181     }
182
183
184     /**
185      * Mark the specified node as the one-and-only currently selected one,
186      * deselecting any previous node that was so marked.
187      *
188      * @param node Name of the node to mark as selected, or <code>null</code>
189      * if there should be no currently selected node
190      */

191     public void selectNode(String JavaDoc name) {
192
193         if (selected != null) {
194             selected.setSelected(false);
195             selected = null;
196         }
197         selected = findNode(name);
198         if (selected != null)
199             selected.setSelected(true);
200
201     }
202
203
204     // -------------------------------------------------------- Package Methods
205

206
207     /**
208      * Register the specified node in our registry of the complete tree.
209      *
210      * @param node The <code>TreeControlNode</code> to be registered
211      *
212      * @exception IllegalArgumentException if the name of this node
213      * is not unique
214      */

215     void addNode(TreeControlNode node) throws IllegalArgumentException JavaDoc {
216
217         synchronized (registry) {
218             String JavaDoc name = node.getName();
219             if (registry.containsKey(name))
220                 throw new IllegalArgumentException JavaDoc("Name '" + name +
221                                                    "' is not unique");
222             node.setTree(this);
223             registry.put(name, node);
224         }
225
226     }
227
228
229     /**
230      * Calculate the width of the subtree below the specified node.
231      *
232      * @param node The node for which to calculate the width
233      */

234     int getWidth(TreeControlNode node) {
235
236         int width = node.getWidth();
237         if (!node.isExpanded())
238             return (width);
239         TreeControlNode children[] = node.findChildren();
240         for (int i = 0; i < children.length; i++) {
241             int current = getWidth(children[i]);
242             if (current > width)
243                 width = current;
244         }
245         return (width);
246
247     }
248
249
250     /**
251      * Deregister the specified node, as well as all child nodes of this
252      * node, from our registry of the complete tree. If this node is not
253      * present, no action is taken.
254      *
255      * @param node The <code>TreeControlNode</code> to be deregistered
256      */

257     void removeNode(TreeControlNode node) {
258
259         synchronized (registry) {
260             TreeControlNode children[] = node.findChildren();
261             for (int i = 0; i < children.length; i++)
262                 removeNode(children[i]);
263             TreeControlNode parent = node.getParent();
264             if (parent != null) {
265                 parent.removeChild(node);
266             }
267             node.setParent(null);
268             node.setTree(null);
269             if (node == this.root) {
270                 this.root = null;
271             }
272             registry.remove(node.getName());
273         }
274
275     }
276
277
278 }
279
Popular Tags