KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2004 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): Jerome Moroy, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.util.explorer.plugin.java.reflect.swing;
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.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 /**
49  *
50  *
51  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>,
52  * <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>
53  *
54  * @version 0.1
55  */

56 public class ClassPanel
57     implements Panel JavaDoc {
58
59     //==================================================================
60
//
61
// Internal states.
62
//
63
//==================================================================
64

65     protected JPanel JavaDoc panel_;
66
67     protected StringBuffer JavaDoc htmlText_;
68
69     /** The comparator to use to sort the arrays */
70     protected MemberComparator comparator_;
71
72     //==================================================================
73
//
74
// Constructor.
75
//
76
//==================================================================
77

78     public ClassPanel(){
79         panel_ = new JPanel JavaDoc();
80         htmlText_ = new StringBuffer JavaDoc();
81         FlowLayout JavaDoc flow = new FlowLayout JavaDoc(FlowLayout.LEFT);
82         panel_.setLayout(flow);
83         panel_.setBackground(Color.white);
84         comparator_ = new MemberComparator();
85     }
86
87     //==================================================================
88
//
89
// No internal method.
90
//
91
//==================================================================
92

93     protected String JavaDoc[] getFields(Field JavaDoc[] fields, ClassVisibilityConfig config){
94         Arrays.sort(fields,comparator_);
95         Vector JavaDoc fieldList = new Vector JavaDoc();
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 JavaDoc[])fieldList.toArray(new String JavaDoc[0]);
104     }
105
106     protected String JavaDoc[] getMethods(Method JavaDoc[] methods, ClassVisibilityConfig config){
107         Arrays.sort(methods,comparator_);
108         Vector JavaDoc methodList = new Vector JavaDoc();
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 JavaDoc[])methodList.toArray(new String JavaDoc[0]);
117     }
118
119     protected String JavaDoc[] getInnerClasses(Class JavaDoc[] classes, ClassVisibilityConfig config){
120         Arrays.sort(classes,comparator_);
121         Vector JavaDoc classList = new Vector JavaDoc();
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 JavaDoc[])classList.toArray(new String JavaDoc[0]);
130     }
131
132     protected String JavaDoc getHTMLList(String JavaDoc[] elements){
133         if(elements!=null){
134             StringBuffer JavaDoc list = new StringBuffer JavaDoc();
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     //==================================================================
145
//
146
// Public methods for Panel interface.
147
//
148
//==================================================================
149

150
151     /* (non-Javadoc)
152      * @see org.objectweb.util.explorer.api.Panel#getPanel()
153      */

154     public Object JavaDoc getPanel() {
155         return panel_;
156     }
157
158     /* (non-Javadoc)
159      * @see org.objectweb.util.explorer.api.Panel#selected(org.objectweb.util.explorer.api.TreeView)
160      */

161     public void selected(TreeView treeView) {
162
163         Class JavaDoc theClass = (Class JavaDoc)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 JavaDoc name = theClass.getName();
173         String JavaDoc package_ = theClass.isPrimitive()?"":(name.lastIndexOf('.')==-1?"":name.substring(0,name.lastIndexOf('.')));
174         String JavaDoc type = theClass.isInterface()?"Interface ":"Class ";
175         String JavaDoc 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 JavaDoc[] 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 JavaDoc[] 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 JavaDoc[] 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 JavaDoc html = new JEditorPane JavaDoc("text/html",htmlText_.toString());
202         html.setEditable(false);
203         panel_.add(html);
204     }
205
206     /* (non-Javadoc)
207      * @see org.objectweb.util.explorer.api.Panel#unselected(org.objectweb.util.explorer.api.TreeView)
208      */

209     public void unselected(TreeView treeview) {
210         // Nothing to do
211
}
212
213 }
214
Popular Tags