KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > taglib > TreeControl


1 /*
2  * $Header: /cvsroot/jonas/jonas/src/org/objectweb/jonas/webapp/taglib/TreeControl.java,v 1.1 2003/10/16 12:17:28 antonma Exp $
3  * $Revision: 1.1 $
4  * $Date: 2003/10/16 12:17:28 $
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 package org.objectweb.jonas.webapp.taglib;
63
64 import java.io.Serializable JavaDoc;
65 import java.util.HashMap JavaDoc;
66
67 /**
68  * <p>The overall data structure representing a <em>tree control</em>
69  * that can be rendered by the <code>TreeControlTag</code> custom tag.
70  * Each node of the tree is represented by an instance of
71  * <code>TreeControlNode</code>.</p>
72  *
73  * @author Jazmin Jonson
74  * @author Craig R. McClanahan
75  * @version $Revision: 1.4
76  */

77
78 public class TreeControl implements Serializable JavaDoc {
79     public static final String JavaDoc ID_PREFIX = "treenode";
80
81 // ----------------------------------------------------------- Constructors
82

83     /**
84      * Construct a new instance with no predefined root node.
85      */

86     public TreeControl() {
87         super();
88         setRoot(null);
89     }
90
91     /**
92      * Construct a new instance with the specified root node.
93      *
94      * @param root The new root node
95      */

96     public TreeControl(TreeControlNode root) {
97         super();
98         setRoot(root);
99     }
100
101 // ----------------------------------------------------- Instance Variables
102

103     /**
104      * The collection of nodes that represent this tree, keyed by name.
105      */

106     protected HashMap JavaDoc registry = new HashMap JavaDoc();
107
108     /**
109      * The most recently selected node.
110      */

111     protected TreeControlNode selected = null;
112
113     // Id
114
protected int mi_Id = 0;
115
116 // ------------------------------------------------------------- Properties
117

118     /**
119      * The root node of the entire tree.
120      */

121     protected TreeControlNode root = null;
122
123     public TreeControlNode getRoot() {
124         return (this.root);
125     }
126
127     protected void setRoot(TreeControlNode root) {
128         if (this.root != null) {
129             removeNode(this.root);
130         }
131         if (root != null) {
132             addNode(root);
133         }
134         root.setLast(true);
135         this.root = root;
136     }
137
138     /**
139      * The current displayable "width" of this tree (that is, the maximum
140      * depth of the visible part of the tree).
141      */

142     public int getWidth() {
143         if (root == null) {
144             return (0);
145         }
146         else {
147             return (getWidth(root));
148         }
149     }
150
151 // --------------------------------------------------------- Public Methods
152

153     /**
154      * Find and return the <code>TreeControlNode</code> for the specified
155      * node name, if it exists; otherwise, return <code>null</code>.
156      *
157      * @param name Name of the <code>TreeControlNode</code> to be returned
158      */

159     public TreeControlNode findNode(String JavaDoc name) {
160         synchronized (registry) {
161             return ((TreeControlNode) registry.get(name));
162         }
163     }
164
165     /**
166      * Mark the specified node as the one-and-only currently selected one,
167      * deselecting any previous node that was so marked.
168      *
169      * @param node Name of the node to mark as selected, or <code>null</code>
170      * if there should be no currently selected node
171      */

172     public void selectNode(String JavaDoc name) {
173         if (selected != null) {
174             selected.setSelected(false);
175             selected = null;
176         }
177         selected = findNode(name);
178         if (selected != null) {
179             selected.setSelected(true);
180         }
181     }
182
183     /**
184      * Get the last node selected.
185      *
186      * @return the current node selected
187      */

188     public TreeControlNode getSelected() {
189         return selected;
190     }
191
192     /**
193      * Expand a branch in the tree of the selected node.
194      */

195     public void expandSelectedParents() {
196         TreeControlNode oCur = getSelected();
197         while (oCur != null){
198             oCur = oCur.getParent();
199             if (oCur != null) {
200                 oCur.setExpanded(true);
201             }
202         }
203     }
204
205     public String JavaDoc newId() {
206         StringBuffer JavaDoc sbRet = new StringBuffer JavaDoc(ID_PREFIX);
207         sbRet.append(mi_Id);
208         mi_Id++;
209         return sbRet.toString();
210     }
211
212 // -------------------------------------------------------- Package Methods
213

214     /**
215      * Register the specified node in our registry of the complete tree.
216      *
217      * @param node The <code>TreeControlNode</code> to be registered
218      *
219      * @exception IllegalArgumentException if the name of this node
220      * is not unique
221      */

222     void addNode(TreeControlNode node)
223         throws IllegalArgumentException JavaDoc {
224         synchronized (registry) {
225             String JavaDoc name = node.getName();
226             if (registry.containsKey(name)) {
227                 throw new IllegalArgumentException JavaDoc("Name '" + name + "' is not unique");
228             }
229             node.setTree(this);
230             registry.put(name, node);
231             // Refresh expand info
232
autoRefresh(node);
233         }
234     }
235
236     /**
237      * Calculate the width of the subtree below the specified node.
238      *
239      * @param node The node for which to calculate the width
240      */

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

263     void removeNode(TreeControlNode node) {
264         synchronized (registry) {
265             TreeControlNode children[] = node.findChildren();
266             for (int i = 0; i < children.length; i++) {
267                 removeNode(children[i]);
268             }
269             TreeControlNode parent = node.getParent();
270             if (parent != null) {
271                 parent.removeChild(node);
272             }
273             node.setParent(null);
274             node.setTree(null);
275             if (node == this.root) {
276                 this.root = null;
277             }
278             registry.remove(node.getName());
279             // Save removed node in list
280
addRemovedList(node);
281         }
282     }
283
284     /**
285      * List to save removed node used by the auto-refresh mode.
286      *
287      */

288     private HashMap JavaDoc m_RemovedList = null;
289
290     /**
291      * Disable auto-refresh mode.
292      */

293     public void disableAutoRefresh() {
294         if (m_RemovedList != null) {
295             m_RemovedList.clear();
296         }
297         m_RemovedList = null;
298     }
299
300     /**
301      * Enable auto-refresh mode.
302      * When a set of children are refreshed (removed then added),
303      * the expanded info is copied of the removed node to the added node.
304      * The name is used to retreive the good node.
305      * By default, the auto-refresh mode is disabled.
306      * Be careful, enabled this mode before the remove of all nodes and
307      * disabled it after the add.
308      */

309     public void enableAutoRefresh() {
310         m_RemovedList = new HashMap JavaDoc();
311     }
312
313     /**
314      * Add the removed node in the removed list.
315      *
316      * @param p_RemovedNode The removed node
317      */

318     void addRemovedList(TreeControlNode p_RemovedNode) {
319         if (m_RemovedList != null) {
320             m_RemovedList.put(p_RemovedNode.getName(), p_RemovedNode);
321         }
322     }
323
324     /**
325      * Search the added node in the removed list and if it's found, copy the expanded info.
326      *
327      * @param p_AddedNode The added node
328      */

329     protected void autoRefresh(TreeControlNode p_AddedNode) {
330         if (m_RemovedList != null) {
331             TreeControlNode oRemove = (TreeControlNode) m_RemovedList.get(p_AddedNode.getName());
332             if (oRemove != null) {
333                 p_AddedNode.setExpanded(oRemove.isExpanded());
334             }
335         }
336     }
337 }
338
Popular Tags