1 26 27 package org.objectweb.util.browser.plugin.java.reflect; 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.browser.api.Panel; 40 import org.objectweb.util.browser.api.TreeView; 41 42 50 public class ClassPanel 51 implements Panel { 52 53 59 protected JPanel panel_; 60 61 protected StringBuffer htmlText_; 62 63 64 protected MemberComparator comparator_; 65 66 72 public ClassPanel(){ 73 panel_ = new JPanel (); 74 htmlText_ = new StringBuffer (); 75 FlowLayout flow = new FlowLayout (FlowLayout.LEFT); 76 panel_.setLayout(flow); 77 panel_.setBackground(Color.white); 78 comparator_ = new MemberComparator(); 79 } 80 81 87 protected String [] getFields(Field [] fields, ClassVisibilityConfig config){ 88 Arrays.sort(fields,comparator_); 89 Vector fieldList = new Vector (); 90 FieldUtil fieldUtil = null; 91 for(int i=0 ; i<fields.length ; i++){ 92 if(ClassVisibility.hasToBeAdded(fields[i].getModifiers(),config,ClassVisibilityConfig.ATTRIBUTE)){ 93 fieldUtil = new FieldUtil(fields[i]); 94 fieldList.add(fieldUtil.toHTML()); 95 } 96 } 97 return (String [])fieldList.toArray(new String [0]); 98 } 99 100 protected String [] getMethods(Method [] methods, ClassVisibilityConfig config){ 101 Arrays.sort(methods,comparator_); 102 Vector methodList = new Vector (); 103 MethodUtil methodUtil = null; 104 for(int i=0 ; i<methods.length ; i++){ 105 if(ClassVisibility.hasToBeAdded(methods[i].getModifiers(),config,ClassVisibilityConfig.METHOD)){ 106 methodUtil = new MethodUtil(methods[i]); 107 methodList.add(methodUtil.toHTML()); 108 } 109 } 110 return (String [])methodList.toArray(new String [0]); 111 } 112 113 protected String [] getInnerClasses(Class [] classes, ClassVisibilityConfig config){ 114 Arrays.sort(classes,comparator_); 115 Vector classList = new Vector (); 116 ClassUtil classUtil = null; 117 for(int i=0 ; i<classes.length ; i++){ 118 if(ClassVisibility.hasToBeAdded(classes[i].getModifiers(),config,ClassVisibilityConfig.INNER_CLASS)){ 119 classUtil = new ClassUtil(classes[i]); 120 classList.add(classUtil.toHTML()); 121 } 122 } 123 return (String [])classList.toArray(new String [0]); 124 } 125 126 protected String getHTMLList(String [] elements){ 127 if(elements!=null){ 128 StringBuffer list = new StringBuffer (); 129 list.append("<ul>"); 130 for(int i = 0 ; i < elements.length ; i++) 131 list.append("<li>" + elements[i] + "</li>"); 132 list.append("</ul>"); 133 return list.toString(); 134 } 135 return ""; 136 } 137 138 144 145 public JPanel getPanel() { 146 return panel_; 147 } 148 149 public void selected(TreeView treeView) { 150 151 Class theClass = (Class )treeView.getSelectedObject(); 152 ClassVisibilityConfig config = (ClassVisibilityConfig)ClassVisibility.config_.get(theClass.getName()); 153 if(config == null) 154 config = (ClassVisibilityConfig)ClassVisibility.config_.get(ClassVisibility.DEFAULT_CONFIG); 155 156 htmlText_.append("<html>"); 157 htmlText_.append("<header></header>"); 158 htmlText_.append("<body>"); 159 160 String name = theClass.getName(); 161 String package_ = theClass.isPrimitive()?"":(name.lastIndexOf('.')==-1?"":name.substring(0,name.lastIndexOf('.'))); 162 String type = theClass.isInterface()?"Interface ":"Class "; 163 String className = name.lastIndexOf('.')==-1?name:name.substring(name.lastIndexOf('.')+1); 164 htmlText_.append(package_); 165 htmlText_.append("<h2>" + type + " " + className + "</h2>"); 166 htmlText_.append("<hr width=\"50%\"><br>"); 167 168 String [] fields = getFields(theClass.getDeclaredFields(), config); 169 if(fields.length>0){ 170 htmlText_.append("<b>Fields</b>"); 171 htmlText_.append(getHTMLList(fields)); 172 } 173 174 String [] methods = getMethods(theClass.getDeclaredMethods(), config); 175 if(methods.length>0){ 176 htmlText_.append("<b>Methods</b>"); 177 htmlText_.append(getHTMLList(methods)); 178 } 179 180 String [] innerClasses = getInnerClasses(theClass.getDeclaredClasses(), config); 181 if(innerClasses.length>0){ 182 htmlText_.append("<b>Inner classes</b>"); 183 htmlText_.append(getHTMLList(innerClasses)); 184 } 185 186 htmlText_.append("</body>"); 187 htmlText_.append("</html>"); 188 189 JEditorPane html = new JEditorPane ("text/html",htmlText_.toString()); 190 html.setEditable(false); 191 panel_.add(html); 192 } 193 194 public void unselected(TreeView treeview) { 195 } 197 198 } 199 | Popular Tags |