KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > console > ObjectTreeModel


1 package org.sape.carbon.services.console;
2
3 import java.beans.BeanInfo JavaDoc;
4 import java.beans.Introspector JavaDoc;
5 import java.beans.PropertyDescriptor JavaDoc;
6 import java.lang.reflect.Array JavaDoc;
7 import java.lang.reflect.Method JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Arrays JavaDoc;
10 import java.util.List JavaDoc;
11
12 import org.sape.carbon.core.config.Configuration;
13 import org.sape.carbon.core.config.type.ConfigurationTypeService;
14 import org.sape.carbon.core.config.type.ConfigurationTypeServiceFactory;
15 import org.sape.carbon.core.exception.ExceptionUtility;
16 import org.sape.carbon.services.swing.treetable.AbstractTreeTableModel;
17 import org.sape.carbon.services.swing.treetable.TreeTableModel;
18
19 /**
20  * <p>This is the template for Classes.</p>
21  *
22  *
23  * @since carbon 1.0
24  * @author Greg Hinkle, January 2002
25  * @version $Revision: 1.10 $($Author: ghinkl $ / $Date: 2003/04/04 01:11:49 $)
26  * <br>Copyright 2002 Sapient
27  */

28 public class ObjectTreeModel extends AbstractTreeTableModel {
29     private static String JavaDoc[] COLUMN_NAMES =
30         new String JavaDoc[] { "Attribute", "Value", "Editer" };
31
32     private Configuration config;
33     private TreeNode rootNode;
34
35     public ObjectTreeModel(Configuration config) {
36         super(new ObjectTreeModel.ConfigNode(config));
37         this.rootNode = (TreeNode)super.root;
38         //this.rootNode = ;
39
this.config = config;
40
41         fireTreeStructureChanged(this,null, null, null);
42     }
43
44     public Object JavaDoc getChild(Object JavaDoc obj, int param) {
45 // System.out.println("Getting child on: " + obj);
46
return getChildren(obj)[param];
47     }
48
49     public int getChildCount(Object JavaDoc obj) {
50         Object JavaDoc[] arr = getChildren(obj);
51         if (arr != null)
52             return arr.length;
53         else
54             return 0;
55     }
56
57     public Object JavaDoc[] getChildren(Object JavaDoc obj) {
58         return ((TreeNode)obj).getChildren();
59     }
60
61     /**
62      * Returns the number ofs availible column.
63      */

64     public int getColumnCount() {
65         return COLUMN_NAMES.length;
66     }
67
68     /**
69      * Returns the name for column number <code>column</code>.
70      */

71     public String JavaDoc getColumnName(int column) {
72         return COLUMN_NAMES[column];
73     }
74
75     /**
76      * Returns the value to be displayed for node <code>node</code>,
77      * at column number <code>column</code>.
78      */

79     public Object JavaDoc getValueAt(Object JavaDoc node, int column) {
80 // System.out.println("This Type is " +node.getClass());
81
switch (column) {
82             case 0:
83                 if (node instanceof TreeNode) {
84                     return node;
85                 } else {
86                     return "";
87                 }
88             case 1:
89                 if (node instanceof ArrayNode) {
90                     return node; //(new JButton("New..."));
91
} else if (node instanceof TreeNode) {
92                     return ((TreeNode)node).getValue();
93                 } else {
94                     return "";
95                 }
96             case 2:
97                 Class JavaDoc type = ((TreeNode)node).getType();
98                 if (type != null) {
99                     String JavaDoc name = type.getName();
100                     return name.substring(name.lastIndexOf('.')+1);
101                 } else {
102                     return null;
103                 }
104             default:
105                 return null;
106         }
107     }
108
109
110     public boolean isCellEditable(Object JavaDoc node, int column) {
111
112         switch (column) {
113             case 0:
114                 return true;
115             case 1:
116                 return ((TreeNode)node).isEditable();
117             default:
118                 return false;
119         }
120
121     }
122
123
124
125     public Class JavaDoc getColumnClass(int column) {
126         switch (column) {
127             case 0:
128                 return TreeTableModel.class;
129             case 1:
130             case 2:
131             default:
132                 return String JavaDoc.class;
133         }
134     }
135
136
137     public void setValueAt(Object JavaDoc aValue, Object JavaDoc node, int column) {
138
139         if (column == 1) {
140             TreeNode tNode = (TreeNode)node;
141
142             tNode.setValue(aValue);
143         }
144
145     }
146
147
148
149     
150     
151     
152     
153     
154
155     public static interface TreeNode {
156         Object JavaDoc getValue();
157         void setValue(Object JavaDoc obj);
158         Class JavaDoc getType();
159         TreeNode[] getChildren();
160         boolean isEditable();
161     }
162
163     public static class ConfigNode implements TreeNode {
164         private Configuration config;
165         private BeanInfo JavaDoc beanInfo;
166         
167         private static final List JavaDoc IGNORED_ATTRIBUTES =
168             Arrays.asList(new String JavaDoc[]
169                 { "DataStructure", "RootElement", "Class", "Name" });
170         
171         public ConfigNode(Configuration config) {
172             this.config = config;
173             try {
174                 this.beanInfo =
175                     Introspector.getBeanInfo(config.getClass());
176             } catch (Exception JavaDoc e) {
177                 e.printStackTrace();
178             }
179         }
180         
181         public TreeNode[] getChildren() {
182             
183             PropertyDescriptor JavaDoc[] props = this.beanInfo.getPropertyDescriptors();
184             ArrayList JavaDoc arraylist = new ArrayList JavaDoc();
185
186             try {
187                 for (int i=0; i< props.length; i++) {
188                     TreeNode newNode;
189                     String JavaDoc attributeName = props[i].getName();
190                     attributeName = capitalize(attributeName);
191                     Class JavaDoc returnType = props[i].getPropertyType();
192
193                     if (!IGNORED_ATTRIBUTES.contains(attributeName)) {
194                         
195                         if (returnType.isArray()) {
196                             newNode = new ArrayNode(this.config, props[i]);
197                         } else if (Configuration.class.isAssignableFrom(returnType)) {
198                             newNode = new ConfigNode(
199                                 ((Configuration)props[i].getReadMethod().invoke(this.config,new Object JavaDoc[]{})));
200                         } else {
201                             newNode = new DataNode(this.config, props[i]);
202                         }
203
204                         arraylist.add(newNode);
205                     }
206                 }
207             } catch (Exception JavaDoc e) {e.printStackTrace(); }
208
209             TreeNode[] foo = new TreeNode[arraylist.size()];
210             return (TreeNode[]) arraylist.toArray(foo);
211         }
212
213         public Object JavaDoc getValue() {
214             try {
215             return this.config.getConfigurationName(); //.getConfigurationInterface().getName();
216
} catch (Exception JavaDoc e) {
217                 //e.printStackTrace();
218
return "couldn't determine config type";
219             }
220         }
221
222         public String JavaDoc toString() {
223             return this.config.getConfigurationName();
224         }
225
226         public void setValue(Object JavaDoc value) { }
227         
228         public Class JavaDoc getType() {
229             return null;
230         }
231         
232         public boolean isEditable() {
233             return false;
234         }
235         
236     }
237
238     public static class ArrayNode implements TreeNode {
239         private Configuration parent;
240         private PropertyDescriptor JavaDoc descriptor;
241         
242         public ArrayNode(Configuration parent, PropertyDescriptor JavaDoc descriptor) {
243             this.parent = parent;
244             this.descriptor = descriptor;
245         }
246         
247         public TreeNode[] getChildren() {
248             return (TreeNode[]) getArray();
249         }
250
251         public Object JavaDoc[] getArray() {
252             try {
253                 Method JavaDoc m = descriptor.getReadMethod();
254                 //Method m = this.parent.getClass().getMethod("get"+attributeName,new Class[]{});
255
Object JavaDoc arr = m.invoke(this.parent, new Object JavaDoc[]{});
256
257                 ArrayList JavaDoc list = new ArrayList JavaDoc();
258                 for (int i = 0; i < Array.getLength(arr); i++) {
259                     if (!Configuration.class.isAssignableFrom(m.getReturnType().getComponentType())) {
260                         list.add(new ArrayNodeDataValue(this, Array.get(arr, i), i));
261                     } else {
262                        list.add(new ArrayNodeConfigurationValue(this,(Configuration)Array.get(arr, i), i));
263                     }
264                 }
265                 TreeNode[] treeNodes = new TreeNode[list.size()];
266                 return list.toArray(treeNodes);
267
268             } catch (Exception JavaDoc e) {
269                 e.printStackTrace();
270                 return new Object JavaDoc[]{};
271             }
272         }
273
274         public Object JavaDoc getValue() {
275             try {
276              Method JavaDoc m = descriptor.getReadMethod();
277                 //Method m = this.parent.getClass().getMethod("get"+attributeName,new Class[]{});
278
return m.invoke(this.parent, new Object JavaDoc[]{});
279             } catch (Exception JavaDoc e) {
280                 e.printStackTrace();
281                 return null;
282             }
283         }
284
285         public String JavaDoc toString() {
286             return this.descriptor.getName();
287         }
288
289         public void setValue(Object JavaDoc obj) {
290             
291 // System.out.println("Trying to overwrite an array: " +
292
// obj.getClass());
293
//
294
// System.out.println("\tComponentType is: " +
295
// obj.getClass().getComponentType());
296

297             try {
298                 Method JavaDoc m = this.descriptor.getWriteMethod();
299                 
300                 m.invoke(this.parent, new Object JavaDoc[] { obj });
301             } catch (Exception JavaDoc e) {
302                 e.printStackTrace();
303             }
304         }
305
306         public Class JavaDoc getType() {
307             return ArrayNode.class;
308         }
309         
310         public boolean isEditable() {
311             return true;
312         }
313         
314     }
315
316     public static class ArrayNodeDataValue implements TreeNode {
317         private ArrayNode parent;
318         private int index;
319         private Object JavaDoc data;
320         public ArrayNodeDataValue(ArrayNode parent,Object JavaDoc data, int index) {
321             this.parent = parent;
322             this.data = data;
323             this.index = index;
324         }
325
326         public TreeNode[] getChildren() {
327             return null;
328         }
329         public Object JavaDoc getValue() {
330             Object JavaDoc obj = this.data;
331             if (obj != null) {
332                 return obj.toString();
333             } else {
334                 return "";
335             }
336         }
337         public String JavaDoc toString() {
338             return parent.toString() + "[" + index + "]";
339         }
340
341         public void setValue(Object JavaDoc obj) {
342         }
343
344         public Class JavaDoc getType() {
345             return null;
346         }
347         
348         public boolean isEditable() {
349             return false;
350         }
351         
352     }
353     public static class ArrayNodeConfigurationValue extends ConfigNode {
354         private ArrayNode parent;
355         private int index;
356         public ArrayNodeConfigurationValue(ArrayNode parent,Configuration config, int index) {
357             super(config);
358             this.parent = parent;
359             this.index = index;
360         }
361
362         public String JavaDoc toString() {
363             return parent.toString() + "[" + index + "]";
364         }
365
366     }
367
368     public static class DataNode implements TreeNode {
369         private Configuration parent;
370         //private String attributeName;
371
//private Method getter;
372
private PropertyDescriptor JavaDoc descriptor;
373         
374         public DataNode(Configuration parent, PropertyDescriptor JavaDoc pd) {
375             this.parent = parent;
376             this.descriptor = pd;
377         }
378             
379         
380
381         public TreeNode[] getChildren() {
382             return null;
383         }
384
385         public Object JavaDoc getValue() {
386             try {
387                 Method JavaDoc m = this.descriptor.getReadMethod();
388                 Object JavaDoc obj = m.invoke(this.parent, new Object JavaDoc[]{});
389                 ConfigurationTypeService typeService =
390                     ConfigurationTypeServiceFactory.getInstance();
391
392                 return obj; //typeService.toString(m.getReturnType(), obj);
393
} catch (Exception JavaDoc e) {
394                 return null;
395             }
396         }
397
398         public String JavaDoc toString() {
399             return capitalize(this.descriptor.getName());
400         }
401
402         public void setValue(Object JavaDoc obj) {
403             System.out.println("Altering [" + toString() + "] = " +
404                 obj);
405             try {
406                 Method JavaDoc m = this.descriptor.getWriteMethod();
407
408                 ConfigurationTypeService typeService =
409                     ConfigurationTypeServiceFactory.getInstance();
410
411                 Object JavaDoc value =
412                     typeService.toObject(
413                         this.descriptor.getPropertyType(),
414                         obj.toString());
415
416                 m.invoke(this.parent, new Object JavaDoc[]{ value });
417
418             } catch (UnsupportedOperationException JavaDoc uoe) {
419                 System.out.println(ExceptionUtility.printStackTracesToString(uoe));
420             } catch (Exception JavaDoc e) {
421                 // Useful for debugging
422
System.out.println(ExceptionUtility.printStackTracesToString(e));
423             }
424
425         }
426
427         public Class JavaDoc getType() {
428             if (this.descriptor != null) {
429                 Class JavaDoc type = this.descriptor.getPropertyType();
430                 return type;
431             } else {
432                 return null;
433             }
434         }
435         
436         public boolean isEditable() {
437             return (null != this.descriptor.getWriteMethod());
438         }
439         
440     }
441     
442     public static String JavaDoc capitalize(String JavaDoc string) {
443         return Character.toUpperCase(string.toCharArray()[0]) +
444             string.substring(1);
445     }
446
447 }
448
Popular Tags