1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.dojo.components;
23
24 import java.util.Random;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.apache.struts2.components.ClosingUIBean;
30 import org.apache.struts2.views.annotations.StrutsTag;
31 import org.apache.struts2.views.annotations.StrutsTagAttribute;
32 import org.apache.struts2.views.annotations.StrutsTagSkipInheritance;
33
34 import com.opensymphony.xwork2.util.ValueStack;
35
36 /***
37 * <!-- START SNIPPET: javadoc -->
38 *
39 * Renders a tree node within a tree widget with AJAX support.<p/>
40 *
41 * Either of the following combinations should be used depending on if the tree
42 * is to be constructed dynamically or statically. <p/>
43 *
44 * <b>Dynamically:</b>
45 * <ul>
46 * <li>id - id of this tree node</li>
47 * <li>title - label to be displayed for this tree node</li>
48 * </ul>
49 *
50 * <b>Statically:</b>
51 * <ul>
52 * <li>rootNode - the parent node of which this tree is derived from</li>
53 * <li>nodeIdProperty - property to obtained this current tree node's id</li>
54 * <li>nodeTitleProperty - property to obtained this current tree node's title</li>
55 * <li>childCollectionProperty - property that returnds this current tree node's children</li>
56 * </ul>
57 *
58 * <!-- END SNIPPET: javadoc -->
59 *
60 * <p/> <b>Examples</b>
61 *
62 * <pre>
63 * <!-- START SNIPPET: example -->
64 *
65 * <-- Creating tree statically using hard-coded data. -->
66 * <s:tree id="..." label="...">
67 * <s:treenode id="..." label="..." />
68 * <s:treenode id="..." label="...">
69 * <s:treenode id="..." label="..." />
70 * <s:treenode id="..." label="..." />
71 * </s:treenode>
72 * <s:treenode id="..." label="..." />
73 * </s:tree>
74 *
75 * <-- Creating tree dynamically using data from backing action. -->
76 * <s:tree
77 * id="..."
78 * rootNode="..."
79 * nodeIdProperty="..."
80 * nodeTitleProperty="..."
81 * childCollectionProperty="..." />
82 *
83 * <!-- END SNIPPET: example -->
84 * </pre>
85 *
86 */
87 @StrutsTag(name="treenode", tldTagClass="org.apache.struts2.dojo.views.jsp.ui.TreeNodeTag", description="Render a tree node within a tree widget.")
88 public class TreeNode extends ClosingUIBean {
89 private static final String TEMPLATE = "treenode-close";
90 private static final String OPEN_TEMPLATE = "treenode";
91 private final static transient Random RANDOM = new Random();
92
93 public TreeNode(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
94 super(stack, request, response);
95 }
96
97 @Override
98 @StrutsTagSkipInheritance
99 public void setTheme(String theme) {
100 super.setTheme(theme);
101 }
102
103 @Override
104 public String getTheme() {
105 return "ajax";
106 }
107
108 public String getDefaultOpenTemplate() {
109 return OPEN_TEMPLATE;
110 }
111
112 protected String getDefaultTemplate() {
113 return TEMPLATE;
114 }
115
116 protected void evaluateExtraParams() {
117 super.evaluateExtraParams();
118
119
120 Boolean parseContent = (Boolean)stack.getContext().get(Head.PARSE_CONTENT);
121 boolean generateId = (parseContent != null ? !parseContent : true);
122
123 addParameter("pushId", generateId);
124 if ((this.id == null || this.id.length() == 0) && generateId) {
125
126
127 int nextInt = RANDOM.nextInt();
128 nextInt = nextInt == Integer.MIN_VALUE ? Integer.MAX_VALUE : Math.abs(nextInt);
129 this.id = "widget_" + String.valueOf(nextInt);
130 addParameter("id", this.id);
131 }
132
133 Tree parentTree = (Tree) findAncestor(Tree.class);
134 parentTree.addChildrenId(this.id);
135 }
136
137 @StrutsTagAttribute(description="Label expression used for rendering tree node label.", required=true)
138 public void setLabel(String label) {
139 super.setLabel(label);
140 }
141
142 @StrutsTagAttribute(description="The css class to use for element")
143 public void setCssClass(String cssClass) {
144 super.setCssClass(cssClass);
145 }
146
147 @StrutsTagAttribute(description="The css style to use for element")
148 public void setCssStyle(String cssStyle) {
149 super.setCssStyle(cssStyle);
150 }
151
152 @StrutsTagAttribute(description="The id to use for the element")
153 public void setId(String id) {
154 super.setId(id);
155 }
156
157 @StrutsTagAttribute(description="The name to set for element")
158 public void setName(String name) {
159 super.setName(name);
160 }
161 }