KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > web > tree > DefaultTreeNodeRenderer


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: DefaultTreeNodeRenderer.java,v $
31  * Revision 1.3 2005/04/10 20:32:01 colinmacleod
32  * Added new themes.
33  * Changed id type to String.
34  * Changed i tag to em and b tag to strong.
35  * Improved PicoContainerFactory with NanoContainer scripts.
36  *
37  * Revision 1.2 2005/04/09 17:19:10 colinmacleod
38  * Changed copyright text to GPL v2 explicitly.
39  *
40  * Revision 1.1.1.1 2005/03/10 17:50:36 colinmacleod
41  * Restructured ivata op around Hibernate/PicoContainer.
42  * Renamed ivata groupware.
43  *
44  * Revision 1.3 2004/12/23 21:01:31 colinmacleod
45  * Updated Struts to v1.2.4.
46  * Changed base classes to use ivata masks.
47  *
48  * Revision 1.2 2004/11/12 15:57:20 colinmacleod
49  * Removed dependencies on SSLEXT.
50  * Moved Persistence classes to ivata masks.
51  *
52  * Revision 1.1 2004/09/30 15:16:03 colinmacleod
53  * Split off addressbook elements into security subproject.
54  *
55  * Revision 1.3 2004/07/13 19:41:15 colinmacleod
56  * Moved project to POJOs from EJBs.
57  * Applied PicoContainer to services layer (replacing session EJBs).
58  * Applied Hibernate to persistence layer (replacing entity EJBs).
59  *
60  * Revision 1.2 2004/03/21 21:16:18 colinmacleod
61  * Shortened name to ivata op.
62  *
63  * Revision 1.1.1.1 2004/01/27 20:57:58 colinmacleod
64  * Moved ivata openportal to SourceForge..
65  *
66  * Revision 1.2 2004/01/20 15:11:46 jano
67  * fixing problems with new sslext
68  *
69  * Revision 1.1.1.1 2003/10/13 20:50:07 colin
70  * Restructured portal into subprojects
71  *
72  * Revision 1.2 2003/08/19 06:09:53 peter
73  * committed because the version on lucenec1 doesn't compile
74  *
75  * Revision 1.1 2003/02/24 19:33:33 colin
76  * moved to jsp
77  *
78  * Revision 1.6 2003/02/04 17:43:46 colin
79  * copyright notice
80  *
81  * Revision 1.5 2003/01/24 19:30:27 peter
82  * added pageContext to initialize parameters, URL rewriting
83  *
84  * Revision 1.4 2002/09/16 15:53:13 jano
85  * if you set up fomName it will submit form when you open or
86  * close folder, otherwise it will make a link
87  *
88  * Revision 1.3 2002/08/10 21:17:48 colin
89  * first version of HTML sanitizer/parser to clean up HTML code
90  *
91  * Revision 1.2 2002/07/01 08:09:44 colin
92  * added new antlr parser; trapped parse exception
93  *
94  * Revision 1.1 2002/06/21 11:58:37 colin
95  * restructured com.ivata.groupware.web into seperate
96  * subcategories:
97  * fornat, javascript, theme and tree.
98  */

99 // Source file: h:/cvslocal/ivata groupware/src/com.ivata.groupware/jsp/tree/DefaultTreeNodeRenderer.java
100
package com.ivata.groupware.web.tree;
101
102
103 import java.util.HashMap JavaDoc;
104
105 import javax.servlet.http.HttpServletRequest JavaDoc;
106 import javax.servlet.http.HttpSession JavaDoc;
107 import javax.servlet.jsp.JspException JavaDoc;
108 import javax.servlet.jsp.JspWriter JavaDoc;
109 import javax.servlet.jsp.PageContext JavaDoc;
110
111 import org.apache.struts.taglib.TagUtils;
112
113 import com.ivata.groupware.web.tag.webgui.tree.TreeTag;
114 import com.ivata.mask.util.StringHandling;
115 import com.ivata.mask.web.theme.Theme;
116 import com.ivata.mask.web.theme.ThemeParseException;
117
118
119 /**
120  * <p>Create a default tree node renderer to draw your tree. This
121  * renderer
122  * calls {@link com.ivata.groupware.web.theme.Theme#parseSection
123  * Theme.parseSection}
124  * to parse the standard tree theme sections 'treeOpen', 'treeClosed',
125  * 'treeNoChildren' and 'treeLeaf'.</p>
126  *
127  * <p>The object you provide must implement {@link
128  * com.ivata.groupware.web.DefaultTreeNode
129  * TreeNode}.</p>
130  *
131  * <p>In each of these sections, the property 'caption' is parsed out
132  * to the
133  * {@link com.ivata.groupware.web.DefaultTreeNode#getName( ) getName}
134  * value
135  * of the node
136  * provided, and the property 'id' is parsed out to the value of
137  * {@link com.ivata.groupware.web.DefaultTreeNode#getName( )
138  * getId}.</p>
139  *
140  * @since 2002-05-16
141  * @author Colin MacLeod
142  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
143  * @version $Revision: 1.3 $
144  * @see TreeNodeRenderer
145  * @see com.ivata.groupware.web.theme.Theme#parseSection
146  */

147 public class DefaultTreeNodeRenderer extends TreeNodeRenderer {
148
149     /**
150      * <p>The URL of the current page, used to create open folder/ close
151      * folder
152      * links.</p>
153      */

154     private String JavaDoc URL;
155
156     /**
157      * <p>valueholder foor the current <code>PageContext</code></p>
158      */

159     private PageContext JavaDoc pageContext;
160
161     /**
162      * <p>Render a standard tree using the theme sections 'treeOpen',
163      * 'treeClosed',
164      * 'treeNoChildren' and 'treeLeaf'.</p>
165      * <p>The following properties will be defined:<br/>
166      * <table cellpadding='2' cellspacing='5' border='0' align='center'
167      * width='85%'>
168      * <tr class='TableHeadingColor'>
169      * <th>property</th>
170      * <th>description</th>
171      * </tr>
172      * <tr class='TableRowColor'>
173      * <td>caption</td>
174      * <td>The value returned by <code>node.toString( )</code>.</td>
175      * </tr>
176      * <tr class='TableRowColor'>
177      * <td>id</td>
178      * <td>The id of the node.</td>
179      * </tr>
180      * <tr class='TableRowColor'>
181      * <td>last</td>
182      * <td>'Last' if this is the last node in the current branch,
183      * otherwise
184      * this property is not set.</td>
185      * </tr>
186      * </table>
187      * </p>
188      * <p> These properties are evaluated by calling {@link
189      * com.ivata.groupware.web.theme.Theme#parseSection(String
190      * sName,
191      * java.util.Properties properties)
192      * Theme.parseSection(String sName, java.util.Properties
193      * properties)}
194      * for each of the relevant theme sections.</p>
195      * @param model <code>TreeModel</code> representing the data of the
196      * tree.
197      * @param node <code>Object</code> representing a node in the tree.
198      * The
199      * <code>toString( )</code> of this node will be used as the
200      * 'caption'
201      * properties when parsing.
202      * @param expanded <code>true</code> if this node is 'open', otherwise
203      * <code>false</code>.
204      * @param leaf <code>true</code> if this node is a leaf node,
205      * otherwise
206      * <code>false</code>.
207      * A leaf node is one which can have no children, like a file in a
208      * filesystem
209      * tree.
210      * @param level the depth of this node within the tree, with 0 being
211      * root.
212      * @param last <code>true</code> if this node is the last in the
213      * current branch,
214      * otherwise <code>false</code>.
215      * @param theme this theme does the parsing.
216      * @param properties existing properties to parse.
217      * @return the parsed string.
218      * @throws JspException if the theme sections 'treeOpen',
219      * 'treeClosed'
220      * 'treeNoChildren' or 'treeLeaf' have not been defined.
221      *
222      *
223      */

224     public String JavaDoc render(javax.swing.tree.TreeModel JavaDoc model, Object JavaDoc node, boolean expanded, boolean leaf, int level, boolean last, Theme theme, java.util.Properties JavaDoc properties) throws JspException JavaDoc {
225         try {
226             TreeNode treeNode = (TreeNode) node;
227             HashMap JavaDoc URLProperties = new HashMap JavaDoc();
228
229             // set the caption property
230
properties.setProperty("caption", treeNode.getName());
231             // set the id property
232
if (treeNode.getId() != null) {
233                 properties.setProperty("id", treeNode.getId().toString());
234             }
235             // is this the last node? if so, set the last property
236
if (last) {
237                 properties.setProperty("last", "Last");
238             }
239             // is this a leaf = no children?
240
if (leaf) {
241                 return theme.parseSection("treeLeaf",
242                         setAdditionalProperties(treeNode, level, properties));
243             }
244             // see if this node has no children
245
if (model.getChildCount(node) == 0) {
246                 return theme.parseSection("treeNoChildren",
247                         setAdditionalProperties(treeNode, level, properties));
248             }
249             // options: it is either open or closed and if we will submit the form or we will make a link
250
TreeTag treeTag = getTreeTag();
251             if (expanded) {
252                 if (treeTag.getFormName() != null) {
253                     properties.setProperty("folderURL", "javascript:" + treeTag.getFormName() +
254                         ".closeFolder.value=" + treeNode.getId() + ";" +
255                         treeTag.getFormName() + ".submit();");
256                     return theme.parseSection("treeOpen",
257                             setAdditionalProperties(treeNode, level, properties));
258                 } else {
259                     URLProperties.put("closeFolder", treeNode.getId().toString());
260                     try {
261                         properties.setProperty("folderURL",
262                             TagUtils.getInstance().computeURL(pageContext, null, URL,
263                                 null, null, null, URLProperties, null, true));
264                     } catch (java.net.MalformedURLException JavaDoc e) {
265                         throw new JspException JavaDoc(e);
266                     }
267                     return theme.parseSection("treeOpen",
268                             setAdditionalProperties(treeNode, level, properties));
269                 }
270             } else {
271                 if (treeTag.getFormName() != null) {
272                     properties.setProperty("folderURL", "javascript:" + treeTag.getFormName() +
273                         ".openFolder.value=" + treeNode.getId() + ";" +
274                         treeTag.getFormName() + ".submit();");
275                     return theme.parseSection("treeClosed",
276                             setAdditionalProperties(treeNode, level, properties));
277                 } else {
278                     URLProperties.put("openFolder", treeNode.getId().toString());
279                     try {
280                         properties.setProperty("folderURL",
281                                 TagUtils.getInstance().computeURL(pageContext, null,
282                                         URL, null, null, null, URLProperties,
283                                         null, true));
284                     } catch (java.net.MalformedURLException JavaDoc e) {
285                         throw new JspException JavaDoc(e);
286                     }
287                     return theme.parseSection("treeClosed",
288                             setAdditionalProperties(treeNode, level, properties));
289                 }
290             }
291         } catch (ThemeParseException e) {
292             throw new RuntimeException JavaDoc(e);
293         }
294     }
295
296     /**
297      * <p>This method is called by the tree tag during
298      * <code>doStartTag</code> to
299      * allow the renderer to open or close folders as apropriate.</p>
300      *
301      * @param session the current session which can be used to retrieve
302      * settings.
303      * @param request the current servlet request which can be used to
304      * retrieve
305      * settings.
306      * @param out jsp writer which can be used to output HTML.
307      * @param pageContext the current <code>PageContext</code>
308      * @throws JspException not thrown by this class but can be thrown by
309      * subclasses
310      * who experience an error on initialization.
311      */

312     public void initialize(HttpSession JavaDoc session, HttpServletRequest JavaDoc request, JspWriter JavaDoc out, PageContext JavaDoc pageContext) throws JspException JavaDoc {
313         String JavaDoc openFolder = request.getParameter("openFolder");
314         TreeTag treeTag = getTreeTag();
315
316         if (!StringHandling.isNullOrEmpty(openFolder)) {
317             treeTag.setOpenFolder(StringHandling.integerValue(openFolder));
318         }
319         String JavaDoc closeFolder = request.getParameter("closeFolder");
320
321         if (!StringHandling.isNullOrEmpty(closeFolder)) {
322             treeTag.setCloseFolder(StringHandling.integerValue(closeFolder));
323         }
324         URL = request.getRequestURL().toString();
325         this.pageContext = pageContext;
326     }
327
328     /**
329      * <p>Can be overridden by super-class to provide addtional property
330      * information
331      * for a node.</p>
332      *
333      * @param treeNode the current node in the tree being drawn.
334      * @param level the depth of this node within the tree, with 0 being
335      * root.
336      * @param properties all the properties are already defined. New
337      * properties
338      * should be added to this instance and returned.
339      * @return all of the properties which should be evaluated in the
340      * client theme
341      * section.
342      * @throws JspException thrown by subclasses if there is a formatting
343      * error.
344      */

345     public java.util.Properties JavaDoc setAdditionalProperties(TreeNode treeNode, int level, java.util.Properties JavaDoc properties) throws JspException JavaDoc {
346         // this default implementation does nothing...
347
return properties;
348     }
349
350     /**
351      * <p>valueholder foor the current <code>PageContext</code></p>
352      *
353      * @return the current value of pageContext.
354      */

355     public PageContext JavaDoc getPageContext() {
356         return pageContext;
357     }
358
359     /**
360      * <p>valueholder foor the current <code>PageContext</code></p>
361      *
362      * @param pageContext the new value of pageContext.
363      */

364     public void setPageContext(PageContext JavaDoc pageContext) {
365         this.pageContext = pageContext;
366     }
367 }
368
Popular Tags