KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > business > library > comment > tree > CommentTreeModel


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: CommentTreeModel.java,v $
31  * Revision 1.3 2005/04/10 20:31:59 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:44 colinmacleod
38  * Changed copyright text to GPL v2 explicitly.
39  *
40  * Revision 1.1.1.1 2005/03/10 17:52:06 colinmacleod
41  * Restructured ivata op around Hibernate/PicoContainer.
42  * Renamed ivata groupware.
43  *
44  * Revision 1.4 2004/11/12 18:16:05 colinmacleod
45  * Ordered imports.
46  *
47  * Revision 1.3 2004/11/12 15:57:15 colinmacleod
48  * Removed dependencies on SSLEXT.
49  * Moved Persistence classes to ivata masks.
50  *
51  * Revision 1.2 2004/07/31 10:26:38 colinmacleod
52  * Fixed comment tree.
53  *
54  * Revision 1.1 2004/07/13 19:47:28 colinmacleod
55  * Moved project to POJOs from EJBs.
56  * Applied PicoContainer to services layer (replacing session EJBs).
57  * Applied Hibernate to persistence layer (replacing entity EJBs).
58  *
59  * Revision 1.3 2004/03/21 21:16:28 colinmacleod
60  * Shortened name to ivata op.
61  *
62  * Revision 1.2 2004/02/01 22:07:31 colinmacleod
63  * Added full names to author tags
64  *
65  * Revision 1.1.1.1 2004/01/27 20:58:38 colinmacleod
66  * Moved ivata openportal to SourceForge..
67  *
68  * Revision 1.3 2003/10/28 13:16:14 jano
69  * commiting library,
70  * still fixing compile and building openGroupware project
71  *
72  * Revision 1.2 2003/10/15 14:16:53 colin
73  * fixing for XDoclet
74  *
75  * Revision 1.1 2003/02/24 19:09:22 colin
76  * moved to business
77  *
78  * Revision 1.2 2003/02/04 17:43:47 colin
79  * copyright notice
80  *
81  * Revision 1.1 2002/07/08 10:16:13 colin
82  * first version of comment/comment tree model design
83  * -----------------------------------------------------------------------------
84  */

85 package com.ivata.groupware.business.library.comment.tree;
86
87 import java.io.IOException JavaDoc;
88 import java.io.ObjectInputStream JavaDoc;
89 import java.io.ObjectOutputStream JavaDoc;
90 import java.io.Serializable JavaDoc;
91 import java.util.List JavaDoc;
92
93 import javax.swing.event.TreeModelListener JavaDoc;
94 import javax.swing.tree.TreeModel JavaDoc;
95 import javax.swing.tree.TreePath JavaDoc;
96
97 import com.ivata.groupware.admin.security.server.SecuritySession;
98 import com.ivata.groupware.business.library.Library;
99 import com.ivata.groupware.business.library.comment.CommentDO;
100 import com.ivata.groupware.business.library.item.LibraryItemDO;
101 import com.ivata.mask.util.SystemException;
102
103
104 /**
105  * <p>Provides a tree model of the heirarchy of comments, for a given {@link
106  * com.ivata.groupware.business.library.item.LibraryItemBean library item}.<p>
107  *
108  * <p>In ivata groupware, this tree control is displayed using {@link
109  * com.ivata.groupware.web.tag.webgui.TreeTag TreeTag}.</p>
110  *
111  * <p>This is a wrapper class which wraps the remote class
112  * {@link CommentTreeModelRemote CommentTreeModelRemote}, handles any
113  * <code>RemoteException</code> and implements interfaces required
114  * by the client application.</p>
115  *
116  * <p><strong>Note:</strong> This class provides data from {@link CommentTreeModelBean CommentTreeModelBean}.
117  * This is no local copy of the bean class, however, and changes here
118  * will not be automatically reflected in {@link CommentTreeModelBean CommentTreeModelBean}.</p>
119  *
120  * @since 2002-07-05
121  * @author Colin MacLeod
122  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
123  * @version $Revision: 1.3 $
124  * @see CommentTreeModelBean
125  * @see CommentTreeModelRemote
126  */

127 public class CommentTreeModel implements TreeModel JavaDoc, Serializable JavaDoc {
128     /**
129      * <p>Stores the library item this tree applies to.</p>
130      */

131     private LibraryItemDO item;
132     private Library library;
133     private SecuritySession securitySession;
134
135     /**
136      * <p>Initialize the comment tree model.</p>
137      */

138     public CommentTreeModel(Library library,
139             SecuritySession securitySession) {
140         this.library = library;
141         this.securitySession = securitySession;
142     }
143
144     /**
145      * <p>This method is not implemented for this class</p>
146      *
147      * @param l the listener to add. Not used in this class.
148      */

149     public void addTreeModelListener(final TreeModelListener JavaDoc l) {
150         throw new UnsupportedOperationException JavaDoc(
151             "Method addTreeModelListener( ) not implemented.");
152     }
153
154     /**
155      * <p>Get the reply to the parent comment provided, with the specified
156      * index.</p>
157      *
158      * @param parent the parent for whom to return the reply comment.
159      * @param index the index of the child within the internal parent array.
160      * @return the reply to the parent comment provided, with the specified
161      * index, or <code>null</code> if the specified comment has no
162      * replies.
163      */

164     public final Object JavaDoc getChild(final Object JavaDoc parent,
165             final int index) {
166         CommentDO parentComment = (CommentDO) parent;
167         return getReplies(parentComment).get(index);
168     }
169
170
171     /**
172      * <p>Get the number of replies to the given parent comment.</p>
173      *
174      * @param parentDO the parent for whom to return the count of the number of
175      * replies.
176      * @return the number of replies to the given parent comment.
177      */

178     public final int getChildCount(final Object JavaDoc parent) {
179         CommentDO parentComment = (CommentDO) parent;
180         return getReplies(parentComment).size();
181     }
182
183     /**
184      * <p>Get the index of a particular reply in the parent's array of
185      * reply comments.</p>
186      *
187      * @param parentDO the parent node for whom to find the reply index.
188      * @param childDO the reply node to find the index for.
189      * @return the index of a reply (child) node for the given parent comment.
190      */

191     public final int getIndexOfChild(final Object JavaDoc parent,
192             final Object JavaDoc child) {
193         CommentDO parentComment = (CommentDO) parent;
194         return getReplies(parentComment).indexOf(child);
195     }
196
197     /**
198      * <p>Get the library item for the tree. Each tree is unique to
199      * one library item.</p>
200      *
201      * @return library item referenced.
202      */

203     public final LibraryItemDO getItem() {
204         return item;
205     }
206
207     /**
208      * <p>
209      * Private Helper. Get all the comments with the parent provided.
210      * </p>
211      */

212     private List JavaDoc getReplies(final CommentDO parent) {
213         // is this the 'fake' root comment
214
if (parent.getId() == null) {
215             try {
216                 return library.findCommentByItem(securitySession, parent.getItem().getId());
217             } catch (SystemException e) {
218                 throw new RuntimeException JavaDoc(e);
219             }
220         } else {
221             try {
222                 return library.findCommentByParent(securitySession, parent.getId());
223             } catch (SystemException e) {
224                 throw new RuntimeException JavaDoc(e);
225             }
226         }
227     }
228
229     /**
230      * <p>Get the root node of the tree. The data structure for comments does
231      * not actually have a root comment - the effective root of each comment
232      * tree is the library item itself. This method returns a new
233      * <code>CommentDO</code> with a <code>null</code> identifier and the
234      * correct library item identifier.</p>
235      *
236      * @return the root node of the tree, as an instance of {@link
237      * com.ivata.groupware.business.library.comment.CommentDO CommentDO}.
238      */

239     public final Object JavaDoc getRoot() {
240         CommentDO root = new CommentDO();
241         root.setId(null);
242         root.setItem(item);
243         return root;
244     }
245
246     /**
247      * <p>Always returns <code>false</code> since there are no leaf nodes in the
248      * comment tree.</p>
249      *
250      * @param node this parameter is not used.
251      * @return always returns <code>false</code>.
252      */

253     public boolean isLeaf(final Object JavaDoc node) {
254         return false;
255     }
256
257     /**
258      * <p>Serialize the object from the input stream provided.</p>
259      *
260      * @param ois the input stream to serialize the object from
261      * @throws IOException thrown by <code>ObjectInputStream.defaultReadObject( )</code>.
262      * @throws ClassNotFoundException thrown by <code>ObjectInputStream.defaultReadObject( )</code>.
263      */

264     private void readObject(final ObjectInputStream JavaDoc ois)
265         throws ClassNotFoundException JavaDoc, IOException JavaDoc {
266         ois.defaultReadObject();
267     }
268
269     /**
270      * <p>This method is not implemented for this class</p>
271      *
272      * @param l the listener to remove. Not usd in this class.
273      */

274     public void removeTreeModelListener(final TreeModelListener JavaDoc l) {
275         throw new UnsupportedOperationException JavaDoc(
276             "Method removeTreeModelListener( ) not implemented.");
277     }
278     /**
279      * <p>Get the library item for the tree. Each tree is unique to
280      * one library item.</p>
281      *
282      * @param item library item referenced.
283      */

284     public final void setItem(final LibraryItemDO item) {
285         this.item = item;
286     }
287
288     /**
289      * <p>This method is not implemented for this class</p>
290      *
291      * @param path path to the node that the user has altered.
292      * @param newValue the new value from the TreeCellEditor.
293      */

294     public void valueForPathChanged(final TreePath JavaDoc path,
295             final Object JavaDoc newValue) {
296         throw new UnsupportedOperationException JavaDoc(
297             "Method valueForPathChanged( ) not implemented.");
298     }
299
300     /**
301      * <p>Serialize the object to the output stream provided.</p>
302      *
303      * @param oos the output stream to serialize the object to
304      * @throws IOException thrown by <code>ObjectOutputStream.defaultWriteObject( )</code>
305      */

306     private void writeObject(final ObjectOutputStream JavaDoc oos) throws IOException JavaDoc {
307         oos.defaultWriteObject();
308     }
309
310 }
311
Popular Tags