KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > adminGui > feature > account > groups > GroupTreeModel


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
package org.ozoneDB.adminGui.feature.account.groups;
8
9 import java.util.Vector JavaDoc;
10 import javax.swing.event.TreeModelEvent JavaDoc;
11 import javax.swing.event.TreeModelListener JavaDoc;
12 import javax.swing.tree.TreeModel JavaDoc;
13 import javax.swing.tree.TreePath JavaDoc;
14
15 import org.ozoneDB.adminGui.feature.account.AccountItem;
16
17
18 //#############################################################################
19
/**
20  * This class is used to manage the group table.
21  *
22  * @author <p align=center>Ibsen Ramos-Bonilla
23  * <br>Copyright &copy 1997-@year@ by SMB GmbH. All Rights Reserved.</p>
24  *
25  * @version 1.0
26  */

27 //#############################################################################
28

29 public class GroupTreeModel implements TreeModel JavaDoc {
30
31     /** The listeners used on the tree to know was going on. */
32     private Vector JavaDoc treeModelListeners = new Vector JavaDoc();
33     /** The group root item, holds all other nodes. */
34     private AccountItem rootItem = null;
35
36
37     /**
38      * Overloaded constructor begins building the model from the root.
39      */

40     public GroupTreeModel() {
41         this.rootItem = new AccountItem("Accounts", 0);
42     }
43
44     /**
45      * Adds a listener for the TreeModelEvent posted after the tree changes.
46      *
47      * @param l - a tree model listener.
48      */

49     public void addTreeModelListener(TreeModelListener JavaDoc l) {
50         this.treeModelListeners.addElement(l);
51     }
52
53     /**
54      * Removes a listener previously added with addTreeModelListener().
55      *
56      * @param l - a tree model listener.
57      */

58     public void removeTreeModelListener(TreeModelListener JavaDoc l) {
59         this.treeModelListeners.removeElement(l);
60     }
61
62     /**
63      * This method returns the root of the tree.
64      *
65      * @return Object - root item.
66      */

67     public Object JavaDoc getRoot() {
68         return this.rootItem;
69     }
70
71     /**
72      * This method returns true if node is a leaf.
73      *
74      * @param node - the selected node.
75      * @return boolean - TRUE = node is a leaf.
76      * FALSE = node is not a leaf.
77      */

78     public boolean isLeaf(Object JavaDoc node) {
79         AccountItem item = (AccountItem) node;
80         return item.getChildCount() == 0;
81     }
82
83     /**
84      * This method returns the number of children of parent.
85      *
86      * @param parent - the parent of the child count.
87      * @return int - the number of children.
88      */

89     public int getChildCount(Object JavaDoc parent) {
90         AccountItem item = (AccountItem) parent;
91         return item.getChildCount();
92     }
93
94     /**
95      * This method returns the index of child in parent.
96      *
97      * @param parent - the parent to check connection to.
98      * @param child - the child to find conenction from.
99      * @return int - index of selected child.
100      */

101     public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
102         AccountItem item = (AccountItem) parent;
103         return item.getIndexOfChild((AccountItem) child);
104     }
105
106     /**
107      * Messaged when the account has altered the value for the item identified by
108      * path to newValue. Not used by this model.
109      *
110      * @param path - the path to the selected node.
111      * @param newValue - the value for the path node changed.
112      */

113     public void valueForPathChanged(TreePath JavaDoc path, Object JavaDoc newValue) {
114         System.out.println("*** valueForPathChanged : " + path + " --> " +
115                 newValue);
116     }
117
118     /**
119      * This method returns the child of parent at index index in the parent's
120      * child array.
121      *
122      * @param parent - the parent to search for a child.
123      * @param index - the desired children pointed by the parent.
124      * @return Object - parent's child.
125      */

126     public Object JavaDoc getChild(Object JavaDoc parent, int index) {
127         AccountItem item = (AccountItem) parent;
128         return item.getChildAt(index);
129     }
130
131     /**
132      * The only event raised by this model is TreeStructureChanged with the
133      * root as path, i.e. the whole tree has changed.
134      *
135      */

136     public void fireTreeStructureChanged() {
137         int len = this.treeModelListeners.size();
138         TreeModelEvent JavaDoc e = new TreeModelEvent JavaDoc(this, new Object JavaDoc[]{rootItem});
139
140         for (int i = 0; i < len; i++) {
141             ((TreeModelListener JavaDoc) this.treeModelListeners.elementAt(i)).
142                     treeStructureChanged(e);
143         }
144     }
145
146     /**
147      * This method returns a reference to the root item.
148      *
149      * @return AccountItem - the root item.
150      */

151     public AccountItem getRootItem() {
152         return this.rootItem;
153     }
154
155     /**
156      * This method clears the tree information.
157      */

158     public void clearGroups() {
159         //get rid of all children
160
this.rootItem.setChildren(null);
161     }
162
163 } //--------------------------------- E O F -----------------------------------
164

165
166
Popular Tags