KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > adwt > BeanContextTreeModel


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.adwt;
8
9 import java.beans.beancontext.BeanContext JavaDoc;
10 import java.beans.beancontext.BeanContextMembershipEvent JavaDoc;
11 import java.beans.beancontext.BeanContextMembershipListener JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 import javax.swing.event.TreeModelEvent JavaDoc;
18 import javax.swing.event.TreeModelListener JavaDoc;
19 import javax.swing.tree.TreePath JavaDoc;
20
21 import org.ejtools.adwt.util.DefaultObjectIndexer;
22 import org.ejtools.adwt.util.ObjectIndexer;
23 import org.ejtools.adwt.util.SearchableTreeModel;
24
25 /**
26  * Description of the Class
27  *
28  * @author Laurent Etiemble
29  * @version $Revision: 1.2 $
30  */

31 public class BeanContextTreeModel implements SearchableTreeModel, BeanContextMembershipListener JavaDoc
32 {
33    /** Description of the Field */
34    protected Collection JavaDoc contexts = new Vector JavaDoc();
35    /** Description of the Field */
36    protected ObjectIndexer indexer = new DefaultObjectIndexer();
37    /** Description of the Field */
38    protected Collection JavaDoc listeners = new Vector JavaDoc();
39    /** Root of the tree. */
40    protected BeanContext JavaDoc root;
41
42
43    /**
44     * Constructor for the BeanContextTreeModel object
45     *
46     * @param root Description of the Parameter
47     */

48    public BeanContextTreeModel(BeanContext JavaDoc root)
49    {
50       this.root = root;
51       this.root.addBeanContextMembershipListener(this);
52    }
53
54
55    /**
56     * Adds a feature to the TreeModelListener attribute of the
57     * BeanContextTreeModel object
58     *
59     * @param tml The feature to be added to the TreeModelListener attribute
60     */

61    public void addTreeModelListener(TreeModelListener JavaDoc tml)
62    {
63       this.listeners.add(tml);
64    }
65
66
67    /**
68     * Description of the Method
69     *
70     * @param bcme Description of the Parameter
71     */

72    public void childrenAdded(BeanContextMembershipEvent JavaDoc bcme)
73    {
74       for (Iterator JavaDoc it = bcme.iterator(); it.hasNext(); )
75       {
76          Object JavaDoc o = it.next();
77          if (o instanceof BeanContext JavaDoc)
78          {
79             BeanContext JavaDoc context = (BeanContext JavaDoc) o;
80             if (!contexts.contains(context))
81             {
82                contexts.add(context);
83                context.addBeanContextMembershipListener(this);
84             }
85          }
86          this.indexer.put(o, computePath(bcme.getBeanContext(), o));
87       }
88       this.fireTreeStructureChanged();
89    }
90
91
92    /**
93     * Description of the Method
94     *
95     * @param bcme Description of the Parameter
96     */

97    public void childrenRemoved(BeanContextMembershipEvent JavaDoc bcme)
98    {
99       for (Iterator JavaDoc it = bcme.iterator(); it.hasNext(); )
100       {
101          Object JavaDoc o = it.next();
102          if (o instanceof BeanContext JavaDoc)
103          {
104             BeanContext JavaDoc context = (BeanContext JavaDoc) o;
105             if (contexts.contains(context))
106             {
107                context.removeBeanContextMembershipListener(this);
108                contexts.remove(context);
109             }
110          }
111          this.indexer.remove(o);
112       }
113       this.fireTreeStructureChanged();
114    }
115
116
117    /**
118     * Description of the Method
119     *
120     * @param o Description of the Parameter
121     * @return Description of the Return Value
122     */

123    public Object JavaDoc find(Object JavaDoc o)
124    {
125       return this.indexer.get(o);
126    }
127
128
129    /**
130     * Gets the child attribute of the BeanContextTreeModel object
131     *
132     * @param parent Description of the Parameter
133     * @param index Description of the Parameter
134     * @return The child value
135     */

136    public Object JavaDoc getChild(Object JavaDoc parent, int index)
137    {
138       Collection JavaDoc c = (Collection JavaDoc) parent;
139       int i = 0;
140       Object JavaDoc result = null;
141       for (Iterator JavaDoc it = c.iterator(); it.hasNext(); i++)
142       {
143          result = it.next();
144          if (i == index)
145          {
146             break;
147          }
148          result = null;
149       }
150       return result;
151    }
152
153
154    /**
155     * Gets the childCount attribute of the BeanContextTreeModel object
156     *
157     * @param parent Description of the Parameter
158     * @return The childCount value
159     */

160    public int getChildCount(Object JavaDoc parent)
161    {
162       Collection JavaDoc c = (Collection JavaDoc) parent;
163       return c.size();
164    }
165
166
167    /**
168     * Gets the indexOfChild attribute of the BeanContextTreeModel object
169     *
170     * @param parent Description of the Parameter
171     * @param child Description of the Parameter
172     * @return The indexOfChild value
173     */

174    public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child)
175    {
176       Collection JavaDoc c = (Collection JavaDoc) parent;
177       int i = 0;
178       int index = -1;
179       for (Iterator JavaDoc it = c.iterator(); it.hasNext(); i++)
180       {
181          if (it.next().equals(child))
182          {
183             index = i;
184             break;
185          }
186       }
187       return index;
188    }
189
190
191    /**
192     * Gets the root attribute of the BeanContextTreeModel object
193     *
194     * @return The root value
195     */

196    public Object JavaDoc getRoot()
197    {
198       return this.root;
199    }
200
201
202    /**
203     * Gets the leaf attribute of the BeanContextTreeModel object
204     *
205     * @param node Description of the Parameter
206     * @return The leaf value
207     */

208    public boolean isLeaf(Object JavaDoc node)
209    {
210       Collection JavaDoc c = (Collection JavaDoc) node;
211       return (c.size() == 0);
212    }
213
214
215    /**
216     * Description of the Method
217     *
218     * @param tml Description of the Parameter
219     */

220    public void removeTreeModelListener(TreeModelListener JavaDoc tml)
221    {
222       this.listeners.remove(tml);
223    }
224
225
226    /**
227     * Sets the indexer attribute of the BeanContextTreeModel object
228     *
229     * @param indexer The new indexer value
230     */

231    public void setIndexer(ObjectIndexer indexer)
232    {
233       this.indexer = indexer;
234    }
235
236
237    /**
238     * Description of the Method
239     *
240     * @param path Description of the Parameter
241     * @param newValue Description of the Parameter
242     */

243    public void valueForPathChanged(TreePath JavaDoc path, Object JavaDoc newValue)
244    {
245       throw new UnsupportedOperationException JavaDoc("Not yet implemented");
246    }
247
248
249    /**
250     * Description of the Method
251     *
252     * @param parent Description of the Parameter
253     * @param o Description of the Parameter
254     * @return Description of the Return Value
255     */

256    protected Object JavaDoc[] computePath(BeanContext JavaDoc parent, Object JavaDoc o)
257    {
258       ArrayList JavaDoc paths = new ArrayList JavaDoc();
259       BeanContext JavaDoc current = parent;
260       paths.add(parent);
261       paths.add(o);
262       while (this.root.equals(current.getBeanContext()))
263       {
264          current = current.getBeanContext();
265          paths.add(0, current);
266       }
267       return paths.toArray();
268    }
269
270
271    /** Description of the Method */
272    protected void fireTreeNodesChanged()
273    {
274       TreeModelEvent JavaDoc event = new TreeModelEvent JavaDoc(this, new TreePath JavaDoc(this.root));
275       Object JavaDoc[] copy = this.listeners.toArray();
276       for (int i = 0; i < copy.length; i++)
277       {
278          TreeModelListener JavaDoc listener = (TreeModelListener JavaDoc) copy[i];
279          listener.treeNodesChanged(event);
280       }
281    }
282
283
284    /** Description of the Method */
285    protected void fireTreeNodesInserted()
286    {
287       TreeModelEvent JavaDoc event = new TreeModelEvent JavaDoc(this, new TreePath JavaDoc(this.root));
288       Object JavaDoc[] copy = this.listeners.toArray();
289       for (int i = 0; i < copy.length; i++)
290       {
291          TreeModelListener JavaDoc listener = (TreeModelListener JavaDoc) copy[i];
292          listener.treeNodesInserted(event);
293       }
294    }
295
296
297    /** Description of the Method */
298    protected void fireTreeNodesRemoved()
299    {
300       TreeModelEvent JavaDoc event = new TreeModelEvent JavaDoc(this, new TreePath JavaDoc(this.root));
301       Object JavaDoc[] copy = this.listeners.toArray();
302       for (int i = 0; i < copy.length; i++)
303       {
304          TreeModelListener JavaDoc listener = (TreeModelListener JavaDoc) copy[i];
305          listener.treeNodesRemoved(event);
306       }
307    }
308
309
310    /** Description of the Method */
311    protected void fireTreeStructureChanged()
312    {
313       TreeModelEvent JavaDoc event = new TreeModelEvent JavaDoc(this, new TreePath JavaDoc(this.root));
314       Object JavaDoc[] copy = this.listeners.toArray();
315       for (int i = 0; i < copy.length; i++)
316       {
317          TreeModelListener JavaDoc listener = (TreeModelListener JavaDoc) copy[i];
318          listener.treeStructureChanged(event);
319       }
320    }
321 }
322
Popular Tags