KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > web > tree > comment > CommentTreeNodeRenderer


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: CommentTreeNodeRenderer.java,v $
31  * Revision 1.2 2005/04/09 17:19:48 colinmacleod
32  * Changed copyright text to GPL v2 explicitly.
33  *
34  * Revision 1.1.1.1 2005/03/10 17:52:07 colinmacleod
35  * Restructured ivata op around Hibernate/PicoContainer.
36  * Renamed ivata groupware.
37  *
38  * Revision 1.5 2004/11/12 18:16:06 colinmacleod
39  * Ordered imports.
40  *
41  * Revision 1.4 2004/11/12 15:57:16 colinmacleod
42  * Removed dependencies on SSLEXT.
43  * Moved Persistence classes to ivata masks.
44  *
45  * Revision 1.3 2004/07/13 19:47:29 colinmacleod
46  * Moved project to POJOs from EJBs.
47  * Applied PicoContainer to services layer (replacing session EJBs).
48  * Applied Hibernate to persistence layer (replacing entity EJBs).
49  *
50  * Revision 1.2 2004/03/21 21:16:29 colinmacleod
51  * Shortened name to ivata op.
52  *
53  * Revision 1.1.1.1 2004/01/27 20:58:45 colinmacleod
54  * Moved ivata openportal to SourceForge..
55  *
56  * Revision 1.3 2003/11/18 09:57:52 jano
57  * commiting all forgoten staff
58  *
59  * Revision 1.2 2003/10/15 14:16:54 colin
60  * fixing for XDoclet
61  *
62  * Revision 1.2 2003/02/27 07:53:01 colin
63  * added missing setUserName on dateFormatter
64  *
65  * Revision 1.1 2003/02/24 19:33:33 colin
66  * moved to jsp
67  *
68  * Revision 1.4 2003/02/04 17:43:46 colin
69  * copyright notice
70  *
71  * Revision 1.3 2003/01/24 19:30:27 peter
72  * added pageContext to initialize parameters, URL rewriting
73  *
74  * Revision 1.2 2002/09/05 13:23:24 colin
75  * changed library user rights from canUser... to can...
76  *
77  * Revision 1.1 2002/08/15 09:48:49 colin
78  * first version
79  * -----------------------------------------------------------------------------
80  */

81 package com.ivata.groupware.web.tree.comment;
82
83 import java.util.Properties JavaDoc;
84
85 import javax.servlet.http.HttpServletRequest JavaDoc;
86 import javax.servlet.http.HttpSession JavaDoc;
87 import javax.servlet.jsp.JspException JavaDoc;
88 import javax.servlet.jsp.JspWriter JavaDoc;
89 import javax.servlet.jsp.PageContext JavaDoc;
90
91 import com.ivata.groupware.admin.security.server.SecuritySession;
92 import com.ivata.groupware.admin.setting.Settings;
93 import com.ivata.groupware.admin.setting.SettingsDataTypeException;
94 import com.ivata.groupware.business.library.comment.CommentDO;
95 import com.ivata.groupware.business.library.right.LibraryRights;
96 import com.ivata.groupware.util.SettingDateFormatter;
97 import com.ivata.groupware.web.tree.DefaultTreeNodeRenderer;
98 import com.ivata.groupware.web.tree.TreeNode;
99 import com.ivata.mask.util.SystemException;
100 import com.ivata.mask.web.format.CharacterEntityFormat;
101 import com.ivata.mask.web.format.DateFormatterException;
102 import com.ivata.mask.web.format.FormatConstants;
103 import com.ivata.mask.web.format.HTMLFormatter;
104 import com.ivata.mask.web.format.LineBreakFormat;
105
106
107 /**
108  * <p>Create at tree node renderer for library item comments. This
109  * renderer uses
110  * the same theme sections as <code>DefaultTreeNodeRenderer</code>
111  * though it
112  * sets
113  * new properties to be evaluated in a separate theme.</p>
114  *
115  * @since 2002-07-07
116  * @author Colin MacLeod
117  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
118  * @version $Revision: 1.2 $
119  * @see DefaultTreeNodeRenderer
120  */

121 public class CommentTreeNodeRenderer extends DefaultTreeNodeRenderer {
122
123     /**
124      * <p>Applied to the content of the tree tag</p>
125      */

126     private CharacterEntityFormat characterEntityFormat = new CharacterEntityFormat();
127
128     /**
129      * <p>Used to format the modified by field.</p>
130      */

131     private SettingDateFormatter dateFormatter;
132
133     /**
134      * <p>Used to check whether or not the current user can edit the
135      * current
136      * node.</p>
137      */

138     private LibraryRights libraryRights;
139
140     /**
141      * <p>Used to convert line breaks in plain text comments.</p>
142      */

143     private LineBreakFormat lineBreakFormat = new LineBreakFormat();
144
145     /**
146      * <p>Used to initialize get the spacer width.</p>
147      */

148     private Settings settings;
149     /**
150      * <p>Stores the number of the top level comment (top level meaning a
151      * comment
152      * which is not a reply to another comment). This is used to set the
153      * css
154      * class.</p>
155      */

156     int threadNumber = 0;
157     SecuritySession securitySession;
158
159     /**
160      * Constructor.
161      */

162     public CommentTreeNodeRenderer(SecuritySession securitySession,
163             SettingDateFormatter dateFormatter, LibraryRights
164             libraryRights, Settings settings) {
165         this.securitySession = securitySession;
166         this.dateFormatter = dateFormatter;
167         this.libraryRights = libraryRights;
168         this.settings = settings;
169     }
170
171     /**
172      * <p>Overridden to close the table which surrounds comment trees.</p>
173      *
174      * @param session the current session which can be used to retrieve
175      * settings.
176      * @param request the current servlet request which can be used to
177      * retrieve
178      * settings.
179      * @param out jsp writer which can be used to output HTML.
180      * @throws JspException if there is an error parsing the theme or
181      * writing the
182      * output.
183      * who experience an error on finalization.
184      */

185     public void finalize(final HttpSession JavaDoc session,
186             final HttpServletRequest JavaDoc request,
187             final JspWriter JavaDoc out) throws JspException JavaDoc {
188         try {
189             out.print(getTreeTag().getTheme().parseSection("finalizeCommentTree",
190                     new Properties JavaDoc()));
191         } catch (java.io.IOException JavaDoc e) {
192             throw new JspException JavaDoc(e);
193         }
194
195         super.finalize(session, request, out);
196     }
197
198     /**
199      * <p>Access the internal date formatter to change the date or time
200      * format
201      * applied.</p>
202      *
203      * @return the formatter which is used internally to convert the
204      * modified field
205      * to a string.
206      */

207     public final SettingDateFormatter getDateFormatter() {
208         return dateFormatter;
209     }
210
211     /**
212      * <p>This method has been overriden to initialize the settings in the
213      * internal
214      * date formatter, and to parse the comment surrounding table.</p>
215      *
216      * @param session the current session which can be used to retrieve
217      * settings.
218      * @param request the current servlet request which can be used to
219      * retrieve
220      * settings.
221      * @param out jsp writer which can be used to output HTML.
222      * @param pageContext the current PageContext
223      * @throws JspException if there is an error parsing the theme or
224      * writing the
225      * output.
226      * who experience an error on initialization.
227      */

228     public void initialize(final HttpSession JavaDoc session,
229             final HttpServletRequest JavaDoc request,
230             final JspWriter JavaDoc out,
231             final PageContext JavaDoc pageContext) throws JspException JavaDoc {
232         try {
233             out.print(getTreeTag().getTheme().parseSection("initializeCommentTree",
234                     new Properties JavaDoc()));
235         } catch (java.io.IOException JavaDoc e) {
236             throw new JspException JavaDoc(e);
237         }
238
239         super.initialize(session, request, out, pageContext);
240
241         // if we apply it, we'll want the line break format to do its job ;-)
242
lineBreakFormat.setConvertLineBreaks(true);
243     }
244
245     /**
246      * <p>Overridden to add the comment text property.</p>
247      *
248      * @param treeNode the current node in the tree being drawn.
249      * @param level the depth of this node within the tree, with 0 being
250      * root.
251      * @param properties all the properties are already defined. New
252      * properties
253      * should be added to this instance and returned.
254      * @return all of the properties which should be evaluated in the
255      * client theme
256      * section.
257      * @throws JspException if there is a format error with the date.
258      */

259     public Properties JavaDoc setAdditionalProperties(final TreeNode treeNode,
260             final int level,
261             final Properties JavaDoc properties) throws JspException JavaDoc {
262         // make the right formatter for the occasion
263
HTMLFormatter formatter = new HTMLFormatter();
264         CommentDO commentDO = (CommentDO) treeNode;
265
266         // see if the user is allowed to edit this comment
267
try {
268             if (libraryRights.canAmendComment(securitySession, commentDO)) {
269                 properties.setProperty("canUserEdit", "true");
270             }
271         } catch (SystemException e) {
272             throw new RuntimeException JavaDoc(e);
273         }
274
275         // if this is a plain text message, convert character entities and line
276
// breaks
277
if (commentDO.getFormat() == FormatConstants.FORMAT_TEXT) {
278             formatter.add(characterEntityFormat);
279             formatter.add(lineBreakFormat);
280         }
281
282         properties.setProperty("commentSubject",
283             formatter.format(commentDO.getSubject()));
284         properties.setProperty("commentText",
285             formatter.format(commentDO.getText()));
286         properties.setProperty("commentUserName", commentDO.getCreatedBy().getName());
287
288         try {
289             properties.setProperty("commentModified",
290                 dateFormatter.format(commentDO.getModified()));
291         } catch (DateFormatterException e1) {
292             throw new RuntimeException JavaDoc(e1);
293         }
294
295         // how large the spacer is depends on the level
296
try {
297             properties.setProperty("spacer",
298                 new Integer JavaDoc(
299                     level * settings.getIntegerSetting(securitySession,
300                             "libraryCommentSpacer",
301                             securitySession.getUser()).intValue()).toString());
302         } catch (SettingsDataTypeException e) {
303             throw new JspException JavaDoc(e);
304         } catch (SystemException e) {
305             throw new JspException JavaDoc(e);
306         }
307
308         // the css style for this row depends on the whether the thread number
309
// is odd or even, and whether the reply number within that thread is
310
// odd or even too.
311
String JavaDoc threadClass;
312
313         if ((threadNumber % 2) == 0) {
314             if ((level % 2) == 0) {
315                 threadClass = "commentEvenThreadEvenReply";
316             } else {
317                 threadClass = "commentEvenThreadOddReply";
318             }
319         } else if ((level % 2) == 0) {
320             threadClass = "commentOddThreadEvenReply";
321         } else {
322             threadClass = "commentOddThreadOddReply";
323         }
324
325         properties.setProperty("threadClass", threadClass);
326
327         // if this is a top level comment, then increase the thread counter
328
if (level == 0) {
329             ++threadNumber;
330         }
331
332         return properties;
333     }
334 }
335
Popular Tags