KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jdepend > swingui > DependTreeModel


1 package jdepend.swingui;
2
3 import java.util.*;
4 import javax.swing.tree.*;
5 import javax.swing.event.*;
6
7 /**
8  * The <code>DependTreeModel</code> class defines the data model being
9  * observed by a <code>DependTree</code> instance.
10  *
11  * @author <b>Mike Clark</b>
12  * @author Clarkware Consulting, Inc.
13  */

14
15 public class DependTreeModel implements TreeModel {
16
17     private PackageNode root;
18
19     private Vector listeners;
20
21     /**
22      * Constructs a <code>DependTreeModel</code> with the specified root
23      * package node.
24      *
25      * @param root Root package node.
26      */

27     public DependTreeModel(PackageNode root) {
28         this.root = root;
29         listeners = new Vector();
30     }
31
32     /**
33      * Returns the root of the tree.
34      *
35      * @return The root of the tree, or <code>null</code> if the tree has no
36      * nodes.
37      */

38     public Object JavaDoc getRoot() {
39         return root;
40     }
41
42     /**
43      * Returns the child of the specified parent at the specified index in the
44      * parent's child collection.
45      * <p>
46      * The specified parent must be a node previously obtained from this data
47      * source.
48      *
49      * @param parent A node in the tree, obtained from this data source.
50      * @param index Index of child in the parent's child collection.
51      * @return Child.
52      */

53     public Object JavaDoc getChild(Object JavaDoc parent, int index) {
54
55         Object JavaDoc answer = null;
56         ArrayList children;
57
58         if (parent instanceof PackageNode) {
59             children = ((PackageNode) parent).getChildren();
60
61             if (children != null) {
62                 if (index < children.size()) {
63                     answer = children.get(index);
64                 }
65             }
66         }
67
68         return answer;
69     }
70
71     /**
72      * Returns the number of children for the specified parent.
73      * <p>
74      * The specified parent must be a node previously obtained from this data
75      * source.
76      *
77      * @param parent A node in the tree, obtained from this data source.
78      * @return The number of children of the specified parent, or 0 if the
79      * parent is a leaf node or if it has no children.
80      */

81     public int getChildCount(Object JavaDoc parent) {
82
83         int answer = 0;
84         ArrayList children;
85
86         if (parent instanceof PackageNode) {
87             children = ((PackageNode) parent).getChildren();
88
89             if (children != null) {
90                 answer = children.size();
91             }
92         }
93
94         return answer;
95     }
96
97     /**
98      * Determines whether the specified tree node is a leaf node.
99      *
100      * @param o A node in the tree, obtained from this data source.
101      * @return <code>true</code> if the node is a leaf; <code>false</code>
102      * otherwise.
103      */

104     public boolean isLeaf(Object JavaDoc o) {
105
106         boolean answer = true;
107
108         if (o instanceof PackageNode) {
109             PackageNode node = (PackageNode) o;
110             return node.isLeaf();
111         }
112
113         return answer;
114     }
115
116     /**
117      * Callback method triggered when the value for the item specified by
118      * <i>path </i> has changed to <i>newValue </i>.
119      *
120      * @param path Path to the node that has changed.
121      * @param newValue The new value of the node.
122      */

123     public void valueForPathChanged(TreePath path, Object JavaDoc newValue) {
124         // do nothing
125
}
126
127     /**
128      * Returns the index of the specified child within the specified parent.
129      *
130      * @param parent Parent node.
131      * @param child Child node.
132      * @return Index of child within parent.
133      */

134     public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
135         int answer = -1;
136         ArrayList children = null;
137
138         if (parent instanceof PackageNode) {
139             children = ((PackageNode) parent).getChildren();
140
141             if (children != null) {
142                 answer = children.indexOf(child);
143             }
144         }
145
146         return answer;
147     }
148
149     /**
150      * Adds a listener for the <code>TreeModelEvent</code> posted after the
151      * tree changes.
152      *
153      * @param l The listener to add.
154      */

155     public void addTreeModelListener(TreeModelListener l) {
156
157         if ((l != null) && !listeners.contains(l)) {
158             listeners.addElement(l);
159         }
160     }
161
162     /**
163      * Removes a listener for <code>TreeModelEvent</code>s.
164      *
165      * @param l The listener to remove.
166      */

167     public void removeTreeModelListener(TreeModelListener l) {
168         listeners.removeElement(l);
169     }
170 }
171
172
Popular Tags