1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.tags;
18
19 import java.io.IOException;
20 import javax.portlet.PortletRequest;
21 import javax.portlet.PortletURL;
22 import javax.portlet.RenderResponse;
23 import javax.servlet.http.HttpServletResponse;
24 import javax.servlet.jsp.JspException;
25 import javax.servlet.jsp.JspWriter;
26
27 import org.apache.webapp.admin.TreeControl;
28 import org.apache.webapp.admin.TreeControlNode;
29 import org.apache.webapp.admin.TreeControlTag;
30
31 /***
32 * @author <a href="mailto:jford@apache.org">Jeremy Ford</a>
33 */
34 public class PortletTreeControlTag extends TreeControlTag
35 {
36 private static final String PORTLET_REQUEST = "portlet_request";
37 private static final String PORTLET_SESSION = "portlet_session";
38
39 /***
40 * The names of tree state images that we need.
41 *
42 * Copied from parent
43 */
44 static final String IMAGE_HANDLE_DOWN_LAST = "handledownlast.gif";
45 static final String IMAGE_HANDLE_DOWN_MIDDLE = "handledownmiddle.gif";
46 static final String IMAGE_HANDLE_RIGHT_LAST = "handlerightlast.gif";
47 static final String IMAGE_HANDLE_RIGHT_MIDDLE = "handlerightmiddle.gif";
48 static final String IMAGE_LINE_LAST = "linelastnode.gif";
49 static final String IMAGE_LINE_MIDDLE = "linemiddlenode.gif";
50 static final String IMAGE_LINE_VERTICAL = "linevertical.gif";
51
52 public void setScope(String scope) {
53 if (!PORTLET_REQUEST.equals(scope) &&
54 !PORTLET_SESSION.equals(scope)
55 )
56 {
57 super.setScope(scope);
58 }
59
60 this.scope = scope;
61 }
62
63 /***
64 * Return the <code>TreeControl</code> instance for the tree control that
65 * we are rendering.
66 *
67 * @exception JspException if no TreeControl instance can be found
68 */
69 protected TreeControl getTreeControl() throws JspException {
70
71 Object treeControl = null;
72
73 PortletRequest renderRequest = (PortletRequest) pageContext.findAttribute("javax.portlet.request");
74 if(PORTLET_REQUEST.equals(scope))
75 {
76
77 treeControl = renderRequest.getAttribute(tree);
78 }
79 else if(PORTLET_SESSION.equals(scope))
80 {
81 treeControl = renderRequest.getPortletSession().getAttribute(tree);
82 }
83
84 if (treeControl == null)
85 {
86 treeControl = super.getTreeControl();
87 }
88
89 return (TreeControl)treeControl;
90 }
91
92 /***
93 * Render the specified node, as controlled by the specified parameters.
94 *
95 * @param out The <code>JspWriter</code> to which we are writing
96 * @param node The <code>TreeControlNode</code> we are currently
97 * rendering
98 * @param level The indentation level of this node in the tree
99 * @param width Total displayable width of the tree
100 * @param last Is this the last node in a list?
101 *
102 * @exception IOException if an input/output error occurs
103 */
104 protected void render(JspWriter out, TreeControlNode node,
105 int level, int width, boolean last)
106 throws IOException {
107
108 try
109 {
110 HttpServletResponse response =
111 (HttpServletResponse) pageContext.getResponse();
112
113 RenderResponse renderResponse = (RenderResponse)pageContext.getRequest().getAttribute("javax.portlet.response");
114
115
116
117
118 if ("ROOT-NODE".equalsIgnoreCase(node.getName()) &&
119 (node.getLabel() == null)) {
120
121 TreeControlNode children[] = node.findChildren();
122 int lastIndex = children.length - 1;
123 int newLevel = level + 1;
124 for (int i = 0; i < children.length; i++) {
125 render(out, children[i], newLevel, width, i == lastIndex);
126 }
127 return;
128 }
129
130
131 out.println(" <tr valign=\"middle\">");
132 out.print("<td><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr valign=\"middle\">");
133
134
135 for (int i = 0; i < level; i++) {
136 int levels = level - i;
137 TreeControlNode parent = node;
138 for (int j = 1; j <= levels; j++)
139 parent = parent.getParent();
140 if (parent.isLast())
141 out.print(" <td> </td>");
142 else {
143 out.print(" <td><img src=\"");
144 out.print(images);
145 out.print("/");
146 out.print(IMAGE_LINE_VERTICAL);
147 out.print("\" alt=\"\" border=\"0\"></td>");
148 }
149 out.println();
150 }
151
152
153
154 PortletURL treeActionUrl = renderResponse.createActionURL();
155 treeActionUrl.setParameter("node", node.getName());
156 String treeAction = treeActionUrl.toString();
157 out.print(" <td>");
158
159
160
161 out.print("<a name=\"");
162 out.print(node.getName());
163 out.print("\">");
164
165 if ((action != null) && !node.isLeaf()) {
166 out.print("<a href=\"");
167 out.print(response.encodeURL(treeAction));
168 out.print("\">");
169 }
170 out.print("<img src=\"");
171 out.print(images);
172 out.print("/");
173 if (node.isLeaf()) {
174 if (node.isLast())
175 out.print(IMAGE_LINE_LAST);
176 else
177 out.print(IMAGE_LINE_MIDDLE);
178 out.print("\" alt=\"");
179 } else if (node.isExpanded()) {
180 if (node.isLast())
181 out.print(IMAGE_HANDLE_DOWN_LAST);
182 else
183 out.print(IMAGE_HANDLE_DOWN_MIDDLE);
184 out.print("\" alt=\"close node");
185 } else {
186 if (node.isLast())
187 out.print(IMAGE_HANDLE_RIGHT_LAST);
188 else
189 out.print(IMAGE_HANDLE_RIGHT_MIDDLE);
190 out.print("\" alt=\"expand node");
191 }
192 out.print("\" border=\"0\">");
193 if ((action != null) && !node.isLeaf())
194 out.print("</a>");
195 out.println("</td>");
196
197
198
199
200 String hyperlink = null;
201 String nodeAction = node.getAction();
202 if(nodeAction == null && node.isExpandWhenClicked())
203 {
204 hyperlink = treeAction;
205 }
206 if (nodeAction != null)
207 {
208 if(node.getAction().equals("portlet_url"))
209 {
210 PortletURL actionUrl = renderResponse.createActionURL();
211 actionUrl.setParameter("select_node", node.getName());
212 hyperlink = ((HttpServletResponse) pageContext.getResponse()).encodeURL(actionUrl.toString());
213 }
214 else
215 {
216 hyperlink = ((HttpServletResponse) pageContext.getResponse()).
217 encodeURL(node.getAction());
218 }
219 }
220
221
222
223 out.print(" <td ");
224
225 if(node.getLabel() != null)
226 {
227
228 out.print(" style=\"");
229 out.print("white-space:nowrap; vertical-align:middle;");
230 out.print("\"");
231 }
232
233 out.print(">");
234 if (node.getIcon() != null) {
235 if (hyperlink != null) {
236 out.print("<a href=\"");
237 out.print(hyperlink);
238 out.print("\"");
239 String target = node.getTarget();
240 if(target != null) {
241 out.print(" target=\"");
242 out.print(target);
243 out.print("\"");
244 }
245
246
247
248
249
250
251 out.print(">");
252 }
253 out.print("<img src=\"");
254 out.print(images);
255 out.print("/");
256 out.print(node.getIcon());
257 out.print("\" alt=\"");
258 out.print("\" border=\"0\">");
259 if (hyperlink != null)
260 out.print("</a>");
261 }
262
263
264
265 if (node.getLabel() != null) {
266 String labelStyle = node.getCSSClass();
267 if (node.isSelected() && (styleSelected != null))
268 labelStyle = styleSelected;
269 else if (!node.isSelected() && (styleUnselected != null))
270 labelStyle = styleUnselected;
271 if (hyperlink != null) {
272
273
274 out.print(" <a href=\"");
275 out.print(hyperlink);
276 out.print("\"");
277 String target = node.getTarget();
278 if(target != null) {
279 out.print(" target=\"");
280 out.print(target);
281 out.print("\"");
282 }
283 if (labelStyle != null) {
284 out.print(" class=\"");
285 out.print(labelStyle);
286 out.print("\"");
287 }
288
289 if(node.getTitle() != null)
290 {
291 out.print(" title=\"");
292 out.print(node.getTitle());
293 out.print("\"");
294 }
295
296
297
298
299
300
301
302 out.print(">");
303 } else if (labelStyle != null) {
304 out.print("<span class=\"");
305 out.print(labelStyle);
306 out.print("\">");
307 }
308 out.print(node.getLabel());
309 if (hyperlink != null)
310 out.print("</a>");
311 else if (labelStyle != null)
312 out.print("</span>");
313 }
314 out.println("</td>");
315
316
317
318
319 out.println(" </tr>");
320 out.println("</table></td></tr>");
321
322
323 if (node.isExpanded()) {
324 TreeControlNode children[] = node.findChildren();
325 int lastIndex = children.length - 1;
326 int newLevel = level + 1;
327 for (int i = 0; i < children.length; i++) {
328 render(out, children[i], newLevel, width, i == lastIndex);
329 }
330 }
331 }
332 catch(Exception e)
333 {
334 out.print(e.getLocalizedMessage());
335 }
336 }
337 }