KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > menu > control > GenerateComponentAction


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui.menu.control;
25
26 import org.objectweb.fractal.gui.model.Component;
27 import org.objectweb.fractal.gui.model.ServerInterface;
28 import org.objectweb.fractal.gui.model.ClientInterface;
29 import org.objectweb.fractal.gui.model.Interface;
30 import org.objectweb.asm.ClassReader;
31 import org.objectweb.asm.ClassVisitor;
32 import org.objectweb.asm.CodeVisitor;
33 import org.objectweb.asm.Constants;
34 import org.objectweb.asm.Type;
35 import org.objectweb.asm.Attribute;
36
37 import java.awt.event.ActionEvent JavaDoc;
38 import java.net.URL JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.List JavaDoc;
43
44 import javax.swing.ImageIcon JavaDoc;
45 import javax.swing.JOptionPane JavaDoc;
46 import javax.swing.KeyStroke JavaDoc;
47
48 /**
49  * An action to create new components from an existing component class.
50  */

51
52 public class GenerateComponentAction extends CreateAction {
53
54   /**
55    * Name of the last generated component.
56    */

57
58   private String JavaDoc lastClassName;
59
60   /**
61    * Constructs a new {@link GenerateComponentAction} component.
62    */

63
64   public GenerateComponentAction () {
65     putValue(NAME, "New component...");
66     putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control INSERT"));
67     URL JavaDoc url = getClass().getResource(
68       "/org/objectweb/fractal/gui/resources/empty.gif");
69     putValue(SMALL_ICON, new ImageIcon JavaDoc(url));
70     setEnabled(false);
71   }
72
73   // -------------------------------------------------------------------------
74
// Implementation of the SelectionListener interface
75
// -------------------------------------------------------------------------
76

77   public void selectionChanged () {
78     boolean enabled = false;
79     Object JavaDoc o = selection.getSelection();
80     if (o instanceof Component) {
81       Component c = (Component)o;
82       enabled = c.getMasterComponent() == null;
83     }
84     setEnabled(enabled);
85   }
86
87   // -------------------------------------------------------------------------
88
// Implementation of the ActionListener interface
89
// -------------------------------------------------------------------------
90

91   public void actionPerformed (final ActionEvent JavaDoc e) {
92     String JavaDoc s = (String JavaDoc)JOptionPane.showInputDialog(
93       null,
94       "Enter the name of an existing primitive component class",
95       "New Component",
96       JOptionPane.QUESTION_MESSAGE,
97       null,
98       null,
99       lastClassName);
100     if (s != null) {
101       lastClassName = s;
102       ClassReader cr;
103       try {
104         cr = new ClassReader(s);
105       } catch (IOException JavaDoc ioe) {
106         JOptionPane.showMessageDialog(
107           null, ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
108         return;
109       }
110       Object JavaDoc o = selection.getSelection();
111       Component parent = (Component)o;
112       Component child = factory.createComponent();
113       cr.accept(new ClassAnalyzer(child), true);
114       parent.addSubComponent(child);
115       //selection.selectComponent(child);
116
}
117   }
118
119   /**
120    * A class visitor to introspect a compiled class and to extract information
121    * about provided and required interfaces. Some information are extracted by
122    * recognizing naming patterns.
123    */

124
125   class ClassAnalyzer implements ClassVisitor {
126
127     /**
128      * The component that must be modified to reflect the information extracted
129      * from the visited class.
130      */

131
132     private Component component;
133
134     /**
135      * A map that stores the descriptors of the fields of the visited class.
136      */

137
138     private Map JavaDoc fields;
139
140     /**
141      * Constructs a new {@link GenerateComponentAction.ClassAnalyzer} object.
142      *
143      * @param component the component that must be modified to reflect the
144      * information extracted from the visited class.
145      */

146
147     public ClassAnalyzer (final Component component) {
148       this.component = component;
149       this.fields = new HashMap JavaDoc();
150     }
151
152     public void visit (
153       final int version,
154       final int access,
155       final String JavaDoc name,
156       final String JavaDoc superName,
157       final String JavaDoc[] interfaces,
158       final String JavaDoc sourceFile)
159     {
160       if (!superName.equals("java/lang/Object")) {
161         try {
162           ClassReader cr = new ClassReader(superName.replace('/', '.'));
163           cr.accept(this, true);
164         } catch (IOException JavaDoc ioe) {
165         }
166       }
167       if (name.indexOf('/') != -1) {
168         component.setName(name.substring(name.lastIndexOf('/') + 1));
169       } else {
170         component.setName(name);
171       }
172       component.setType(name.replace('/', '.'));
173       component.setImplementation(name.replace('/', '.'));
174       for (int i = 0; i < interfaces.length; ++i) {
175         if (interfaces[i].equals(
176               "org/objectweb/fractal/api/control/BindingController") ||
177             interfaces[i].equals("java/lang/Cloneable") ||
178             interfaces[i].equals("java/io/Serializable"))
179         {
180           continue;
181         }
182         String JavaDoc s = interfaces[i].substring(interfaces[i].lastIndexOf('/') + 1);
183         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
184         for (int j = 0; j < s.length(); ++j) {
185           if (Character.isUpperCase(s.charAt(j))) {
186             if (j > 0) {
187               buf.append('-');
188             }
189             buf.append(Character.toLowerCase(s.charAt(j)));
190           } else {
191             buf.append(s.charAt(j));
192           }
193         }
194         ServerInterface sitf = factory.createServerInterface(component);
195         sitf.setSignature(interfaces[i].replace('/', '.'));
196         sitf.setName(buf.toString());
197         component.addServerInterface(sitf);
198       }
199     }
200
201     public void visitInnerClass (
202       final String JavaDoc name,
203       final String JavaDoc outerName,
204       final String JavaDoc innerName,
205       final int access)
206     {
207       // does nothing
208
}
209
210     public void visitField (
211       final int access,
212       final String JavaDoc name,
213       final String JavaDoc desc,
214       final Object JavaDoc value,
215       final Attribute attrs)
216     {
217       if ((access & Constants.ACC_STATIC) != 0) {
218         if (name.endsWith("_BINDING") && value instanceof String JavaDoc) {
219           ClientInterface citf = factory.createClientInterface(component);
220           citf.setName((String JavaDoc)value);
221           component.addClientInterface(citf);
222         }
223       } else {
224         fields.put(name, desc);
225       }
226     }
227
228     public CodeVisitor visitMethod (
229       final int access,
230       final String JavaDoc name,
231       final String JavaDoc desc,
232       final String JavaDoc[] exceptions,
233       final Attribute attrs)
234     {
235       return null;
236     }
237
238     public void visitAttribute (final Attribute attribute) {
239       // does nothing
240
}
241
242     public void visitEnd () {
243       List JavaDoc itfs = component.getClientInterfaces();
244       for (int i = 0; i < itfs.size(); ++i) {
245         Interface itf = (Interface)itfs.get(i);
246         String JavaDoc desc = (String JavaDoc)fields.get(itf.getName());
247         if (desc != null) {
248           Type type = Type.getType(desc);
249           if (type.getSort() == Type.OBJECT) {
250             itf.setSignature(type.getClassName());
251           }
252         }
253       }
254     }
255   }
256 }
257
Popular Tags