KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > thinlet > drafts > ClassExplorer


1 package thinlet.drafts;
2
3 import java.awt.*;
4 import java.lang.reflect.*;
5 import thinlet.*;
6
7 /**
8  *
9  */

10 public class ClassExplorer {
11     
12     private Image classicon, methodicon, parametericon, fieldicon, exceptionicon;
13     
14     /**
15      *
16      */

17     public void initClass(Thinlet thinlet, Object JavaDoc combobox, Object JavaDoc tree) {
18         classicon = thinlet.getIcon("/icon/class.gif");
19         methodicon = thinlet.getIcon("/icon/method.gif");
20         parametericon = thinlet.getIcon("/icon/parameter.gif");
21         fieldicon = thinlet.getIcon("/icon/field.gif");
22         exceptionicon = thinlet.getIcon("/icon/exception.gif");
23     
24         Class JavaDoc thinletclass = thinlet.getClass();
25         int n = 0;
26         for (Class JavaDoc c = thinletclass; c != null; c = c.getSuperclass()) {
27             Object JavaDoc choice = Thinlet.create("choice");
28             thinlet.setString(choice, "text", c.getName());
29             thinlet.setIcon(choice, "icon", classicon);
30             thinlet.add(combobox, choice, 0);
31             n++;
32         }
33         thinlet.setInteger(combobox, "selected", n - 1);
34         
35         loadClass(thinlet, thinletclass.getName(), tree);
36     }
37     
38     /**
39      *
40      */

41     public void loadClass(Thinlet thinlet, String JavaDoc classname, Object JavaDoc tree) {
42         thinlet.removeAll(tree);
43         try {
44             Class JavaDoc aclass = Class.forName(classname);
45             Object JavaDoc classnode = createNode(thinlet, tree, aclass.getName(), classicon);
46             try {
47                 Constructor[] constructors = aclass.getConstructors();
48                 for (int i = 0; i < constructors.length; i++) {
49                     Object JavaDoc constructornode = createNode(thinlet, classnode,
50                         Modifier.toString(constructors[i].getModifiers()) + " " +
51                         constructors[i].getName(), classicon);
52                     
53                     Class JavaDoc[] cparameters = constructors[i].getParameterTypes();
54                     Class JavaDoc[] cexceptions = constructors[i].getExceptionTypes();
55                     if ((cparameters.length > 0) || (cexceptions.length > 0)) {
56                         for (int j = 0; j < cparameters.length; j++) {
57                             createNode(thinlet, constructornode, getNameOf(cparameters[j]), parametericon);
58                         }
59                         for (int j = 0; j < cexceptions.length; j++) {
60                             createNode(thinlet, constructornode, getNameOf(cexceptions[j]), exceptionicon);
61                         }
62                         thinlet.setBoolean(constructornode, "expanded", false);
63                     }
64                 }
65                 
66                 Field[] fields = aclass.getDeclaredFields();
67                 for (int i = 0; i < fields.length; i++) {
68                     Object JavaDoc fieldnode = createNode(thinlet, classnode,
69                         Modifier.toString(fields[i].getModifiers()) + " " +
70                         fields[i].getName(), fieldicon);
71                 }
72                 
73                 Method[] methods = aclass.getDeclaredMethods();
74                 for (int i = 0; i < methods.length; i++) {
75                     Object JavaDoc methodnode = createNode(thinlet, classnode,
76                         Modifier.toString(methods[i].getModifiers()) + " " +
77                         getNameOf(methods[i].getReturnType()) + " " +
78                         methods[i].getName(), methodicon);
79                     
80                     Class JavaDoc[] parameters = methods[i].getParameterTypes();
81                     Class JavaDoc[] exceptions = methods[i].getExceptionTypes();
82                     if ((parameters.length > 0) || (exceptions.length > 0)) {
83                         for (int j = 0; j < parameters.length; j++) {
84                             createNode(thinlet, methodnode, getNameOf(parameters[j]), parametericon);
85                         }
86                         for (int j = 0; j < exceptions.length; j++) {
87                             createNode(thinlet, methodnode, getNameOf(exceptions[j]), exceptionicon);
88                         }
89                         thinlet.setBoolean(methodnode, "expanded", false);
90                     }
91                 }
92             } catch (SecurityException JavaDoc se) {
93                 createNode(thinlet, classnode, se.getMessage(), exceptionicon);
94             }
95         } catch (ClassNotFoundException JavaDoc cnfe) { /*never*/}
96     }
97     
98     private Object JavaDoc createNode(Thinlet thinlet, Object JavaDoc parent, String JavaDoc text, Image icon) {
99         Object JavaDoc node = Thinlet.create("node");
100         thinlet.setString(node, "text", text);
101         thinlet.setIcon(node, "icon", icon);
102         thinlet.add(parent, node);
103         return node;
104     }
105     
106     private String JavaDoc getNameOf(Class JavaDoc aclass) {
107         return (aclass.isArray()) ? (getNameOf(aclass.getComponentType()) + "[]") : aclass.getName();
108     }
109 }
Popular Tags