KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.icesoft.faces.component.tree.IceUserObject;
37
38 import javax.faces.context.FacesContext;
39 import javax.faces.event.ActionEvent;
40 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
41 import java.util.Locale JavaDoc;
42 import java.util.MissingResourceException JavaDoc;
43 import java.util.ResourceBundle JavaDoc;
44
45 /**
46  * <p>The <code>NodeUserObject</code> represents a nodes user object. This
47  * particular IceUserobject implementation stores extra information on how many
48  * times the parent node is clicked on. It is also responsible for copying and
49  * deleting its self.</p>
50  * <p/>
51  * <p>In this example pay particularly close attention to the
52  * <code>wrapper</code> instance variable on IceUserObject. The
53  * <code>wrapper</code> allows for direct manipulations of the parent tree.
54  * </p>
55  *
56  * @since 1.0
57  */

58 public class NodeUserObject extends IceUserObject {
59
60     // treebean pointer - used to store selected node
61
private TreeBean treeBean;
62
63     // message bundle references
64
private static String JavaDoc nodeToolTip;
65
66     // index of the component type
67
private Integer JavaDoc componentType = new Integer JavaDoc(1);
68
69     // actual node label
70
private String JavaDoc label;
71     
72     // value of the component
73
private String JavaDoc value;
74     
75     
76     /**
77      * Load resource bundle for displaying proper node labels.
78      */

79     static {
80         Locale JavaDoc locale =
81                 FacesContext.getCurrentInstance().getViewRoot().getLocale();
82         // assign a default local if the faces context has none; shouldn't happen
83
if (locale == null) {
84             locale = Locale.ENGLISH;
85         }
86         ResourceBundle JavaDoc messages = ResourceBundle.getBundle(
87                 "com.icesoft.icefaces.samples.showcase.resources.messages",
88                 locale);
89
90         // assign labels
91
try {
92             if (messages != null) {
93                 nodeToolTip =
94                         messages.getString("components.tree.node.tooltip");
95             }
96         } catch (MissingResourceException JavaDoc e) {
97             // eat the error
98
}
99     }
100
101     /**
102      * Creates a new <code>NodeUserObject</code> object. Default image states
103      * are set as well as the expansion of all branch leafs.
104      *
105      * @param wrapper parent tree node which wrapps this object
106      * @param treeBeanPointer callback to parent TreeBean class.
107      */

108     public NodeUserObject(DefaultMutableTreeNode JavaDoc wrapper,
109                           TreeBean treeBeanPointer) {
110         super(wrapper);
111
112         treeBean = treeBeanPointer;
113         label = generateLabel();
114         value = generateValues();
115         
116         setLeafIcon("xmlhttp/css/xp/css-images/tree_document.gif");
117         setBranchContractedIcon(
118                 "xmlhttp/css/xp/css-images/tree_folder_close.gif");
119         setBranchExpandedIcon("xmlhttp/css/xp/css-images/tree_folder_open.gif");
120         setText(label);
121         setTooltip(nodeToolTip);
122         setExpanded(true);
123     }
124
125     /**
126      * Generates a label for the node based on an incrementing int.
127      *
128      * @return the generated label (eg. 'Node 5')
129      */

130     private String JavaDoc generateLabel() {
131         return "Node " + treeBean.getIncreasedLabelCount();
132     }
133
134     /**
135      * Returns true if this node is the root of the tree. The root is the only
136      * node in the tree with a null parent; every tree has exactly one root.
137      *
138      * @return true if this node is the root of its tree.
139      */

140     public boolean isRootNode() {
141         return getWrapper().isRoot();
142     }
143
144     /**
145      * Deletes this not from the parent tree.
146      *
147      * @param event that fired this method
148      */

149     public void deleteNode(ActionEvent event) {
150         ((DefaultMutableTreeNode JavaDoc) getWrapper().getParent())
151                 .remove(getWrapper());
152     }
153
154     /**
155      * Copies this node and adds it as a child node.
156      *
157      * @return the newly created node copy.
158      */

159     public NodeUserObject copyNode() {
160         DefaultMutableTreeNode JavaDoc clonedWrapper = new DefaultMutableTreeNode JavaDoc();
161         NodeUserObject clonedUserObject =
162                 new NodeUserObject(clonedWrapper, treeBean);
163         NodeUserObject originalUserObject =
164                 (NodeUserObject) getWrapper().getUserObject();
165         clonedUserObject.setAction(originalUserObject.getAction());
166         clonedUserObject.setBranchContractedIcon(
167                 originalUserObject.getBranchContractedIcon());
168         clonedUserObject.setBranchExpandedIcon(
169                 originalUserObject.getBranchExpandedIcon());
170         clonedUserObject.setExpanded(originalUserObject.isExpanded());
171         clonedUserObject.setLeafIcon(originalUserObject.getLeafIcon());
172         clonedUserObject.setTooltip(nodeToolTip);
173         clonedWrapper.setUserObject(clonedUserObject);
174         getWrapper().insert(clonedWrapper, 0);
175
176         return clonedUserObject;
177     }
178
179     /**
180      * Returns the label associated with the node.
181      *
182      * @return the label of the node
183      */

184     public String JavaDoc getLabel() {
185         return label;
186     }
187
188     /**
189      * Registers a user click with this object and updates the selected node in
190      * the TreeBean.
191      *
192      * @param event that fired this method
193      */

194     public void nodeClicked(ActionEvent event) {
195         treeBean.setSelectedNode(this.label);
196         treeBean.setSelectedNodeObject(this);
197         
198     }
199
200     /**
201      * Gets the component type.
202      *
203      * @return the component type
204      */

205     public Integer JavaDoc getComponentType() {
206         return componentType;
207     }
208
209     /**
210      * Sets the component type.
211      *
212      * @param componentType the new component type
213      */

214     public void setComponentType(Integer JavaDoc componentType) {
215         this.componentType = componentType;
216     }
217      
218     /**
219      * gets the information that is stored in each node.
220      *
221      * @return the string value
222      */

223     public String JavaDoc getValue()
224     {
225         return value;
226     }
227     
228     /**
229      * sets the value which is the information stored in the node.
230      *
231      * @param value to be stored
232      */

233     public void setValue(String JavaDoc value)
234     {
235         this.value = value;
236     }
237     
238     /**
239      * generates default names for the value attribute depending on the
240      * component type
241      *
242      * @return the string value
243      */

244     public String JavaDoc generateValues()
245     {
246       return "label";
247     }
248     
249     
250 }
Popular Tags