KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > JXTree


1 /*
2  * $Id: JXTree.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 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;
9
10 import java.lang.reflect.Method JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.util.Vector JavaDoc;
13
14 import java.awt.event.ActionEvent JavaDoc;
15
16 import javax.swing.ActionMap JavaDoc;
17 import javax.swing.JTree JavaDoc;
18 import javax.swing.tree.TreeModel JavaDoc;
19 import javax.swing.tree.TreeNode JavaDoc;
20 import javax.swing.tree.TreePath JavaDoc;
21
22 import org.jdesktop.swing.decorator.ComponentAdapter;
23 import org.jdesktop.swing.decorator.FilterPipeline;
24 import org.jdesktop.swing.decorator.HighlighterPipeline;
25
26 /**
27  * JXTree.
28  *
29  * @author Ramesh Gupta
30  */

31 public class JXTree extends JTree JavaDoc {
32     /** @todo Highlighters */
33     private Method JavaDoc conversionMethod = null;
34     private final static Class JavaDoc[] methodSignature = new Class JavaDoc[] {Object JavaDoc.class};
35     private final static Object JavaDoc[] methodArgs = new Object JavaDoc[] {null};
36
37     protected FilterPipeline filters = null;
38     protected HighlighterPipeline highlighters = null;
39     /**
40      * Constructs a <code>JXTree</code> with a sample model. The default model
41      * used by this tree defines a leaf node as any node without children.
42      */

43     public JXTree() {
44     initActions();
45     }
46
47     /**
48      * Constructs a <code>JXTree</code> with each element of the specified array
49      * as the child of a new root node which is not displayed. By default, this
50      * tree defines a leaf node as any node without children.
51      *
52      * This version of the constructor simply invokes the super class version
53      * with the same arguments.
54      *
55      * @param value an array of objects that are children of the root.
56      */

57     public JXTree(Object JavaDoc[] value) {
58         super(value);
59     initActions();
60     }
61
62     /**
63      * Constructs a <code>JXTree</code> with each element of the specified
64      * Vector as the child of a new root node which is not displayed.
65      * By default, this tree defines a leaf node as any node without children.
66      *
67      * This version of the constructor simply invokes the super class version
68      * with the same arguments.
69      *
70      * @param value an Vector of objects that are children of the root.
71      */

72     public JXTree(Vector JavaDoc value) {
73         super(value);
74     initActions();
75     }
76
77     /**
78      * Constructs a <code>JXTree</code> created from a Hashtable which does not
79      * display with root. Each value-half of the key/value pairs in the HashTable
80      * becomes a child of the new root node. By default, the tree defines a leaf
81      * node as any node without children.
82      *
83      * This version of the constructor simply invokes the super class version
84      * with the same arguments.
85      *
86      * @param value a Hashtable containing objects that are children of the root.
87      */

88     public JXTree(Hashtable JavaDoc value) {
89         super(value);
90     initActions();
91     }
92
93     /**
94      * Constructs a <code>JXTree</code> with the specified TreeNode as its root,
95      * which displays the root node. By default, the tree defines a leaf node as
96      * any node without children.
97      *
98      * This version of the constructor simply invokes the super class version
99      * with the same arguments.
100      *
101      * @param root root node of this tree
102      */

103     public JXTree(TreeNode JavaDoc root) {
104         super(root, false);
105     }
106
107     /**
108      * Constructs a <code>JXTree</code> with the specified TreeNode as its root,
109      * which displays the root node and which decides whether a node is a leaf
110      * node in the specified manner.
111      *
112      * This version of the constructor simply invokes the super class version
113      * with the same arguments.
114      *
115      * @param root root node of this tree
116      * @param asksAllowsChildren if true, only nodes that do not allow children
117      * are leaf nodes; otherwise, any node without children is a leaf node;
118      * @see javax.swing.tree.DefaultTreeModel#asksAllowsChildren
119      */

120     public JXTree(TreeNode JavaDoc root, boolean asksAllowsChildren) {
121         super(root, asksAllowsChildren);
122         initActions();
123     }
124
125     /**
126      * Constructs an instance of <code>JXTree</code> which displays the root
127      * node -- the tree is created using the specified data model.
128      *
129      * This version of the constructor simply invokes the super class version
130      * with the same arguments.
131      *
132      * @param newModel the <code>TreeModel</code> to use as the data model
133      */

134     public JXTree(TreeModel JavaDoc newModel) {
135         super(newModel);
136         initActions();
137         // To support delegation of convertValueToText() to the model...
138
conversionMethod = getValueConversionMethod(newModel);
139     }
140
141     public void setModel(TreeModel JavaDoc newModel) {
142         super.setModel(newModel);
143         // To support delegation of convertValueToText() to the model...
144
conversionMethod = getValueConversionMethod(newModel);
145     }
146
147     private Method JavaDoc getValueConversionMethod(TreeModel JavaDoc model) {
148         try {
149             return model.getClass().getMethod("convertValueToText", methodSignature);
150         }
151         catch (NoSuchMethodException JavaDoc ex) {
152             // not an error
153
}
154         return null;
155     }
156
157     public String JavaDoc convertValueToText(Object JavaDoc value, boolean selected,
158                                      boolean expanded, boolean leaf,
159                                      int row, boolean hasFocus) {
160         // Delegate to model, if possible. Otherwise fall back to superclass...
161

162         if(value != null) {
163             if (conversionMethod == null) {
164                 return value.toString();
165             }
166             else {
167                 try {
168                     methodArgs[0] = value;
169                     return (String JavaDoc) conversionMethod.invoke(getModel(), methodArgs);
170                 }
171                 catch (Exception JavaDoc ex) {
172                     // fall through
173
}
174             }
175         }
176         return "";
177     }
178
179     private void initActions() {
180         // Register the actions that this class can handle.
181
ActionMap JavaDoc map = getActionMap();
182         map.put("expand-all", new Actions("expand-all"));
183         map.put("collapse-all", new Actions("collapse-all"));
184     }
185
186     /**
187      * A small class which dispatches actions.
188      * TODO: Is there a way that we can make this static?
189      */

190     private class Actions extends UIAction {
191         Actions(String JavaDoc name) {
192             super(name);
193         }
194
195         public void actionPerformed(ActionEvent JavaDoc evt) {
196             if ("expand-all".equals(getName())) {
197         expandAll();
198             }
199             else if ("collapse-all".equals(getName())) {
200                 collapseAll();
201             }
202         }
203     }
204
205
206     /**
207      * Collapses all nodes in the tree table.
208      */

209     public void collapseAll() {
210         for (int i = getRowCount() - 1; i >= 0 ; i--) {
211             collapseRow(i);
212         }
213     }
214
215     /**
216      * Expands all nodes in the tree table.
217      */

218     public void expandAll() {
219         for (int i = 0; i < getRowCount(); i++) {
220             expandRow(i);
221         }
222     }
223
224     public FilterPipeline getFilters() {
225         return filters;
226     }
227
228     public void setFilters(FilterPipeline pipeline) {
229         /** @todo */
230         //TreeModel model = getModel();
231
//adjustListeners(pipeline, model, model);
232
filters = pipeline;
233     }
234
235     public HighlighterPipeline getHighlighters() {
236         return highlighters;
237     }
238
239     public void setHighlighters(HighlighterPipeline pipeline) {
240         highlighters = pipeline;
241     }
242
243     protected ComponentAdapter getComponentAdapter() {
244         return dataAdapter;
245     }
246
247     private final ComponentAdapter dataAdapter = new TreeAdapter(this);
248
249     static class TreeAdapter extends ComponentAdapter {
250         private final JTree JavaDoc tree;
251         private TreePath JavaDoc path;
252         // JTreeRenderContext requires cooperation from JTree, which must set path,
253
// and call getRowForPath() to set the row in this context at appropriate times.
254

255         /**
256          * Constructs a <code>TableCellRenderContext</code> for the specified
257          * target component.
258          *
259          * @param component the target component
260          */

261         public TreeAdapter(JTree JavaDoc component) {
262             super(component);
263             tree = component;
264         }
265         public JTree JavaDoc getTree() {
266             return tree;
267         }
268
269         public boolean hasFocus() {
270             return tree.isFocusOwner() && (tree.getLeadSelectionRow() == row);
271         }
272
273         public Object JavaDoc getValueAt(int row, int column) {
274             return path.getLastPathComponent();
275         }
276
277         public Object JavaDoc getFilteredValueAt(int row, int column) {
278             /** @todo Implement filtering */
279             return path.getLastPathComponent();
280         }
281
282         public boolean isSelected() {
283             return tree.isRowSelected(row);
284         }
285
286         public boolean isExpanded() {
287             return tree.isExpanded(row);
288         }
289
290         public boolean isLeaf() {
291             return tree.getModel().isLeaf(getValue());
292         }
293
294         public boolean isCellEditable(int row, int column) {
295             return false; /** @todo */
296         }
297
298         public void setValueAt(Object JavaDoc aValue, int row, int column) {
299             /** @todo */
300         }
301     }
302
303 }
304
Popular Tags