KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > core > panel > MethodGUI


1 /*===========================================================================
2
3 ObjectWeb Naming Context Framework
4 Copyright (C) 2002 USTL - LIFL - GOAL
5 Contact: architecture@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): Jérôme Moroy.
23 Contributor(s): ______________________________________.
24
25 ===========================================================================*/

26
27 package org.objectweb.util.browser.core.panel;
28
29 /** The Java API'imports */
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import java.awt.Dimension JavaDoc;
33 import java.awt.Color JavaDoc;
34 import java.lang.reflect.Method JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.awt.event.ActionEvent JavaDoc;
37 import javax.swing.border.TitledBorder JavaDoc;
38 import javax.swing.BoxLayout JavaDoc;
39 import javax.swing.Box JavaDoc;
40 import java.awt.Component JavaDoc;
41
42 /** The Console's imports */
43 import org.objectweb.util.browser.api.TreeView;
44
45 /**
46  * This class represents a Method
47  *
48  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
49  * @version 0.1
50  */

51 public class MethodGUI extends Box JavaDoc {
52
53     /** The view on the tree */
54     protected TreeView treeView_;
55
56     /** The object which is represented */
57     protected Object JavaDoc object_;
58
59     /** The associated method */
60     protected Method JavaDoc method_;
61
62     /** The parameters of the method */
63     protected ParameterGUI[] parametersGUI_;
64
65     /**
66      *
67      * @return the box to add
68      */

69     protected Box JavaDoc getHeader() {
70         Box JavaDoc header = Box.createHorizontalBox();
71         //header.add(Box.createHorizontalGlue());
72
//header.add(Box.createHorizontalStrut(10));
73
JButton JavaDoc button = new JButton JavaDoc("Invoke");
74         button.setAlignmentX(Component.LEFT_ALIGNMENT);
75         button.setPreferredSize(new Dimension JavaDoc(90, 20));
76         button.addActionListener(new InvokeAction());
77         header.add(button);
78         //header.add(Box.createHorizontalGlue());
79
return header;
80     }
81
82     /**
83      * Constructs a JPanel containing a representation of the method
84      * @param method The method to represent
85      * @param treeView A view on the tree
86      */

87     public MethodGUI(Method JavaDoc method, TreeView treeView) {
88         super(BoxLayout.X_AXIS);
89         method_ = method;
90         treeView_ = treeView;
91         object_ = treeView_.getSelectedObject();
92         Class JavaDoc[] args = method_.getParameterTypes();
93         JPanel JavaDoc panel = new JPanel JavaDoc();
94         panel.setBackground(Color.white);
95         panel.setBorder(new TitledBorder JavaDoc(null, getUMLRepresentation(), TitledBorder.LEFT, TitledBorder.TOP));
96         Box JavaDoc box = Box.createVerticalBox();
97         box.add(getHeader());
98         parametersGUI_ = new ParameterGUI[args.length];
99         for (int i = 0; i < parametersGUI_.length; i++) {
100             parametersGUI_[i] = new ParameterGUI(args[i]);
101             box.add(parametersGUI_[i]);
102         }
103         panel.add(box);
104         add(panel);
105     }
106
107     /**
108      * Returns the name without the package's name
109      * @return the string
110      */

111     protected String JavaDoc getNameWithoutPackage(String JavaDoc type) {
112         int lastIndex = type.lastIndexOf('.');
113         if (lastIndex != -1)
114             return type.substring(lastIndex + 1);
115         return type;
116     }
117
118     /**
119      * Returns an UML representation of a method
120      *
121      * @return The UML representation
122      */

123     public String JavaDoc getUMLRepresentation() {
124         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
125         str.append(method_.getName() + "(");
126         Class JavaDoc[] args = method_.getParameterTypes();
127         for (int j = 0; j < args.length; j++) {
128             str.append(getNameWithoutPackage(args[j].getName()));
129             if (j != args.length - 1)
130                 str.append(", ");
131         }
132         str.append(") : " + method_.getReturnType().getName());
133         Class JavaDoc[] exceptions = method_.getExceptionTypes();
134         if (exceptions.length != 0) {
135             str.append(" throws ");
136             for (int j = 0; j < exceptions.length; j++) {
137                 str.append(getNameWithoutPackage(exceptions[j].getName()));
138                 if (j != exceptions.length - 1)
139                     str.append(", ");
140             }
141         }
142         return str.toString();
143     }
144
145     /**
146      * Use to execute the method invocation
147      */

148     protected class InvokeAction implements ActionListener JavaDoc {
149         public void actionPerformed(ActionEvent JavaDoc e) {
150             Object JavaDoc[] args = new Object JavaDoc[parametersGUI_.length];
151             for (int i = 0; i < parametersGUI_.length; i++) {
152                 try {
153                     args[i] = new Integer JavaDoc(parametersGUI_[i].getParameterValue());
154                 } catch (java.lang.NumberFormatException JavaDoc ex) {
155                     args[i] = parametersGUI_[i].getParameterValue();
156                 }
157             }
158             try {
159                 method_.invoke(object_, args);
160                 treeView_.getTree().refresh();
161             } catch (java.lang.IllegalAccessException JavaDoc ex) {
162                 // ignore
163
} catch (java.lang.IllegalArgumentException JavaDoc ex) {
164                 // ignore
165
} catch (java.lang.reflect.InvocationTargetException JavaDoc ex) {
166                 // ignore
167
}
168         }
169     }
170 }
171
Popular Tags