1 26 27 package org.objectweb.util.explorer.plugin.java.reflect.swing; 28 29 import java.awt.Color ; 30 import java.awt.FlowLayout ; 31 import java.lang.reflect.Field ; 32 import java.lang.reflect.Method ; 33 import java.util.Arrays ; 34 import java.util.Vector ; 35 36 import javax.swing.JEditorPane ; 37 import javax.swing.JPanel ; 38 39 import org.objectweb.util.explorer.api.Panel; 40 import org.objectweb.util.explorer.api.TreeView; 41 import org.objectweb.util.explorer.plugin.java.reflect.ClassUtil; 42 import org.objectweb.util.explorer.plugin.java.reflect.ClassVisibility; 43 import org.objectweb.util.explorer.plugin.java.reflect.ClassVisibilityConfig; 44 import org.objectweb.util.explorer.plugin.java.reflect.FieldUtil; 45 import org.objectweb.util.explorer.plugin.java.reflect.MemberComparator; 46 import org.objectweb.util.explorer.plugin.java.reflect.MethodUtil; 47 48 56 public class ClassPanel 57 implements Panel { 58 59 65 protected JPanel panel_; 66 67 protected StringBuffer htmlText_; 68 69 70 protected MemberComparator comparator_; 71 72 78 public ClassPanel(){ 79 panel_ = new JPanel (); 80 htmlText_ = new StringBuffer (); 81 FlowLayout flow = new FlowLayout (FlowLayout.LEFT); 82 panel_.setLayout(flow); 83 panel_.setBackground(Color.white); 84 comparator_ = new MemberComparator(); 85 } 86 87 93 protected String [] getFields(Field [] fields, ClassVisibilityConfig config){ 94 Arrays.sort(fields,comparator_); 95 Vector fieldList = new Vector (); 96 FieldUtil fieldUtil = null; 97 for(int i=0 ; i<fields.length ; i++){ 98 if(ClassVisibility.hasToBeAdded(fields[i].getModifiers(),config,ClassVisibilityConfig.ATTRIBUTE)){ 99 fieldUtil = new FieldUtil(fields[i]); 100 fieldList.add(fieldUtil.toHTML()); 101 } 102 } 103 return (String [])fieldList.toArray(new String [0]); 104 } 105 106 protected String [] getMethods(Method [] methods, ClassVisibilityConfig config){ 107 Arrays.sort(methods,comparator_); 108 Vector methodList = new Vector (); 109 MethodUtil methodUtil = null; 110 for(int i=0 ; i<methods.length ; i++){ 111 if(ClassVisibility.hasToBeAdded(methods[i].getModifiers(),config,ClassVisibilityConfig.METHOD)){ 112 methodUtil = new MethodUtil(methods[i]); 113 methodList.add(methodUtil.toHTML()); 114 } 115 } 116 return (String [])methodList.toArray(new String [0]); 117 } 118 119 protected String [] getInnerClasses(Class [] classes, ClassVisibilityConfig config){ 120 Arrays.sort(classes,comparator_); 121 Vector classList = new Vector (); 122 ClassUtil classUtil = null; 123 for(int i=0 ; i<classes.length ; i++){ 124 if(ClassVisibility.hasToBeAdded(classes[i].getModifiers(),config,ClassVisibilityConfig.INNER_CLASS)){ 125 classUtil = new ClassUtil(classes[i]); 126 classList.add(classUtil.toHTML()); 127 } 128 } 129 return (String [])classList.toArray(new String [0]); 130 } 131 132 protected String getHTMLList(String [] elements){ 133 if(elements!=null){ 134 StringBuffer list = new StringBuffer (); 135 list.append("<ul>"); 136 for(int i = 0 ; i < elements.length ; i++) 137 list.append("<li>" + elements[i] + "</li>"); 138 list.append("</ul>"); 139 return list.toString(); 140 } 141 return ""; 142 } 143 144 150 151 154 public Object getPanel() { 155 return panel_; 156 } 157 158 161 public void selected(TreeView treeView) { 162 163 Class theClass = (Class )treeView.getSelectedObject(); 164 ClassVisibilityConfig config = (ClassVisibilityConfig)ClassVisibility.config_.get(theClass.getName()); 165 if(config == null) 166 config = (ClassVisibilityConfig)ClassVisibility.config_.get(ClassVisibility.DEFAULT_CONFIG); 167 168 htmlText_.append("<html>"); 169 htmlText_.append("<header></header>"); 170 htmlText_.append("<body>"); 171 172 String name = theClass.getName(); 173 String package_ = theClass.isPrimitive()?"":(name.lastIndexOf('.')==-1?"":name.substring(0,name.lastIndexOf('.'))); 174 String type = theClass.isInterface()?"Interface ":"Class "; 175 String className = name.lastIndexOf('.')==-1?name:name.substring(name.lastIndexOf('.')+1); 176 htmlText_.append(package_); 177 htmlText_.append("<h2>" + type + " " + className + "</h2>"); 178 htmlText_.append("<hr width=\"50%\"><br>"); 179 180 String [] fields = getFields(theClass.getDeclaredFields(), config); 181 if(fields.length>0){ 182 htmlText_.append("<b>Fields</b>"); 183 htmlText_.append(getHTMLList(fields)); 184 } 185 186 String [] methods = getMethods(theClass.getDeclaredMethods(), config); 187 if(methods.length>0){ 188 htmlText_.append("<b>Methods</b>"); 189 htmlText_.append(getHTMLList(methods)); 190 } 191 192 String [] innerClasses = getInnerClasses(theClass.getDeclaredClasses(), config); 193 if(innerClasses.length>0){ 194 htmlText_.append("<b>Inner classes</b>"); 195 htmlText_.append(getHTMLList(innerClasses)); 196 } 197 198 htmlText_.append("</body>"); 199 htmlText_.append("</html>"); 200 201 JEditorPane html = new JEditorPane ("text/html",htmlText_.toString()); 202 html.setEditable(false); 203 panel_.add(html); 204 } 205 206 209 public void unselected(TreeView treeview) { 210 } 212 213 } 214 | Popular Tags |