KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > icefaces > samples > showcase > components > tree > TreeBean


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.icefaces.samples.showcase.components.tree;
35
36 import javax.faces.event.ActionEvent;
37 import javax.faces.event.ValueChangeEvent;
38 import javax.faces.model.SelectItem;
39 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
40 import javax.swing.tree.DefaultTreeModel JavaDoc;
41 import java.util.Enumeration JavaDoc;
42
43 /**
44  * <p>The TreeBean class is the backing bean for the Tree Component showcase
45  * demonstration. It is used to store and display the selected tree node</p>
46  *
47  * @see NodeUserObject
48  * @since 0.3.0
49  */

50 public class TreeBean {
51
52     // tree default model, used as a value for the tree component
53
private DefaultTreeModel JavaDoc model;
54     private DefaultMutableTreeNode JavaDoc rootTreeNode;
55
56     // label count increases one for each new node
57
private int labelCount = 0;
58
59     // selected node name
60
private String JavaDoc selectedNode = "";
61
62     // object reference used to delete and copy the node
63
private NodeUserObject selectedNodeObject = null;
64
65     // list of components the user can add to the tree
66
private SelectItem[] componentList = {
67             new SelectItem(new Integer JavaDoc(1), "outputText"),
68             new SelectItem(new Integer JavaDoc(2), "inputText"),
69             new SelectItem(new Integer JavaDoc(3), "commandButton")
70     };
71
72     // backing for the drop down of components
73
private Integer JavaDoc componentToAdd = new Integer JavaDoc(1);
74
75     /**
76      * Gets the component list.
77      *
78      * @return the component list
79      */

80     public SelectItem[] getComponentList() {
81         return componentList;
82     }
83
84     /**
85      * Sets the component list.
86      *
87      * @param componentList the new component list
88      */

89     public void setComponentList(SelectItem[] componentList) {
90         this.componentList = componentList;
91     }
92
93     /**
94      * Gets the component to add index.
95      *
96      * @return the component to add index
97      */

98     public Integer JavaDoc getComponentToAdd() {
99         return componentToAdd;
100     }
101
102     /**
103      * Sets the component to add index.
104      *
105      * @param componentToAdd the new component to add index
106      */

107     public void setComponentToAdd(Integer JavaDoc componentToAdd) {
108         this.componentToAdd = componentToAdd;
109     }
110
111     /**
112      * Gets the tree node.
113      *
114      * @return the tree node
115      */

116     public NodeUserObject getSelectedNodeObject() {
117         return selectedNodeObject;
118     }
119
120     /**
121      * Sets the tree node.
122      *
123      * @param selectedNodeObject the new tree node
124      */

125     public void setSelectedNodeObject(NodeUserObject selectedNodeObject) {
126         this.selectedNodeObject = selectedNodeObject;
127         componentToAdd = selectedNodeObject.getComponentType();
128     }
129
130     /**
131      * Deletes the selected tree node. The node object reference is set to null
132      * so that the delete and copy buttons will be disabled.
133      *
134      * @param event that fired this method
135      * @see #isDeleteDisabled(), isCopyDisabled()
136      */

137     public void deleteSelectedNode(ActionEvent event) {
138         // can't delete the root node; this check is a failsafe in case
139
// the delete method is somehow activated despite the button being disabled
140
if (selectedNodeObject != null && !selectedNode.equals("Node 1")) {
141
142             // get the node we want to delete
143
NodeUserObject nodeToDelete = selectedNodeObject;
144
145             // update the selected state, try to set it to parent of deleted
146
// node if possible.
147
if (selectedNodeObject.getWrapper().getParent() != null){
148                 DefaultMutableTreeNode JavaDoc node =
149                         (DefaultMutableTreeNode JavaDoc)selectedNodeObject.getWrapper().getParent();
150                 selectedNodeObject = (NodeUserObject)node.getUserObject();
151                 selectedNode = selectedNodeObject.getLabel();
152             }else{
153                 selectedNode = "";
154                 selectedNodeObject = null;
155             }
156
157             // delete the node.
158
nodeToDelete.deleteNode(event);
159             // update the icons for the newly changes state.
160
updateNodeIcon(selectedNodeObject);
161         }
162
163
164     }
165     
166     /**
167      * changes the value for the selected node, this method is activated
168      * when the user changed the information for a input text box.
169      *
170      *@param the ValueChangeEvent which holds the value
171      *
172      */

173     public void changedValue(ValueChangeEvent vce) {
174         if(selectedNodeObject != null) {
175             Object JavaDoc nvalue = vce.getNewValue();
176             if(nvalue instanceof String JavaDoc) {
177                 selectedNodeObject.setValue((String JavaDoc)nvalue);
178                 
179             }
180         }
181     }
182     
183     /**
184      * changes the component type that is defined by the users selection
185      *
186      *@param the ValueChangeEvent which holds the desired component
187      */

188     public void componentTypeChanged(ValueChangeEvent vce) {
189         if (selectedNodeObject != null) {
190             Object JavaDoc nvalue = vce.getNewValue();
191             if (nvalue instanceof Integer JavaDoc) {
192                 selectedNodeObject.setComponentType((Integer JavaDoc) nvalue);
193             }
194         }
195     }
196
197     /**
198      * Copies the selected node in the tree.
199      *
200      * @param event that fired this method
201      */

202     public void copySelectedNode(ActionEvent event) {
203         if (selectedNodeObject != null)
204             selectedNodeObject.copyNode();
205     }
206     
207     /**
208      * Adds a new node to the tree in determined position, either below a non
209      * root node or under the root node.
210      *
211      *@param event that fired this method
212      */

213     public void addSelectedNode(ActionEvent event) {
214
215         // new object to add
216
DefaultMutableTreeNode JavaDoc treeNode;
217         NodeUserObject node;
218
219         // Add the new node under the currently selected.
220
if (selectedNodeObject != null){
221             // copies node and adds it under the selected node
222
node = selectedNodeObject.copyNode();
223             // update containing component
224
node.setComponentType(componentToAdd);
225             // set selected to newly added node.
226
selectedNodeObject = node;
227             selectedNode = node.getLabel();
228             selectedNodeObject.setValue(node.generateValues());
229         }
230         // otherwise create a new node below the root node.
231
else{
232             treeNode = new DefaultMutableTreeNode JavaDoc();
233             node = new NodeUserObject(treeNode, this);
234             node.setComponentType(componentToAdd);
235             treeNode.setUserObject(node);
236
237             selectedNodeObject = node;
238             selectedNode = node.getLabel();
239             selectedNodeObject.setValue(node.generateValues());
240             // add the new node to the end of the tree
241
rootTreeNode.add(treeNode);
242         }
243
244         // update the icons for the newly changes state.
245
updateNodeIcon(node);
246     }
247     
248     /**
249      * Updates the node icon in the UI to reflect changes to the node
250      *
251      *@param the node object to change
252      */

253     private void updateNodeIcon(NodeUserObject node){
254
255         Enumeration JavaDoc children = rootTreeNode.depthFirstEnumeration();
256         DefaultMutableTreeNode JavaDoc tmpNode;
257         Object JavaDoc tmp;
258         while (children.hasMoreElements()){
259             tmp = children.nextElement();
260             if (tmp instanceof DefaultMutableTreeNode JavaDoc){
261                 tmpNode = (DefaultMutableTreeNode JavaDoc)tmp;
262                 if (tmpNode.isLeaf()){
263                     ((NodeUserObject)(tmpNode.getUserObject())).setLeaf(true);
264                 } else{
265                     ((NodeUserObject)(tmpNode.getUserObject())).setLeaf(false);
266                 }
267             }
268
269         }
270
271     }
272
273     /**
274      * Determines whether the delete button is disabled. The delete button
275      * should be disabled if the node that was previously selected was deleted
276      * or if no node is otherwise selected. The root node is a special case and
277      * cannot be deleted.
278      *
279      * @return the disabled status of the delete button
280      */

281     public boolean isDeleteDisabled() {
282         //can't delete the root node
283
return (selectedNode == null || selectedNode.equals("Node 1") ||
284                 selectedNodeObject == null);
285     }
286
287     /**
288      * Determines whether the copy button is disabled. This should only occur
289      * when there is no node selected, which occurs at initialization and when a
290      * node is deleted.
291      *
292      * @return the disabled status of the copy button
293      */

294     public boolean isCopyDisabled() {
295         return (selectedNode == null || selectedNodeObject == null);
296     }
297
298     /**
299      * Construction the default tree structure by combining tree nodes.
300      */

301     public TreeBean() {
302         rootTreeNode = new DefaultMutableTreeNode JavaDoc();
303         NodeUserObject rootObject = new NodeUserObject(rootTreeNode, this);
304         rootTreeNode.setUserObject(rootObject);
305
306         model = new DefaultTreeModel JavaDoc(rootTreeNode);
307
308         for (int i = 0; i < 3; i++) {
309             DefaultMutableTreeNode JavaDoc branchNode = new DefaultMutableTreeNode JavaDoc();
310             NodeUserObject branchObject = new NodeUserObject(branchNode, this);
311             branchNode.setUserObject(branchObject);
312             branchObject.setLeaf(true);
313             rootTreeNode.add(branchNode);
314
315         }
316     }
317
318     /**
319      * Gets the tree's default model.
320      *
321      * @return tree model.
322      */

323     public DefaultTreeModel JavaDoc getModel() {
324         return model;
325     }
326
327     /**
328      * Sets the tree's default model.
329      *
330      * @param model new default tree model
331      */

332     public void setModel(DefaultTreeModel JavaDoc model) {
333         this.model = model;
334     }
335
336     /**
337      * Gets the selected node display text.
338      *
339      * @return selected node display text.
340      */

341     public String JavaDoc getSelectedNode() {
342         return selectedNode;
343     }
344
345     /**
346      * Sets the selected node. This changes a local instance variable used for
347      * display, it does not directly change the tree node state.
348      *
349      * @param selectedNode selected node text.
350      */

351     public void setSelectedNode(String JavaDoc selectedNode) {
352         this.selectedNode = selectedNode;
353     }
354
355     /**
356      * Increment the label count and return it.
357      *
358      * @return the new label count
359      */

360     public int getIncreasedLabelCount() {
361         return ++labelCount;
362     }
363 }
364
Popular Tags