KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > treetable > DefaultTreeTableModel


1 /*
2  * $Id: DefaultTreeTableModel.java,v 1.2 2004/07/28 21:21:15 aim Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.treetable;
9
10 import javax.swing.event.TreeModelEvent JavaDoc;
11 import javax.swing.event.TreeModelListener JavaDoc;
12 import javax.swing.tree.TreeNode JavaDoc;
13 import javax.swing.tree.TreePath JavaDoc;
14
15 /**
16  * DefaultTreeTableModel is a concrete implementation of <code>AbstractTreeTableModel</code>
17  * and is provided purely as a convenience. Applications that use <code>JXTreeTable</code>
18  * are expected to provide their own implementation of a <code>TreeTableModel</code>,
19  * perhaps by extending this class.
20  *
21  * @author Ramesh Gupta
22  */

23 public class DefaultTreeTableModel extends AbstractTreeTableModel {
24
25     protected boolean asksAllowsChildren;
26
27     public DefaultTreeTableModel() {
28         this(null);
29     }
30
31     public DefaultTreeTableModel(TreeNode JavaDoc root) {
32         this(root, false);
33     }
34
35     public DefaultTreeTableModel(TreeNode JavaDoc root, boolean asksAllowsChildren) {
36         super(root);
37         this.asksAllowsChildren = asksAllowsChildren;
38     }
39
40     public void setRoot(TreeNode JavaDoc root) {
41         Object JavaDoc oldRoot = this.root;
42         this.root = root;
43         if (root == null && oldRoot != null) {
44             fireTreeStructureChanged(this, null);
45         }
46         else {
47             nodeStructureChanged(root);
48         }
49     }
50
51     /*
52      * Notifies all listeners that have registered interest for
53      * notification on this event type. The event instance
54      * is lazily created using the parameters passed into
55      * the fire method.
56      *
57      * @param source the node where the tree model has changed
58      * @param path the path to the root node
59      * @see EventListenerList
60      */

61     private void fireTreeStructureChanged(Object JavaDoc source, TreePath JavaDoc path) {
62         // Guaranteed to return a non-null array
63
Object JavaDoc[] listeners = listenerList.getListenerList();
64         TreeModelEvent JavaDoc e = null;
65         // Process the listeners last to first, notifying
66
// those that are interested in this event
67
for (int i = listeners.length - 2; i >= 0; i -= 2) {
68             if (listeners[i] == TreeModelListener JavaDoc.class) {
69                 // Lazily create the event:
70
if (e == null)
71                     e = new TreeModelEvent JavaDoc(source, path);
72                 ((TreeModelListener JavaDoc) listeners[i + 1]).treeStructureChanged(e);
73             }
74         }
75     }
76
77     public boolean asksAllowsChildren() {
78         return asksAllowsChildren;
79     }
80
81     public void setAsksAllowsChildren(boolean newValue) {
82         asksAllowsChildren = newValue;
83     }
84
85     public Object JavaDoc getValueAt(Object JavaDoc node, int column) {
86         /**@todo Implement this org.jdesktopx.swing.treetable.TreeTableModel abstract method*/
87         return node + "@column " + column;
88     }
89
90     public void setValueAt(Object JavaDoc value, Object JavaDoc node, int column) {
91         /**@todo Implement this org.jdesktopx.swing.treetable.TreeTableModel abstract method*/
92     }
93
94     public TreeNode JavaDoc[] getPathToRoot(TreeNode JavaDoc node) {
95         return getPathToRoot(node, 0);
96     }
97
98     protected TreeNode JavaDoc[] getPathToRoot(TreeNode JavaDoc node, int depth) {
99         TreeNode JavaDoc[] retNodes;
100         // This method recurses, traversing towards the root in order
101
// size the array. On the way back, it fills in the nodes,
102
// starting from the root and working back to the original node.
103

104         /* Check for null, in case someone passed in a null node, or
105            they passed in an element that isn't rooted at root. */

106         if (node == null) {
107             if (depth == 0)
108                 return null;
109             else
110                 retNodes = new TreeNode JavaDoc[depth];
111         }
112         else {
113             depth++;
114             if (node == root)
115                 retNodes = new TreeNode JavaDoc[depth];
116             else
117                 retNodes = getPathToRoot(node.getParent(), depth);
118             retNodes[retNodes.length - depth] = node;
119         }
120         return retNodes;
121     }
122
123     /**
124      * @param node
125      * @return true if the specified node is a leaf node; false otherwise
126      */

127     public boolean isLeaf(Object JavaDoc node) {
128         if (node instanceof TreeNode JavaDoc) {
129             if (asksAllowsChildren) {
130                 return!((TreeNode JavaDoc) node).getAllowsChildren();
131             }
132         }
133         return super.isLeaf(node);
134     }
135
136     public void reload() {
137         TreeNode JavaDoc treeNode;
138         try {
139             treeNode = (TreeNode JavaDoc) root;
140         }
141         catch (ClassCastException JavaDoc ex) {
142             return;
143         }
144
145         reload(treeNode);
146     }
147
148     public void reload(TreeNode JavaDoc node) {
149         if (node != null) {
150             fireTreeStructureChanged(this, getPathToRoot(node), null, null);
151         }
152     }
153
154     /**
155      * Invoke this method after you've inserted some TreeNodes into
156      * node. childIndices should be the index of the new elements and
157      * must be sorted in ascending order.
158      */

159     public void nodesWereInserted(TreeNode JavaDoc node, int[] childIndices) {
160         if (listenerList != null && node != null && childIndices != null
161             && childIndices.length > 0) {
162             int cCount = childIndices.length;
163             Object JavaDoc[] newChildren = new Object JavaDoc[cCount];
164
165             for (int counter = 0; counter < cCount; counter++)
166                 newChildren[counter] = node.getChildAt(childIndices[counter]);
167             fireTreeNodesInserted(this, getPathToRoot(node), childIndices,
168                                   newChildren);
169         }
170     }
171
172     /**
173      * Invoke this method after you've removed some TreeNodes from
174      * node. childIndices should be the index of the removed elements and
175      * must be sorted in ascending order. And removedChildren should be
176      * the array of the children objects that were removed.
177      */

178     public void nodesWereRemoved(TreeNode JavaDoc node, int[] childIndices,
179                                  Object JavaDoc[] removedChildren) {
180         if (node != null && childIndices != null) {
181             fireTreeNodesRemoved(this, getPathToRoot(node), childIndices,
182                                  removedChildren);
183         }
184     }
185
186     /**
187      * Invoke this method after you've changed how the children identified by
188      * childIndicies are to be represented in the tree.
189      */

190     public void nodesChanged(TreeNode JavaDoc node, int[] childIndices) {
191         if (node != null) {
192             if (childIndices != null) {
193                 int cCount = childIndices.length;
194
195                 if (cCount > 0) {
196                     Object JavaDoc[] cChildren = new Object JavaDoc[cCount];
197
198                     for (int counter = 0; counter < cCount; counter++)
199                         cChildren[counter] = node.getChildAt
200                             (childIndices[counter]);
201                     fireTreeNodesChanged(this, getPathToRoot(node),
202                                          childIndices, cChildren);
203                 }
204             }
205             else if (node == getRoot()) {
206                 fireTreeNodesChanged(this, getPathToRoot(node), null, null);
207             }
208         }
209     }
210
211     /**
212      * Invoke this method if you've totally changed the children of
213      * node and its childrens children... This will post a
214      * treeStructureChanged event.
215      */

216     public void nodeStructureChanged(TreeNode JavaDoc node) {
217         if (node != null) {
218             fireTreeStructureChanged(this, getPathToRoot(node), null, null);
219         }
220     }
221 }
Popular Tags