KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > tree > TreeItem


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.tree;
19
20 import org.apache.beehive.netui.tags.AbstractSimpleTag;
21 import org.apache.beehive.netui.tags.html.IUrlParams;
22 import org.apache.beehive.netui.tags.internal.PageFlowTagUtils;
23 import org.apache.beehive.netui.util.Bundle;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.PageContext JavaDoc;
28 import javax.servlet.jsp.tagext.JspTag JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33
34 /**
35  * Instantiates a TreeElement object that will get added to the parent tag (either a Tree or
36  * another TreeItem).
37  * @jsptagref.tagdescription Adds a tree node to the parent node
38  * (either a {@link Tree} or another <netui:treeItem>).
39  * @example In this first sample, a TreeItem contained by a parent TreeItem or Tree will
40  * display with
41  * the label of "Login" and, when clicked, will navigate to the Login pageflow.
42  * <pre> &lt;netui:treeItem action="/netui/login/Login.jpf">Login&lt;/netui:treeItem></pre>
43  * <p>In this next sample, a TreeItem contained by a parent TreeItem or Tree will display with
44  * the label of "Human Resources" and icon "folder16.gif" and most likely will have
45  * child TreeItem tags because it does not have an action and starts expanded.</p>
46  * <pre> &lt;netui:treeItem icon="folder16.gif" expanded="true">
47  * &lt;netui:treeLabel>Human Resources&lt;/netui:treeLabel>
48  * ...
49  * &lt;/netui:treeItem></pre>
50  * @netui:tag name="treeItem" body-content="scriptless" description="Instantiates a TreeElement object that will get added to the parent tag (either a Tree or another TreeItem)."
51  * @see Tree
52  * @see org.apache.beehive.netui.tags.tree.TreeElement
53  */

54 public class TreeItem extends AbstractSimpleTag implements IUrlParams
55 {
56     private String JavaDoc _action; // The action raised when the TreeItem is selected
57
private String JavaDoc _clientAction; // A client action that may run instead of the action.
58
private boolean _expanded; // boolean to indicate if the tree is expanded
59
private boolean _expandOnServer; // boolean to indicate if the tree should be expanded on the server
60
private boolean _disabled; // boolean to indicate if the tree should be expanded on the server
61
private String JavaDoc _href; // href link called when the TreeItem is selected
62
private String JavaDoc _scope; // the scope so that we support multiple page flows
63
private String JavaDoc _icon; // The icon representing the TreeItem
64
private TreeElement _treeElement; // pointer to the tree node
65
private String JavaDoc _target; // The target window for the selection event
66
private String JavaDoc _title; // The title (if there is one)
67
private String JavaDoc _tagId; // tag that should uniquely indentify a tree. Required for multiple tree using auto expand
68
private ArrayList JavaDoc _attributes; // array list of attributes
69
private InheritableState _state; // the state
70

71     private boolean _child = false; // has a child set the content
72
private String JavaDoc _childLabel; // value set by a TreeLabel
73
private String JavaDoc _childContent; // value set by a TreeItemContent tag
74

75
76     /**
77      * Return the name of the Tag.
78      */

79     public String JavaDoc getTagName()
80     {
81         return "TreeItem";
82     }
83
84     //*********************************** TAG PROPERTIES ***************************************************************
85

86     /**
87      * Set the TreeItem's action.
88      * @param action the action.
89      * @jsptagref.attributedescription The action to invoke when this tree node is clicked.
90      * @jsptagref.databindable true
91      * @jsptagref.attributesyntaxvalue <i>string_or_expression</i>
92      * @netui:attribute required="false" rtexprvalue="true"
93      * description="Set the TreeItem's action."
94      */

95     public void setAction(String JavaDoc action)
96             throws JspException JavaDoc
97     {
98         _action = setRequiredValueAttribute(action, "action");
99     }
100
101     /**
102      * Sets an action to run on the client when the tree is selected.
103      * @param action the client action.
104      * @jsptagref.attributedescription Sets an action to run on the client when the tree node is selected.
105      * @jsptagref.databindable true
106      * @jsptagref.attributesyntaxvalue <i>string_or_expression</i>
107      * @netui:attribute required="false" rtexprvalue="true"
108      * description="Sets an action to run on the client when the tree is selected."
109      */

110     public void setClientAction(String JavaDoc action)
111             throws JspException JavaDoc
112     {
113         _clientAction = setRequiredValueAttribute(action, "clientAction");
114     }
115
116     /**
117      * Set the ID of the tag.
118      * @param tagId the tagId.
119      * @jsptagref.attributedescription Sets of the id attribute of the tree node
120      * @jsptagref.databindable true
121      * @jsptagref.attributesyntaxvalue <i>string_or_expression</i>
122      * @netui:attribute required="false" rtexprvalue="true"
123      * description="Set the ID of the tag."
124      */

125     public void setTagId(String JavaDoc tagId)
126             throws JspException JavaDoc
127     {
128         _tagId = setRequiredValueAttribute(tagId, "tagId");
129     }
130
131     /**
132      * Sets the TreeItem expansion state.
133      * @param expanded the expanded state.
134      * @jsptagref.attributedescription Sets the expanded/collapsed state of the tree node.
135      * @jsptagref.databindable true
136      * @jsptagref.attributesyntaxvalue <i>boolean_or_booleanExpression</i>
137      * @netui:attribute required="false" rtexprvalue="true"
138      * description="Sets the TreeItem expansion state."
139      */

140     public void setExpanded(boolean expanded)
141     {
142         _expanded = expanded;
143     }
144
145     /**
146      * Disables the TreeItem.
147      * @param disabled the enabled/disabled state.
148      * @jsptagref.attributedescription Boolean. Determines if the tree node is enabled or disabled.
149      * @jsptagref.databindable true
150      * @jsptagref.attributesyntaxvalue <i>boolean_or_booleanExpression</i>
151      * @netui:attribute required="false" rtexprvalue="true"
152      * description="Sets the TreeItem expansion state."
153      */

154     public void setDisabled(boolean disabled)
155     {
156         _disabled = disabled;
157     }
158
159     /**
160      * Set the value of the expandOnServer attribute. If the attribute is <code>true</code>
161      * and <code>runAtClient</code> is also true, then an expansion on this node will cause that
162      * to happen on the server. When runAtClient is false, all expansions will happen on the server.
163      * @jsptagref.attributedescription If this attribute is <code>true</code>
164      * and <code>runAtClient</code> is also true, then an expansion on this node will cause that
165      * to happen on the server. When runAtClient is false, all expansions will happen on the server.
166      * @jsptagref.databindable true
167      * @jsptagref.attributesyntaxvalue <i>boolean_or_booleanExpression</i>
168      * @param expandOnServer boolean value indicating if the node should be expanded on the server.
169      * @netui:attribute required="false" rtexprvalue="true"
170      * description="If running on the client, cause the node to be expanded and contracted on the server."
171      */

172     public void setExpandOnServer(boolean expandOnServer)
173     {
174         _expandOnServer = expandOnServer;
175     }
176
177     /**
178      * Sets the TreeItem's href.
179      * @param href the href
180      * @jsptagref.attributedescription The href attribute of the node's link.
181      * @jsptagref.databindable true
182      * @jsptagref.attributesyntaxvalue <i>string_or_expression</i>
183      * @netui:attribute required="false" rtexprvalue="true"
184      * description="sets the TreeItem's href."
185      */

186     public void setHref(String JavaDoc href)
187             throws JspException JavaDoc
188     {
189         _href = setRequiredValueAttribute(href, "href");
190     }
191
192     /**
193      * Sets the TreeItems title.
194      * @param title
195      * @jsptagref.attributedescription The node's title
196      * @jsptagref.databindable true
197      * @jsptagref.attributesyntaxvalue <i>string_or_expression</i>
198      * @netui:attribute required="false" rtexprvalue="true"
199      * description="Sets the TreeItems title."
200      */

201     public void setTitle(String JavaDoc title)
202     {
203         _title = setNonEmptyValueAttribute(title);
204     }
205
206     /**
207      * Set the target scope for this tree node's URI. Any page flow that handles the URI will be made active within the
208      * given scope. Scopes allow multiple page flows to be active within the same user session; page flows in different
209      * scopes do not in general interact with each other. This attribute is commonly used in conjunction with the
210      * <code>target</code> attribute ({@link #setTarget}) to invoke a new page flow in a separate window.
211      * @param scope a String that identifies the scope in which the target page flow will be made active.
212      * @jsptagref.attributedescription Set the target scope for this tree node's URI.
213      * Any page flow that handles the URI will be made active within the given scope.
214      * Scopes allow multiple page flows to be active within the same user session; page flows
215      * in different scopes do not in general interact with each other. This attribute is
216      * commonly used in conjunction with the <code>target</code> attribute
217      * to invoke a new page flow in a separate window.
218      * @jsptagref.databindable true
219      * @jsptagref.attributesyntaxvalue <i>string_or_expression</i>
220      * @netui:attribute required="false" rtexprvalue="true"
221      * description="Set the target scope for this tree node's URI."
222      */

223     public void setScope(String JavaDoc scope)
224     {
225         _scope = setNonEmptyValueAttribute(scope);
226     }
227
228     /**
229      * Sets the TreeItem icon URI.
230      * @param icon the icon URI
231      * @jsptagref.attributedescription The icon URI.
232      * @jsptagref.databindable true
233      * @jsptagref.attributesyntaxvalue <i>string_or_expression</i>
234      * @netui:attribute required="false" rtexprvalue="true"
235      * description="Sets the icon representing this tree item."
236      */

237     public void setIcon(String JavaDoc icon)
238     {
239         _icon = setNonEmptyValueAttribute(icon);
240     }
241
242     //********************************** METHODS CALLED BY CHILD TAGS TO SET STATE **********************************
243

244     /**
245      * This method is called by children tags of the TreeItem. If there is a <code>TreeLabel</code>
246      * it will set the text of the label. A child <code>TreeLabel</code> will override the <code>label</code>
247      * attribute.
248      * @param label The text of the Label to be displayed for this TreeItem.
249      */

250     public void setItemLabel(String JavaDoc label)
251     {
252         _child = true;
253         _childLabel = label;
254     }
255
256     /**
257      * @param content
258      */

259     public void setItemContent(String JavaDoc content)
260     {
261         _child = true;
262         _childContent = content;
263     }
264
265     /**
266      * This method is called by the children tags of the TreeItem. If there is a <code>TreeHtmlAttribute</code>
267      * it will set the attribute name and value.
268      * @param attr
269      */

270     public void setItemAttribute(TreeHtmlAttributeInfo attr)
271     {
272         _child = true;
273         if (_attributes == null)
274             _attributes = new ArrayList JavaDoc();
275         _attributes.add(attr);
276     }
277
278     public void setItemInheritableState(InheritableState state)
279     {
280         _child = true;
281         _state = state;
282     }
283
284     public void setAddedChild()
285     {
286         _child = true;
287     }
288
289     /**
290      * @return TreeElement
291      */

292     public TreeElement getNode()
293     {
294         return _treeElement;
295     }
296
297     /**
298      * Sets the window target.
299      * @param target the window target.
300      * @jsptagref.attributedescription Sets the window target of the node's link.
301      * @jsptagref.databindable true
302      * @jsptagref.attributesyntaxvalue <i>string_or_expression</i>
303      * @netui:attribute required="false" rtexprvalue="true"
304      * description="Sets the window target."
305      */

306     public void setTarget(String JavaDoc target)
307     {
308         _target = target;
309     }
310
311
312     public void addParameter(String JavaDoc name, Object JavaDoc value, String JavaDoc facet) throws JspException JavaDoc
313     {
314         _treeElement.addParameter(name, value, facet);
315     }
316
317
318     //************************************* TAG METHODS ****************************************************************
319

320     /**
321      * Instantiate a new TreeElement.
322      * @throws JspException if a JSP exception has occurred
323      */

324     public void doTag()
325             throws JspException JavaDoc, IOException JavaDoc
326     {
327         // It is only legal to set either an action or an href, this will verify we have exactly one.
328
// report an error when there is more than one specified.
329
int have = 0;
330         if (_href != null) have++;
331         if (_action != null) have++;
332         if (have > 1) {
333             String JavaDoc s = Bundle.getString("Tags_Node_InvalidNode", new Object JavaDoc[]{"href, action"});
334             registerTagError(s, null);
335         }
336
337         // If we have specified an action, then we need to verify that the action is a valid action
338
PageContext JavaDoc pageContext = getPageContext();
339         if (_action != null) {
340             HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
341             boolean isAction = PageFlowTagUtils.isAction(request, _action);
342             if (!isAction) {
343                 registerTagError(Bundle.getString("Tags_BadAction", _action), null);
344             }
345         }
346
347         // create the tree node
348
JspTag JavaDoc parentTag = getParent();
349         if (parentTag instanceof Tree) {
350             _treeElement = new TreeRootElement();
351         }
352         else {
353             _treeElement = new TreeElement();
354         }
355
356         // the the values
357
_treeElement.setIcon(_icon);
358         _treeElement.setClientAction(_clientAction);
359         _treeElement.setScope(_scope);
360         _treeElement.setTitle(_title);
361         _treeElement.setTagId(_tagId);
362         _treeElement.setExpandOnServer(_expandOnServer);
363         _treeElement.setExpanded(_expanded);
364         _treeElement.setDisabled(_disabled);
365         _treeElement.setTarget(_target);
366         _treeElement.setHref(_href);
367         _treeElement.setAction(_action);
368
369         boolean inlineError = false;
370         if (hasErrors()) {
371             String JavaDoc s = getInlineError();
372             if (s == null)
373                 s = Bundle.getString("Tags_TreeItemErrorNoInline");
374             else
375                 inlineError = true;
376             _treeElement.setContent(s);
377         }
378
379         // insert the node into the parent node or into the tree itself.
380
if (parentTag instanceof Tree) {
381             Tree parentTree = (Tree) parentTag;
382             if (parentTree.getRootNode() != null) {
383                 String JavaDoc s = Bundle.getString("Tags_TreeMultipleRootNodes");
384                 parentTree.registerTagError(s, null);
385                 //reportErrors();
386
return;
387             }
388             parentTree.setRootNode(_treeElement);
389         }
390         else if (parentTag instanceof TreeItem) {
391             TreeItem parentNode = (TreeItem) parentTag;
392             parentNode.getNode().addChild(_treeElement);
393             parentNode.setAddedChild();
394         }
395         else {
396             // The parent is neither a Tree or a TreeItem so we must report an error
397
String JavaDoc s = Bundle.getString("Tags_InvalidNodeParent");
398             registerTagError(s, null);
399             reportErrors();
400             return;
401         }
402
403         String JavaDoc content = getBufferBody(true);
404         if (!_child) {
405             if (content != null)
406                 _childLabel = content;
407         }
408
409         // if there was a childLabel set then we need to update the label value for the tag.
410
if (_childLabel != null) {
411             _treeElement.setLabel(_childLabel);
412         }
413
414         if (hasErrors()) {
415             if (!inlineError)
416                 reportErrors();
417             return;
418         }
419
420         // if there was content defined for the node then we will set it on the node...
421
if (_childContent != null) {
422             _treeElement.setContent(_childContent);
423         }
424
425         // if there was inherited state set it now...
426
if (_state != null) {
427             _treeElement.setInheritableState(_state);
428         }
429
430         // if there is attribute then we should insert that into the TreeElement
431
if (_attributes != null && _attributes.size() > 0) {
432             Iterator JavaDoc it = _attributes.iterator();
433             while (it.hasNext()) {
434                 TreeHtmlAttributeInfo jsi = (TreeHtmlAttributeInfo) it.next();
435                 _treeElement.addAttribute(jsi);
436             }
437         }
438     }
439 }
440
Popular Tags