KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/jonas/jonas/src/org/objectweb/jonas/webapp/taglib/TreeControlNode.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.ArrayList JavaDoc;
66
67
68 /**
69  * <p>An individual node of a tree control represented by an instance of
70  * <code>TreeControl</code>, and rendered by an instance of
71  * <code>TreeControlTag</code>.</p>
72  *
73  * @author Jazmin Jonson
74  * @author Craig R. McClanahan
75  * @version $Revision: 1.1 $
76  */

77
78 public class TreeControlNode implements Serializable JavaDoc {
79
80 // ----------------------------------------------------------- Constructors
81

82     /**
83      * Construct a new TreeControlNode with the specified parameters.
84      *
85      * @param name Internal name of this node (must be unique within
86      * the entire tree)
87      * @param icon Pathname of the image file for the icon to be displayed
88      * when this node is visible, relative to the image directory
89      * for our images
90      * @param label The label that will be displayed to the user if
91      * this node is visible
92      * @param action The hyperlink to be selected if the user
93      * selects this node, or <code>null</code> if this node's label should
94      * not be a hyperlink
95      * @param target The window target in which the <code>action</code>
96      * hyperlink's results will be displayed, or <code>null</code> for
97      * the current window
98      * @param expanded Should this node be expanded?
99      */

100     public TreeControlNode(String JavaDoc name, String JavaDoc icon, String JavaDoc label, String JavaDoc action, String JavaDoc target
101         , boolean expanded) {
102
103         super();
104         this.name = name;
105         this.icon = icon;
106         this.label = label;
107         this.action = action;
108         this.target = target;
109         this.expanded = expanded;
110
111     }
112
113     public TreeControlNode(TreeControlNode p_Node) {
114        super();
115        this.name = p_Node.getName();
116        this.icon = p_Node.getIcon();
117        this.label = p_Node.getLabel();
118        this.action = p_Node.getAction();
119        this.target = p_Node.getTarget();
120        this.expanded = p_Node.isExpanded();
121    }
122
123 // ----------------------------------------------------- Instance Variables
124

125     /**
126      * The set of child <code>TreeControlNodes</code> for this node, in the
127      * order that they should be displayed.
128      */

129     protected ArrayList JavaDoc children = new ArrayList JavaDoc();
130
131 // ------------------------------------------------------------- Properties
132

133     /**
134      * The hyperlink to which control will be directed if this node
135      * is selected by the user.
136      */

137     protected String JavaDoc action = null;
138
139     public String JavaDoc getAction() {
140         return (this.action);
141     }
142
143     /**
144      * Is this node currently expanded?
145      */

146     protected boolean expanded = false;
147
148     public boolean isExpanded() {
149         return (this.expanded);
150     }
151
152     public void setExpanded(boolean expanded) {
153         this.expanded = expanded;
154     }
155
156     /**
157      * The pathname to the icon file displayed when this node is visible,
158      * relative to the image directory for our images.
159      */

160     protected String JavaDoc icon = null;
161
162     public String JavaDoc getIcon() {
163         return (this.icon);
164     }
165
166     /**
167      * The label that will be displayed when this node is visible.
168      */

169     protected String JavaDoc label = null;
170
171     public String JavaDoc getLabel() {
172         return (this.label);
173     }
174
175     /**
176      * Is this the last node in the set of children for our parent node?
177      */

178     protected boolean last = false;
179
180     public boolean isLast() {
181         return (this.last);
182     }
183
184     void setLast(boolean last) {
185         this.last = last;
186     }
187
188     /**
189      * Is this a "leaf" node (i.e. one with no children)?
190      */

191     public boolean isLeaf() {
192         synchronized (children) {
193             return (children.size() < 1);
194         }
195     }
196
197     /**
198      * The unique (within the entire tree) name of this node.
199      */

200     protected String JavaDoc name = null;
201
202     public String JavaDoc getName() {
203         return (this.name);
204     }
205
206     /**
207      * The parent node of this node, or <code>null</code> if this
208      * is the root node.
209      */

210     protected TreeControlNode parent = null;
211
212     public TreeControlNode getParent() {
213         return (this.parent);
214     }
215
216     void setParent(TreeControlNode parent) {
217         this.parent = parent;
218         if (parent == null) {
219             width = 1;
220         }
221         else {
222             width = parent.getWidth() + 1;
223         }
224     }
225
226     /**
227      * Is this node currently selected?
228      */

229     protected boolean selected = false;
230
231     public boolean isSelected() {
232         return (this.selected);
233     }
234
235     public void setSelected(boolean selected) {
236         this.selected = selected;
237     }
238
239     /**
240      * The window target for the hyperlink identified by the
241      * <code>action</code> property, if this node is selected
242      * by the user.
243      */

244     protected String JavaDoc target = null;
245
246     public String JavaDoc getTarget() {
247         return (this.target);
248     }
249
250     /**
251      * The <code>TreeControl</code> instance representing the
252      * entire tree.
253      */

254     protected TreeControl tree = null;
255
256     public TreeControl getTree() {
257         return (this.tree);
258     }
259
260     void setTree(TreeControl tree) {
261         this.tree = tree;
262     }
263
264     /**
265      * The display width necessary to display this item (if it is visible).
266      * If this item is not visible, the calculated width will be that of our
267      * most immediately visible parent.
268      */

269     protected int width = 0;
270
271     public int getWidth() {
272         return (this.width);
273     }
274
275 // --------------------------------------------------------- Public Methods
276

277     /**
278      * Add a new child node to the end of the list.
279      *
280      * @param child The new child node
281      *
282      * @exception IllegalArgumentException if the name of the new child
283      * node is not unique
284      */

285     public void addChild(TreeControlNode child)
286         throws IllegalArgumentException JavaDoc {
287
288         tree.addNode(child);
289         child.setParent(this);
290         synchronized (children) {
291             int n = children.size();
292             if (n > 0) {
293                 TreeControlNode node = (TreeControlNode) children.get(n - 1);
294                 node.setLast(false);
295             }
296             child.setLast(true);
297             children.add(child);
298         }
299
300     }
301
302     /**
303      * Add a new child node at the specified position in the child list.
304      *
305      * @param offset Zero-relative offset at which the new node
306      * should be inserted
307      * @param child The new child node
308      *
309      * @exception IllegalArgumentException if the name of the new child
310      * node is not unique
311      */

312     public void addChild(int offset, TreeControlNode child)
313         throws IllegalArgumentException JavaDoc {
314
315         tree.addNode(child);
316         child.setParent(this);
317         synchronized (children) {
318             children.add(offset, child);
319         }
320
321     }
322
323     /**
324      * Return the set of child nodes for this node.
325      */

326     public TreeControlNode[] findChildren() {
327
328         synchronized (children) {
329             TreeControlNode results[] = new TreeControlNode[children.size()];
330             return ((TreeControlNode[]) children.toArray(results));
331         }
332
333     }
334
335     /**
336      * Remove this node from the tree.
337      */

338     public void remove() {
339
340         if (tree != null) {
341             tree.removeNode(this);
342         }
343
344     }
345
346     /**
347      * Remove the child node (and all children of that child) at the
348      * specified position in the child list.
349      *
350      * @param offset Zero-relative offset at which the existing
351      * node should be removed
352      */

353     public void removeChild(int offset) {
354
355         synchronized (children) {
356             TreeControlNode child = (TreeControlNode) children.get(offset);
357             tree.removeNode(child);
358             child.setParent(null);
359             children.remove(offset);
360         }
361
362     }
363
364
365     public String JavaDoc toString() {
366         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
367         sb.append(getName());
368         sb.append(" - ");
369         sb.append(isExpanded());
370
371         return sb.toString();
372     }
373
374 // -------------------------------------------------------- Package Methods
375

376     /**
377      * Remove the specified child node. It is assumed that all of the
378      * children of this child node have already been removed.
379      *
380      * @param child Child node to be removed
381      */

382     void removeChild(TreeControlNode child) {
383
384         if (child == null) {
385             return;
386         }
387         synchronized (children) {
388             int n = children.size();
389             for (int i = 0; i < n; i++) {
390                 if (child == (TreeControlNode) children.get(i)) {
391                     children.remove(i);
392                     return;
393                 }
394             }
395         }
396
397     }
398
399 }
400
Popular Tags