KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > taglib > TreeControlRenderTag


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Michel-Ange ANTON
22  * --------------------------------------------------------------------------
23  * $Id: TreeControlRenderTag.java,v 1.7 2003/11/12 17:19:38 antonma Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.webapp.taglib;
28
29 import java.io.IOException JavaDoc;
30 import java.net.URLEncoder JavaDoc;
31
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.jsp.JspException JavaDoc;
34 import javax.servlet.jsp.JspWriter JavaDoc;
35
36
37
38
39
40 public class TreeControlRenderTag extends TreeControlTag {
41     /**
42      * The names of tree state images that we need.
43      */

44     static final String JavaDoc IMAGE_NODE_OPEN = "node_open.gif";
45     static final String JavaDoc IMAGE_NODE_OPEN_FIRST = "node_open_first.gif";
46     static final String JavaDoc IMAGE_NODE_OPEN_MIDDLE = "node_open_middle.gif";
47     static final String JavaDoc IMAGE_NODE_OPEN_LAST = "node_open_last.gif";
48     static final String JavaDoc IMAGE_NODE_CLOSE = "node_close.gif";
49     static final String JavaDoc IMAGE_NODE_CLOSE_FIRST = "node_close_first.gif";
50     static final String JavaDoc IMAGE_NODE_CLOSE_MIDDLE = "node_close_middle.gif";
51     static final String JavaDoc IMAGE_NODE_CLOSE_LAST = "node_close_last.gif";
52     static final String JavaDoc IMAGE_BLANK = "noline.gif";
53     static final String JavaDoc IMAGE_LINE_FIRST = "line_first.gif";
54     static final String JavaDoc IMAGE_LINE_LAST = "line_last.gif";
55     static final String JavaDoc IMAGE_LINE_MIDDLE = "line_middle.gif";
56     static final String JavaDoc IMAGE_LINE_VERTICAL = "line.gif";
57
58 // --------------------------------------------------------- Instance Variables
59

60     /**
61      * Flag who indicate when the first node is rendered.
62      */

63     private boolean mb_FirstNodeRendered;
64     private int mi_MaxChar;
65
66 // --------------------------------------------------------- Public Methods
67

68     /**
69      * Render this tree control.
70      *
71      * @exception JspException if a processing error occurs
72      */

73     public int doEndTag()
74         throws JspException JavaDoc {
75         /*
76               mb_FirstNodeRendered = false;
77               return super.doEndTag();
78          */

79
80         TreeControl treeControl = getTreeControl();
81         JspWriter JavaDoc out = pageContext.getOut();
82         try {
83             out.print("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"");
84             if (style != null) {
85                 out.print(" class=\"");
86                 out.print(style);
87                 out.print("\"");
88             }
89             out.println(">");
90             int level = 0;
91             mb_FirstNodeRendered = false;
92             mi_MaxChar = 0;
93             TreeControlNode node = treeControl.getRoot();
94             render(out, node, level, treeControl.getWidth(), true);
95
96             // Avoid bug Netscape 4
97
out.print("<tr>");
98             for (int i = 0; i < treeControl.getWidth() + 2; i++) {
99                 out.print("<td></td>");
100             }
101             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<td nowrap>");
102             for (int i = 0; i < (mi_MaxChar + 1); i++) {
103                 sb.append("&nbsp;&nbsp;");
104             }
105             sb.append("</td>");
106             out.print(sb.toString());
107             out.println("</tr>");
108
109             out.println("</table>");
110         }
111         catch (IOException JavaDoc e) {
112             throw new JspException JavaDoc(e);
113         }
114         return (EVAL_PAGE);
115
116     }
117
118 // ------------------------------------------------------ Protected Methods
119

120     /**
121      * Render the specified node, as controlled by the specified parameters.
122      *
123      * @param out The <code>JspWriter</code> to which we are writing
124      * @param node The <code>TreeControlNode</code> we are currently
125      * rendering
126      * @param level The indentation level of this node in the tree
127      * @param width Total displayable width of the tree
128      * @param last Is this the last node in a list?
129      *
130      * @exception IOException if an input/output error occurs
131      */

132     protected void render(JspWriter JavaDoc out, TreeControlNode node, int level, int width, boolean last)
133         throws IOException JavaDoc {
134         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
135
136         // if the node is root node and the label value is
137
// null, then do not render root node in the tree.
138

139         if ("ROOT-NODE".equalsIgnoreCase(node.getName()) && (node.getLabel() == null)) {
140             // Render the children of this node
141
TreeControlNode children[] = node.findChildren();
142             int firstIndex = children.length - 1;
143             int lastIndex = children.length - 1;
144             int newLevel = level + 1;
145             for (int i = 0; i < children.length; i++) {
146                 render(out, children[i], newLevel, width, i == lastIndex);
147             }
148             return;
149         }
150
151         // Render the beginning of this node
152
out.println(" <tr valign=\"middle\">");
153
154         // Create the appropriate number of indents
155
for (int i = 0; i < level; i++) {
156             int levels = level - i;
157             TreeControlNode parent = node;
158             for (int j = 1; j <= levels; j++) {
159                 parent = parent.getParent();
160             }
161             if (parent.isLast()) {
162                 out.print(" <td></td>");
163             }
164             else {
165                 out.print(" <td><img SRC=\"");
166                 out.print(images);
167                 out.print("/");
168                 out.print("tree/");
169                 out.print(IMAGE_LINE_VERTICAL);
170                 out.print("\" border=\"0\"></td>");
171             }
172             out.println();
173         }
174
175         // Render the tree state image for this node
176

177         // HACK to take into account special characters like = and &
178
// in the node name, could remove this code if encode URL
179
// and later request.getParameter() could deal with = and &
180
// character in parameter values.
181
String JavaDoc encodedNodeName = URLEncoder.encode(node.getName(),"UTF-8");
182
183         String JavaDoc action = replace(getAction(), "${name}", encodedNodeName);
184
185         String JavaDoc updateTreeAction = replace(getAction(), "tree=${name}", "select=" + encodedNodeName);
186         updateTreeAction = ((HttpServletResponse JavaDoc) pageContext.getResponse()).encodeURL(
187             updateTreeAction);
188
189         out.print(" <td>");
190         if ((action != null) && !node.isLeaf()) {
191             out.print("<a HREF=\"");
192             out.print(response.encodeURL(action));
193             out.print("\">");
194         }
195         out.print("<img SRC=\"");
196         out.print(images);
197         out.print("/");
198         out.print("tree/");
199         if (node.isLeaf()) {
200             if (mb_FirstNodeRendered == false) {
201                 if (node.isLast()) {
202                     out.print(IMAGE_BLANK);
203                 }
204                 else {
205                     out.print(IMAGE_LINE_FIRST);
206                 }
207             }
208             else if (node.isLast()) {
209                 out.print(IMAGE_LINE_LAST);
210             }
211             else {
212                 out.print(IMAGE_LINE_MIDDLE);
213             }
214         }
215         else if (node.isExpanded()) {
216             if (mb_FirstNodeRendered == false) {
217                 if (node.isLast()) {
218                     out.print(IMAGE_NODE_OPEN);
219                 }
220                 else {
221                     out.print(IMAGE_NODE_OPEN_FIRST);
222                 }
223             }
224             else if (node.isLast()) {
225                 out.print(IMAGE_NODE_OPEN_LAST);
226             }
227             else {
228                 out.print(IMAGE_NODE_OPEN_MIDDLE);
229             }
230         }
231         else {
232             if (mb_FirstNodeRendered == false) {
233                 if (node.isLast()) {
234                     out.print(IMAGE_NODE_CLOSE);
235                 }
236                 else {
237                     out.print(IMAGE_NODE_CLOSE_FIRST);
238                 }
239             }
240             else if (node.isLast()) {
241                 out.print(IMAGE_NODE_CLOSE_LAST);
242             }
243             else {
244                 out.print(IMAGE_NODE_CLOSE_MIDDLE);
245             }
246         }
247         out.print("\" border=\"0\" align=\"absmiddle\">");
248         if ((action != null) && !node.isLeaf()) {
249             out.print("</a>");
250         }
251         out.println("</td>");
252
253         // Calculate the hyperlink for this node (if any)
254
String JavaDoc hyperlink = null;
255         if (node.getAction() != null) {
256             hyperlink = ((HttpServletResponse JavaDoc) pageContext.getResponse()).encodeURL(node.getAction());
257         }
258
259         // Render the icon for this node (if any)
260
out.print(" <td>");
261
262         // Anchor name
263
out.print("<a name=\"");
264         out.print(encodedNodeName);
265         out.print("\"></a>");
266
267         if (node.getIcon() != null) {
268             if (hyperlink != null) {
269                 out.print("<a HREF=\"");
270                 out.print(hyperlink);
271                 out.print("\"");
272                 String JavaDoc target = node.getTarget();
273                 if (target != null) {
274                     out.print(" target=\"");
275                     out.print(target);
276                     out.print("\"");
277                 }
278                 // to refresh the tree in the same 'self' frame
279
out.print(" onclick=\"");
280                 out.print("self.location.href='" + updateTreeAction + "'");
281                 out.print("\"");
282                 out.print(">");
283             }
284             out.print("<img SRC=\"");
285             out.print(images);
286             out.print("/");
287             out.print(node.getIcon());
288             out.print("\" border=\"0\" align=\"absmiddle\">");
289             if (hyperlink != null) {
290                 out.print("</a>");
291             }
292         }
293         out.println("</td>");
294
295         // Render the label for this node (if any)
296
int iColspan = width - level + 1;
297         if (iColspan > 1) {
298             out.print(" <td width=\"100%\" colspan=\"");
299             out.print(iColspan);
300             out.print("\" nowrap>");
301         }
302         else {
303             out.print(" <td width=\"100%\" nowrap>");
304         }
305         if (node.getLabel() != null) {
306             if (node.getLabel().length() > mi_MaxChar) {
307                 mi_MaxChar = node.getLabel().length();
308             }
309             // Note the leading space so that the text has some space
310
// between it and any preceding images
311
out.print("&nbsp;");
312             String JavaDoc labelStyle = null;
313             if (node.isSelected() && (styleSelected != null)) {
314                 labelStyle = styleSelected;
315             }
316             else if (!node.isSelected() && (styleUnselected != null)) {
317                 labelStyle = styleUnselected;
318             }
319             if (hyperlink != null) {
320                 out.print("<a HREF=\"");
321                 out.print(hyperlink);
322                 out.print("\"");
323                 String JavaDoc target = node.getTarget();
324                 if (target != null) {
325                     out.print(" target=\"");
326                     out.print(target);
327                     out.print("\"");
328                 }
329                 if (labelStyle != null) {
330                     out.print(" class=\"");
331                     out.print(labelStyle);
332                     out.print("\"");
333                 }
334                 // to refresh the tree in the same 'self' frame
335
out.print(" onclick=\"");
336                 out.print("self.location.href='" + updateTreeAction + "'");
337                 out.print("\"");
338                 out.print(">");
339             }
340             else if (labelStyle != null) {
341                 out.print("<span class=\"");
342                 out.print(labelStyle);
343                 out.print("\">");
344             }
345             out.print(node.getLabel());
346             if (hyperlink != null) {
347                 out.print("</a>");
348             }
349             else if (labelStyle != null) {
350                 out.print("</span>");
351             }
352         }
353         out.println("</td>");
354
355         // Render the end of this node
356
out.println(" </tr>");
357         // Remember the first node is rendered
358
mb_FirstNodeRendered = true;
359
360         // Render the children of this node
361
if (node.isExpanded()) {
362             TreeControlNode children[] = node.findChildren();
363             int lastIndex = children.length - 1;
364             int newLevel = level + 1;
365             for (int i = 0; i < children.length; i++) {
366                 render(out, children[i], newLevel, width, i == lastIndex);
367             }
368         }
369
370     }
371 }
372
Popular Tags