KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Component JavaDoc;
10
11 import javax.swing.Icon JavaDoc;
12 import javax.swing.JTree JavaDoc;
13 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
14 import javax.swing.tree.TreePath JavaDoc;
15
16 import org.ejtools.adwt.util.DefaultObjectRenderer;
17 import org.ejtools.adwt.util.ObjectIndexer;
18 import org.ejtools.adwt.util.ObjectRenderer;
19 import org.ejtools.adwt.util.ObjectSearcher;
20 import org.ejtools.adwt.util.SearchableTreeModel;
21
22 /**
23  * Description of the Class
24  *
25  * @author Laurent Etiemble
26  * @version $Revision: 1.2 $
27  */

28 public class BeanContextTree extends JTree JavaDoc implements ObjectSearcher
29 {
30    /** Description of the Field */
31    protected ObjectRenderer renderer = new DefaultObjectRenderer();
32
33
34    /**
35     * Constructor for the BeanContextTree object
36     *
37     * @param newModel Description of the Parameter
38     */

39    public BeanContextTree(SearchableTreeModel newModel)
40    {
41       super(newModel);
42
43       this.setShowsRootHandles(true);
44       this.setExpandsSelectedPaths(true);
45       this.putClientProperty("JTree.lineStyle", "Angled");
46
47       this.setCellRenderer(
48          new DefaultTreeCellRenderer JavaDoc()
49          {
50             public Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc jtree, Object JavaDoc obj, boolean flag, boolean flag1, boolean flag2, int i, boolean flag3)
51             {
52                try
53                {
54                   Icon JavaDoc leafIcon = null;
55                   Icon JavaDoc openIcon = null;
56                   Icon JavaDoc closedIcon = null;
57
58                   Icon JavaDoc newIcon = BeanContextTree.this.renderer.getIcon(obj);
59
60                   if (newIcon != null)
61                   {
62                      leafIcon = this.getLeafIcon();
63                      openIcon = this.getOpenIcon();
64                      closedIcon = this.getClosedIcon();
65                      this.setLeafIcon(newIcon);
66                      this.setOpenIcon(newIcon);
67                      this.setClosedIcon(newIcon);
68                   }
69
70                   super.getTreeCellRendererComponent(jtree, obj, flag, flag1, flag2, i, flag3);
71
72                   if (newIcon != null)
73                   {
74                      this.setLeafIcon(leafIcon);
75                      this.setOpenIcon(openIcon);
76                      this.setClosedIcon(closedIcon);
77                   }
78
79                   return this;
80                }
81                catch (Exception JavaDoc e)
82                {
83                   return super.getTreeCellRendererComponent(jtree, obj, flag, flag1, flag2, i, flag3);
84                }
85             }
86          });
87    }
88
89
90    /**
91     * Description of the Method
92     *
93     * @param o Description of the Parameter
94     * @return Description of the Return Value
95     */

96    public Object JavaDoc find(Object JavaDoc o)
97    {
98       Object JavaDoc r = ((SearchableTreeModel) this.getModel()).find(o);
99       if (r != null)
100       {
101          Object JavaDoc[] paths = (Object JavaDoc[]) r;
102          TreePath JavaDoc path = new TreePath JavaDoc(paths);
103          this.setSelectionPath(path);
104       }
105       return null;
106    }
107
108
109    /** Description of the Method */
110    public void selectRoot()
111    {
112       TreePath JavaDoc path = new TreePath JavaDoc(this.getModel().getRoot());
113       this.setSelectionPath(path);
114    }
115
116
117    /**
118     * Sets the indexer attribute of the BeanContextTree object
119     *
120     * @param indexer The new indexer value
121     */

122    public void setIndexer(ObjectIndexer indexer)
123    {
124       ((SearchableTreeModel) this.getModel()).setIndexer(indexer);
125    }
126
127
128    /**
129     * Sets the renderer attribute of the BeanContextTree object
130     *
131     * @param renderer The new renderer value
132     */

133    public void setRenderer(ObjectRenderer renderer)
134    {
135       this.renderer = renderer;
136    }
137 }
138
Popular Tags