KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > plugin > java > reflect > ClassPanel


1 /*====================================================================
2
3 Objectweb Browser Framework
4 Copyright (C) 2000-2003 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle, Jerome Moroy.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.util.browser.plugin.java.reflect;
28
29 import java.awt.Color JavaDoc;
30 import java.awt.FlowLayout JavaDoc;
31 import java.lang.reflect.Field JavaDoc;
32 import java.lang.reflect.Method JavaDoc;
33 import java.util.Arrays JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 import javax.swing.JEditorPane JavaDoc;
37 import javax.swing.JPanel JavaDoc;
38
39 import org.objectweb.util.browser.api.Panel;
40 import org.objectweb.util.browser.api.TreeView;
41
42 /**
43  *
44  *
45  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>,
46  * <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>
47  *
48  * @version 0.1
49  */

50 public class ClassPanel
51     implements Panel JavaDoc {
52
53     //==================================================================
54
//
55
// Internal states.
56
//
57
//==================================================================
58

59     protected JPanel JavaDoc panel_;
60
61     protected StringBuffer JavaDoc htmlText_;
62
63     /** The comparator to use to sort the arrays */
64     protected MemberComparator comparator_;
65
66     //==================================================================
67
//
68
// Constructor.
69
//
70
//==================================================================
71

72     public ClassPanel(){
73         panel_ = new JPanel JavaDoc();
74         htmlText_ = new StringBuffer JavaDoc();
75         FlowLayout JavaDoc flow = new FlowLayout JavaDoc(FlowLayout.LEFT);
76         panel_.setLayout(flow);
77         panel_.setBackground(Color.white);
78         comparator_ = new MemberComparator();
79     }
80
81     //==================================================================
82
//
83
// No internal method.
84
//
85
//==================================================================
86

87     protected String JavaDoc[] getFields(Field JavaDoc[] fields, ClassVisibilityConfig config){
88         Arrays.sort(fields,comparator_);
89         Vector JavaDoc fieldList = new Vector JavaDoc();
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 JavaDoc[])fieldList.toArray(new String JavaDoc[0]);
98     }
99
100     protected String JavaDoc[] getMethods(Method JavaDoc[] methods, ClassVisibilityConfig config){
101         Arrays.sort(methods,comparator_);
102         Vector JavaDoc methodList = new Vector JavaDoc();
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 JavaDoc[])methodList.toArray(new String JavaDoc[0]);
111     }
112
113     protected String JavaDoc[] getInnerClasses(Class JavaDoc[] classes, ClassVisibilityConfig config){
114         Arrays.sort(classes,comparator_);
115         Vector JavaDoc classList = new Vector JavaDoc();
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 JavaDoc[])classList.toArray(new String JavaDoc[0]);
124     }
125
126     protected String JavaDoc getHTMLList(String JavaDoc[] elements){
127         if(elements!=null){
128             StringBuffer JavaDoc list = new StringBuffer JavaDoc();
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     //==================================================================
139
//
140
// Public methods for Panel interface.
141
//
142
//==================================================================
143

144
145     public JPanel JavaDoc getPanel() {
146         return panel_;
147     }
148
149     public void selected(TreeView treeView) {
150
151         Class JavaDoc theClass = (Class JavaDoc)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 JavaDoc name = theClass.getName();
161         String JavaDoc package_ = theClass.isPrimitive()?"":(name.lastIndexOf('.')==-1?"":name.substring(0,name.lastIndexOf('.')));
162         String JavaDoc type = theClass.isInterface()?"Interface ":"Class ";
163         String JavaDoc 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 JavaDoc[] 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 JavaDoc[] 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 JavaDoc[] 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 JavaDoc html = new JEditorPane JavaDoc("text/html",htmlText_.toString());
190         html.setEditable(false);
191         panel_.add(html);
192     }
193
194     public void unselected(TreeView treeview) {
195         // Nothing to do
196
}
197
198 }
199
Popular Tags