KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > business > addressbook > person > group > tree > PersonTreeModel


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: PersonTreeModel.java,v $
31  * Revision 1.4 2005/04/29 02:48:13 colinmacleod
32  * Data bugfixes.
33  * Changed primary key back to Integer.
34  *
35  * Revision 1.3 2005/04/10 20:09:37 colinmacleod
36  * Added new themes.
37  * Changed id type to String.
38  * Changed i tag to em and b tag to strong.
39  * Improved PicoContainerFactory with NanoContainer scripts.
40  *
41  * Revision 1.2 2005/04/09 17:19:08 colinmacleod
42  * Changed copyright text to GPL v2 explicitly.
43  *
44  * Revision 1.1.1.1 2005/03/10 17:50:35 colinmacleod
45  * Restructured ivata op around Hibernate/PicoContainer.
46  * Renamed ivata groupware.
47  *
48  * Revision 1.3 2004/11/12 18:17:09 colinmacleod
49  * Ordered imports.
50  *
51  * Revision 1.2 2004/11/12 15:57:05 colinmacleod
52  * Removed dependencies on SSLEXT.
53  * Moved Persistence classes to ivata masks.
54  *
55  * Revision 1.1 2004/07/13 19:41:14 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.3 2004/03/21 21:16:07 colinmacleod
61  * Shortened name to ivata op.
62  *
63  * Revision 1.2 2004/02/01 22:00:32 colinmacleod
64  * Added full names to author tags
65  *
66  * Revision 1.1.1.1 2004/01/27 20:57:51 colinmacleod
67  * Moved ivata openportal to SourceForge..
68  *
69  * Revision 1.3 2003/10/17 12:36:12 jano
70  * fixing problems with building
71  * converting intranet -> portal
72  * Eclipse building
73  *
74  * Revision 1.2 2003/10/15 13:18:02 colin
75  * fixing for XDoclet
76  *
77  * Revision 1.4 2003/08/22 09:27:32 jano
78  * fixing bugs with building tree
79  *
80  * Revision 1.3 2003/07/25 11:42:27 jano
81  * adding functionality for addressBook extension
82  *
83  * Revision 1.2 2003/05/10 17:48:44 colin
84  * changed GroupTreeModel to add onlyUsers parameter
85  *
86  * Revision 1.1 2003/02/24 19:09:21 colin
87  * moved to business
88  *
89  * Revision 1.5 2003/02/04 17:43:45 colin
90  * copyright notice
91  *
92  * Revision 1.4 2002/09/17 08:38:56 colin
93  * added group tree right to make the tree faster
94  *
95  * Revision 1.3 2002/08/26 15:09:59 jano
96  * added PersonTreeNode to TREE for storing GroupDO and PersonDO
97  *
98  * Revision 1.2 2002/06/17 07:28:46 colin
99  * improved and extended javadoc documentation
100  *
101  * Revision 1.1 2002/06/13 11:21:24 colin
102  * first version with rose model integration.
103  * -----------------------------------------------------------------------------
104  */

105 package com.ivata.groupware.business.addressbook.person.group.tree;
106
107
108 import java.io.IOException JavaDoc;
109 import java.io.ObjectInputStream JavaDoc;
110 import java.io.ObjectOutputStream JavaDoc;
111 import java.io.Serializable JavaDoc;
112 import java.util.Iterator JavaDoc;
113 import java.util.List JavaDoc;
114 import java.util.Vector JavaDoc;
115
116 import javax.swing.event.TreeModelListener JavaDoc;
117 import javax.swing.tree.TreeModel JavaDoc;
118 import javax.swing.tree.TreePath JavaDoc;
119
120 import com.ivata.groupware.admin.security.server.SecuritySession;
121 import com.ivata.groupware.business.addressbook.AddressBook;
122 import com.ivata.groupware.business.addressbook.person.PersonDO;
123 import com.ivata.groupware.business.addressbook.person.group.GroupConstants;
124 import com.ivata.groupware.business.addressbook.person.group.GroupDO;
125 import com.ivata.groupware.business.addressbook.person.group.right.UserRightFilter;
126 import com.ivata.mask.util.SystemException;
127
128
129 /**
130  * <p>Provides a tree model of the heirarchy of groups. This class is wrapped
131  * by {@link com.ivata.groupware.business.addressbook.person.group.PersonTreeModel PersonTreeModel}
132  * so that client classes can easily display the group tree heirarchy in a tree
133  * control.</p>
134  *
135  * <p>In <em>ivata groupware</em>, this tree control is displayed using {@link
136  * com.ivata.groupware.web.tag.webgui.TreeTag TreeTag}.</p>
137  *
138  * <p>This is a wrapper class which wraps the remote class
139  * {@link GroupTreeModelRemote GroupTreeModelRemote}, handles any
140  * <code>RemoteException</code> and implements interfaces required
141  * by the client application.</p>
142  *
143  * <p><strong>Note:</strong> This class provides data from {@link GroupTreeModelBean GroupTreeModelBean}.
144  * This is no local copy of the bean class, however, and changes here
145  * will not be automatically reflected in {@link GroupTreeModelBean GroupTreeModelBean}.</p>
146  *
147  * @since 2002-05-16
148  * @author Colin MacLeod
149  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
150  * @version $Revision: 1.4 $
151  * @see GroupTreeModelBean
152  * @see GroupTreeModelRemote
153  */

154 public class PersonTreeModel implements TreeModel JavaDoc, Serializable JavaDoc {
155
156     /**
157      * <p>
158      * The address book implementation, used to find the root group.
159      * </p>
160      */

161     private AddressBook addressBook;
162
163     /**
164      * <p>This parameter is used to set exclude a branch of the tree, beginning
165      * with the id of the node provided. This is useful because you don't want a
166      * node to appear in the tree to select its parent.</p>
167      */

168     private Integer JavaDoc excludeBranch = null;
169
170     /**
171      * <p>Stores the filter to apply, if one has been set.</p>
172      */

173     private UserRightFilter filter = null;
174
175     /**
176      * <p>Stores whether or not the tree includes people data in
177      * <code>PersonTreeNode.personDO</code>.</p>
178      */

179     private boolean includePeople = false;
180
181     /**
182      * <p>Stores whether or not the tree only includes users.</p>
183      */

184     private boolean onlyUsers = false;
185
186     /**
187      * <p>Specifies the id of the root tree element.</p>
188      */

189     private Integer JavaDoc rootId = GroupConstants.ADDRESS_BOOK;
190
191     /**
192      * <p>
193      * SecuritySession valid security session, used to build the whole tree.
194      * </p>
195      */

196     private SecuritySession securitySession;
197
198     /**
199      * Construct a group tree model with the given address book.
200      *
201      * @param addressBook valid address book. Used to retrieve the root node.
202      * @param securitySession valid security session, used to build the whole
203      * tree.
204      */

205     public PersonTreeModel(AddressBook addressBook, SecuritySession securitySession) {
206         this.addressBook = addressBook;
207         this.securitySession = securitySession;
208     }
209
210     /**
211      * <p>This method is not implemented for this class</p>
212      *
213      * @param l the listener to add. Not used in this class.
214      */

215     public void addTreeModelListener(final TreeModelListener JavaDoc l) {
216         throw new UnsupportedOperationException JavaDoc("Method addTreeModelListener() not implemented.");
217     }
218
219     /**
220      * <p>
221      * Helper. Get all the children of the given group node - this includes
222      * people, if so desired.
223      * </p>
224      *
225      * @param parent person tree node representing a parent group.
226      * @return all child groups and people contained in the group.
227      */

228     private List JavaDoc getAllChildren(final Object JavaDoc parent) {
229         GroupDO parentGroup = ((PersonTreeNode) parent).getGroup();
230         // make a list to include people in the children if necessary
231
List JavaDoc allChildren = new Vector JavaDoc();
232         try {
233             List JavaDoc groups = addressBook.findGroupsByParent(securitySession,
234                     parentGroup.getId());
235             Iterator JavaDoc groupIterator = groups.iterator();
236             while(groupIterator.hasNext()) {
237                 GroupDO childGroup = (GroupDO) groupIterator.next();
238                 if (!childGroup.getId().equals(excludeBranch)) {
239                     allChildren.add(new PersonTreeNode(childGroup));
240                 }
241             }
242         } catch(SystemException e) {
243             throw new RuntimeException JavaDoc("ERROR in group tree model: could not locate children of '"
244                 + parentGroup.getId()
245                 + "'",
246                 e);
247         }
248         if (includePeople) {
249             Iterator JavaDoc personIterator = parentGroup.getPeople().iterator();
250             while(personIterator.hasNext()) {
251                 allChildren.add(new PersonTreeNode((PersonDO) personIterator.next()));
252             }
253         }
254         return allChildren;
255     }
256
257     /**
258      * <p>Get the child of the parent group provided, with the specified
259      * index.</p>
260      *
261      * @param parent the parent for whom to return the child.
262      * @param index the index of the child within the internal parent array.
263      * @return the child of the parent group provided, with the specified index,
264      * or <code>null</code> if the specified group has no children.
265      */

266     public final Object JavaDoc getChild(final Object JavaDoc parent,
267             final int index) {
268         PersonTreeNode parentNode = (PersonTreeNode) parent;
269         if (parentNode.getPerson() != null) {
270             return null;
271         }
272         List JavaDoc allChildren = getAllChildren(parent);
273         if (allChildren.size() == 0) {
274             return null;
275         }
276         return allChildren.get(index);
277     }
278
279
280     /**
281      * <p>Get the number of children for the given parent group.</p>
282      *
283      * @param parent the parent for whom to return the count of the number of
284      * children.
285      * @return the number of children for the given parent group.
286      */

287     public final int getChildCount(final Object JavaDoc parent) {
288         PersonTreeNode parentNode = (PersonTreeNode) parent;
289         if (parentNode.getPerson() != null) {
290             return 0;
291         }
292         List JavaDoc allChildren = getAllChildren(parent);
293         return allChildren.size();
294     }
295
296     /**
297      * <p>This parameter is used to set exclude a branch of the tree, beginning
298      * with the id of the node provided. This is useful because you don't want a
299      * node to appear in the tree to select its parent.</p>
300      *
301      * @return branch to be excluded, or null if all branches are included.
302      */

303     public final Integer JavaDoc getExcludeBranch() {
304         return excludeBranch;
305     }
306
307     /**
308      * <p>This parameter is used to set a filter. For a full description, please
309      * refer to the documentation of {@link GroupTreeModelBean
310      * GroupTreeModelBean}.</p>
311      *
312      * @return filter applied, or <code>null</code> if none has been set.
313      */

314     public UserRightFilter getFilter() {
315         return filter;
316     }
317
318     /**
319      * <p>Get whether or not this tree should include people, or just display
320      * groups.</p>
321      *
322      * @return if <code>true</code>, then the tree will include
323      * people, otherwise only groups are displayed.
324      */

325     public final boolean getIncludePeople() {
326         return includePeople;
327     }
328
329     /**
330      * <p>Get the index of a particular child in the parent's array of
331      * children.</p>
332      *
333      * @param parent the parent node for whom to find the child index.
334      * @param child the child node to find the index for.
335      * @return the index of a child node for the given parent group, or
336      * <code>-1</code> if this parent has no children.
337      */

338     public final int getIndexOfChild(final Object JavaDoc parent,
339             final Object JavaDoc child) {
340         PersonTreeNode parentNode = (PersonTreeNode) parent;
341         if (parentNode.getPerson() != null) {
342             return -1;
343         }
344         List JavaDoc allChildren = getAllChildren(parent);
345         if (allChildren.size() == 0) {
346             return -1;
347         }
348         return allChildren.indexOf(child);
349     }
350
351     /**
352      * <p>Get whether or not this tree should include people, or just display
353      * groups.</p>
354      *
355      * @return <code>true</code>if only groups containing users
356      * (or groups containing groups containing users) are included,
357      * otherwise <code>false</code>.
358      */

359     public final boolean getOnlyUsers() {
360         return onlyUsers;
361     }
362
363     /**
364      * <p>Get the root node of the tree.</p>
365      *
366      * @return the root node of the tree, as an instance of {@link
367      * GroupTreeModelRemote GroupTreeModelRemote}.
368      */

369     public final Object JavaDoc getRoot() {
370         // the root element is always a group
371
try {
372             GroupDO root = addressBook.findGroupByPrimaryKey(securitySession, rootId);
373             return new PersonTreeNode(root);
374         } catch(SystemException e) {
375             throw new RuntimeException JavaDoc("ERROR in group tree model: could not locate root with id '"
376                 + rootId
377                 + "'",
378                 e);
379         }
380
381     }
382
383     /**
384      * <p>Set the unique identifier of the root node.</p>
385      *
386      * @return unique identifier of the root node.
387      */

388     public final Integer JavaDoc getRootId() {
389         return rootId;
390     }
391
392     /**
393      * <p>Unless people are included in the tree, always returns
394      * <code>false</code> since there are no leaf nodes in the group tree. If
395      * people are included, returns <code>true</code> if this is a person node.</p>
396      *
397      * @param node this parameter is not used.
398      * @return returns <code>true</code> if this is a person node, otherwise
399      * returns <code>false</code>.
400      */

401     public boolean isLeaf(final Object JavaDoc node) {
402         PersonTreeNode parentNode = (PersonTreeNode) node;
403         return parentNode.getPerson() != null;
404     }
405
406     /**
407      * <p>Serialize the object from the input stream provided.</p>
408      *
409      * @param ois the input stream to serialize the object from
410      * @throws IOException thrown by <code>ObjectInputStream.defaultReadObject()</code>.
411      * @throws ClassNotFoundException thrown by <code>ObjectInputStream.defaultReadObject()</code>.
412      */

413     private void readObject(final ObjectInputStream JavaDoc ois) throws ClassNotFoundException JavaDoc, IOException JavaDoc {
414         ois.defaultReadObject();
415     }
416
417     /**
418      * <p>This interface method is not implemented for this class</p>
419      *
420      * @param l the listener to remove. Not usd in this class.
421      */

422     public void removeTreeModelListener(final TreeModelListener JavaDoc l) {
423         throw new UnsupportedOperationException JavaDoc("Method removeTreeModelListener() not implemented.");
424     }
425
426     /**
427      * <p>This parameter is used to set exclude a branch of the tree, beginning
428      * with the id of the node provided. This is useful because you don't want a
429      * node to appear in the tree to select its parent.</p>
430      *
431      * @param excludeBranchParam new value of branch to exclude, or null not to
432      * exclude any branch.
433      */

434     public final void setExcludeBranch(final Integer JavaDoc excludeBranchParam) {
435         this.excludeBranch = excludeBranchParam;
436     }
437
438     /**
439      * <p>This parameter is used to set a filter. For a full description, please
440      * refer to the documentation of {@link GroupTreeModelBean
441      * GroupTreeModelBean}.</p>
442      *
443      * @param filter new value of the filter to apply to the tree.
444      */

445     public final void setFilter(final UserRightFilter filter) {
446         this.filter = filter;
447     }
448
449     /**
450      * <p>Set whether or not this tree should include people, or just display
451      * groups.</p>
452      *
453      * @param includePeople if <code>true</code>, then the tree will include
454      * people, otherwise (<code>false</code> or <code>null</code>) only
455      * groups are displayed.
456      */

457     public final void setIncludePeople(final boolean includePeople) {
458         this.includePeople = includePeople;
459     }
460
461     /**
462      * <p>Set whether or not this tree should only include users.</p>
463      *
464      * @param onlyUsers if <code>true</code>, then only groups containing users
465      * (or groups containing groups containing users) are included.
466      */

467     public final void setOnlyUsers(final boolean onlyUsers) {
468         this.onlyUsers = onlyUsers;
469     }
470
471     /**
472      * <p>Set the unique identifier of the root node.<p>
473      * @param rootId unique identifier of the root node.
474      */

475     public final void setRootId(final Integer JavaDoc rootId) {
476         this.rootId = rootId;
477     }
478
479     /**
480      * <p>This interface method is not implemented for this class.</p>
481      *
482      * @param path path to the node that the user has altered.
483      * @param newValue the new value from the TreeCellEditor.
484      */

485     public void valueForPathChanged(final TreePath JavaDoc path,
486             final Object JavaDoc newValue) {
487         throw new UnsupportedOperationException JavaDoc("Method valueForPathChanged() not implemented.");
488     }
489
490     /**
491      * <p>Serialize the object to the output stream provided.</p>
492      *
493      * @param oos the output stream to serialize the object to
494      * @throws IOException thrown by <code>ObjectOutputStream.defaultWriteObject()</code>
495      */

496     private void writeObject(final ObjectOutputStream JavaDoc oos) throws IOException JavaDoc {
497         oos.defaultWriteObject();
498     }
499 }
500
Popular Tags