KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > tree > model > DefaultTreeModel


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.tree.model;
17
18 import org.apache.myfaces.custom.tree.DefaultMutableTreeNode;
19 import org.apache.myfaces.custom.tree.MutableTreeNode;
20 import org.apache.myfaces.custom.tree.TreeNode;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.LinkedList JavaDoc;
25
26
27 /**
28  * @author <a HREF="mailto:oliver@rossmueller.com">Oliver Rossmueller</a>
29  * @version $Revision: 1.7 $ $Date: 2004/10/13 11:50:58 $
30  * $Log: DefaultTreeModel.java,v $
31  * Revision 1.7 2004/10/13 11:50:58 matze
32  * renamed packages to org.apache
33  *
34  * Revision 1.6 2004/09/01 18:32:57 mwessendorf
35  * Organize Imports
36  *
37  * Revision 1.5 2004/08/15 15:28:04 o_rossmueller
38  * new model listener handling to get modified from events which occur outside the scope of a tree request
39  *
40  * Revision 1.4 2004/07/01 21:53:04 mwessendorf
41  * ASF switch
42  *
43  * Revision 1.3 2004/05/05 00:18:56 o_rossmueller
44  * various fixes/modifications in model event handling and tree update
45  *
46  * Revision 1.2 2004/05/04 00:28:17 o_rossmueller
47  * model event handling
48  *
49  * Revision 1.1 2004/04/22 10:20:24 manolito
50  * tree component
51  *
52  */

53 public class DefaultTreeModel
54         implements TreeModel
55 {
56
57     private TreeNode root;
58     private LinkedList JavaDoc listeners = new LinkedList JavaDoc();
59
60
61     public DefaultTreeModel()
62     {
63         this(new DefaultMutableTreeNode("Root"));
64     }
65
66
67     public DefaultTreeModel(TreeNode root)
68     {
69         this.root = root;
70     }
71
72
73     public Object JavaDoc getRoot()
74     {
75         return root;
76     }
77
78
79     public Object JavaDoc getChild(Object JavaDoc parent, int index)
80     {
81         return ((TreeNode)parent).getChildAt(index);
82     }
83
84
85     public int getChildCount(Object JavaDoc parent)
86     {
87         return ((TreeNode)parent).getChildCount();
88     }
89
90
91     public boolean isLeaf(Object JavaDoc node)
92     {
93         return ((TreeNode)node).isLeaf();
94     }
95
96
97     public void valueForPathChanged(TreePath path, Object JavaDoc newValue)
98     {
99         MutableTreeNode node = (MutableTreeNode)path.getLastPathComponent();
100
101         node.setUserObject(newValue);
102     }
103
104
105     public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child)
106     {
107         return ((TreeNode)parent).getIndex((TreeNode)child);
108     }
109
110
111     public Collection JavaDoc getTreeModelListeners()
112     {
113         return listeners;
114     }
115
116
117     /**
118      * Invoke this method after you've changed how node is to be
119      * represented in the tree.
120      */

121     public void nodeChanged(TreeNode node)
122     {
123         if (listeners.isEmpty())
124         {
125             // nobody cares
126
return;
127         }
128
129         if (node != null)
130         {
131             TreeNode parent = node.getParent();
132
133             if (parent != null)
134             {
135                 int index = parent.getIndex(node);
136                 if (index != -1)
137                 {
138                     int[] childIndices = new int[1];
139
140                     childIndices[0] = index;
141                     nodesChanged(parent, childIndices);
142                 }
143             }
144             else if (node == getRoot())
145             {
146                 nodesChanged(node, null);
147             }
148         }
149     }
150
151
152     /**
153      * Invoke this method after you've changed how the children identified by
154      * childIndicies are to be represented in the tree.
155      */

156     public void nodesChanged(TreeNode node, int[] childIndices)
157     {
158         if (listeners.isEmpty())
159         {
160             // nobody cares
161
return;
162         }
163
164         if (node != null)
165         {
166             if (childIndices != null)
167             {
168                 int count = childIndices.length;
169
170                 if (count > 0)
171                 {
172                     Object JavaDoc[] children = new Object JavaDoc[count];
173
174                     for (int i = 0; i < count; i++)
175                     {
176                         children[i] = node.getChildAt(childIndices[i]);
177                     }
178                     fireTreeNodesChanged(this, getPathToRoot(node), childIndices, children);
179                 }
180             }
181             else if (node == root)
182             {
183                 fireTreeNodesChanged(this, getPathToRoot(node), null, null);
184             }
185         }
186     }
187
188
189     /**
190      * Invoke this method if you've totally changed the children of
191      * node and its childrens children... This will post a
192      * treeStructureChanged event.
193      */

194     public void nodeStructureChanged(TreeNode node)
195     {
196         if (listeners.isEmpty())
197         {
198             // nobody cares
199
return;
200         }
201
202         if (node != null)
203         {
204             fireTreeStructureChanged(this, getPathToRoot(node), null, null);
205         }
206     }
207
208
209     /**
210      * Invoke this method after you've inserted some TreeNodes into
211      * node. childIndices should be the index of the new elements and
212      * must be sorted in ascending order.
213      */

214     public void nodesWereInserted(TreeNode node, int[] childIndices)
215     {
216         if (listeners.isEmpty())
217         {
218             // nobody cares
219
return;
220         }
221         if (node != null && childIndices != null && childIndices.length > 0)
222         {
223             int cCount = childIndices.length;
224             Object JavaDoc[] newChildren = new Object JavaDoc[cCount];
225
226             for (int counter = 0; counter < cCount; counter++)
227             {
228                 newChildren[counter] = node.getChildAt(childIndices[counter]);
229             }
230             fireTreeNodesInserted(this, getPathToRoot(node), childIndices,
231                                   newChildren);
232         }
233     }
234
235
236     /**
237      * Invoke this method after you've removed some TreeNodes from
238      * node. childIndices should be the index of the removed elements and
239      * must be sorted in ascending order. And removedChildren should be
240      * the array of the children objects that were removed.
241      */

242     public void nodesWereRemoved(TreeNode node, int[] childIndices, Object JavaDoc[] removedChildren)
243     {
244         if (listeners.isEmpty())
245         {
246             // nobody cares
247
return;
248         }
249         if (node != null && childIndices != null)
250         {
251             fireTreeNodesRemoved(this, getPathToRoot(node), childIndices, removedChildren);
252         }
253     }
254
255
256     /**
257      * Collect all parent nodes up to the root node.
258      *
259      * @param node the TreeNode to get the path for
260      */

261     public TreeNode[] getPathToRoot(TreeNode node)
262     {
263         return getPathToRoot(node, 0);
264     }
265
266
267     /**
268      * Recursivly collect parent nodes up the the root node.
269      *
270      * @param node the TreeNode to get the path for
271      * @param depth number of steps already taken towards the root (on recursive calls)
272      * @return an array giving the path from the root to the specified node
273      */

274     protected TreeNode[] getPathToRoot(TreeNode node, int depth)
275     {
276         TreeNode[] answer;
277
278         if (node == null)
279         {
280             if (depth == 0)
281             {
282                 // nothing to do
283
return null;
284             }
285             else
286             {
287                 // end recursion
288
answer = new TreeNode[depth];
289             }
290         }
291         else
292         {
293             // recurse
294
depth++;
295
296             if (node == root)
297             {
298                 // we found the root node
299
answer = new TreeNode[depth];
300             }
301             else
302             {
303                 answer = getPathToRoot(node.getParent(), depth);
304             }
305             answer[answer.length - depth] = node;
306         }
307         return answer;
308     }
309
310
311     /**
312      * Notify all listeners of a node change.
313      *
314      * @param source the node being changed
315      * @param path the path to the root node
316      * @param childIndices the indices of the changed elements
317      * @param children the changed elements
318      */

319     protected void fireTreeNodesChanged(Object JavaDoc source, Object JavaDoc[] path, int[] childIndices, Object JavaDoc[] children)
320     {
321         TreeModelEvent event = null;
322         for (Iterator JavaDoc iterator = listeners.iterator(); iterator.hasNext();)
323         {
324             TreeModelListener listener = (TreeModelListener)iterator.next();
325             if (event == null)
326             {
327                 event = new TreeModelEvent(source, path, childIndices, children);
328             }
329             listener.treeNodesChanged(event);
330         }
331     }
332
333
334     /**
335      * Notify all listeners of structure change.
336      *
337      * @param source the node where new elements are being inserted
338      * @param path the path to the root node
339      * @param childIndices the indices of the new elements
340      * @param children the new elements
341      */

342     protected void fireTreeNodesInserted(Object JavaDoc source, Object JavaDoc[] path, int[] childIndices, Object JavaDoc[] children)
343     {
344         TreeModelEvent event = null;
345         for (Iterator JavaDoc iterator = listeners.iterator(); iterator.hasNext();)
346         {
347             TreeModelListener listener = (TreeModelListener)iterator.next();
348             if (event == null)
349             {
350                 event = new TreeModelEvent(source, path, childIndices, children);
351             }
352             listener.treeNodesInserted(event);
353         }
354     }
355
356
357     /**
358      * Notify all listeners of structure change.
359      *
360      * @param source the node where elements are being removed
361      * @param path the path to the root node
362      * @param childIndices the indices of the removed elements
363      * @param children the removed elements
364      */

365     protected void fireTreeNodesRemoved(Object JavaDoc source, Object JavaDoc[] path, int[] childIndices, Object JavaDoc[] children)
366     {
367         TreeModelEvent event = null;
368         for (Iterator JavaDoc iterator = listeners.iterator(); iterator.hasNext();)
369         {
370             TreeModelListener listener = (TreeModelListener)iterator.next();
371             if (event == null)
372             {
373                 event = new TreeModelEvent(source, path, childIndices, children);
374             }
375             listener.treeNodesRemoved(event);
376         }
377     }
378
379
380     /**
381      * Notify all listeners of structure change.
382      *
383      * @param source the node where the tree model has changed
384      * @param path the path to the root node
385      * @param childIndices the indices of the affected elements
386      * @param children the affected elements
387      */

388     protected void fireTreeStructureChanged(Object JavaDoc source, Object JavaDoc[] path, int[] childIndices, Object JavaDoc[] children)
389     {
390         TreeModelEvent event = null;
391         for (Iterator JavaDoc iterator = listeners.iterator(); iterator.hasNext();)
392         {
393             TreeModelListener listener = (TreeModelListener)iterator.next();
394             if (event == null)
395             {
396                 event = new TreeModelEvent(source, path, childIndices, children);
397             }
398             listener.treeStructureChanged(event);
399         }
400     }
401
402
403     /**
404      * Notify all listeners of structure change.
405      *
406      * @param source the node where the tree model has changed
407      * @param path the path to the root node
408      */

409     protected void fireTreeStructureChanged(Object JavaDoc source, TreePath path)
410     {
411         TreeModelEvent event = null;
412         for (Iterator JavaDoc iterator = listeners.iterator(); iterator.hasNext();)
413         {
414             TreeModelListener listener = (TreeModelListener)iterator.next();
415             if (event == null)
416             {
417                 event = new TreeModelEvent(source, path);
418             }
419             listener.treeStructureChanged(event);
420         }
421     }
422
423 }
424
Popular Tags