1 package thinlet.drafts; 2 3 import java.awt.*; 4 import java.lang.reflect.*; 5 import thinlet.*; 6 7 10 public class ClassExplorer { 11 12 private Image classicon, methodicon, parametericon, fieldicon, exceptionicon; 13 14 17 public void initClass(Thinlet thinlet, Object combobox, Object 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 thinletclass = thinlet.getClass(); 25 int n = 0; 26 for (Class c = thinletclass; c != null; c = c.getSuperclass()) { 27 Object 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 41 public void loadClass(Thinlet thinlet, String classname, Object tree) { 42 thinlet.removeAll(tree); 43 try { 44 Class aclass = Class.forName(classname); 45 Object 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 constructornode = createNode(thinlet, classnode, 50 Modifier.toString(constructors[i].getModifiers()) + " " + 51 constructors[i].getName(), classicon); 52 53 Class [] cparameters = constructors[i].getParameterTypes(); 54 Class [] 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 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 methodnode = createNode(thinlet, classnode, 76 Modifier.toString(methods[i].getModifiers()) + " " + 77 getNameOf(methods[i].getReturnType()) + " " + 78 methods[i].getName(), methodicon); 79 80 Class [] parameters = methods[i].getParameterTypes(); 81 Class [] 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 se) { 93 createNode(thinlet, classnode, se.getMessage(), exceptionicon); 94 } 95 } catch (ClassNotFoundException cnfe) { } 96 } 97 98 private Object createNode(Thinlet thinlet, Object parent, String text, Image icon) { 99 Object 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 getNameOf(Class aclass) { 107 return (aclass.isArray()) ? (getNameOf(aclass.getComponentType()) + "[]") : aclass.getName(); 108 } 109 } | Popular Tags |