1 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 ; 38 import java.net.URL ; 39 import java.io.IOException ; 40 import java.util.Map ; 41 import java.util.HashMap ; 42 import java.util.List ; 43 44 import javax.swing.ImageIcon ; 45 import javax.swing.JOptionPane ; 46 import javax.swing.KeyStroke ; 47 48 51 52 public class GenerateComponentAction extends CreateAction { 53 54 57 58 private String lastClassName; 59 60 63 64 public GenerateComponentAction () { 65 putValue(NAME, "New component..."); 66 putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control INSERT")); 67 URL url = getClass().getResource( 68 "/org/objectweb/fractal/gui/resources/empty.gif"); 69 putValue(SMALL_ICON, new ImageIcon (url)); 70 setEnabled(false); 71 } 72 73 77 public void selectionChanged () { 78 boolean enabled = false; 79 Object 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 91 public void actionPerformed (final ActionEvent e) { 92 String s = (String )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 ioe) { 106 JOptionPane.showMessageDialog( 107 null, ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 108 return; 109 } 110 Object 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 } 117 } 118 119 124 125 class ClassAnalyzer implements ClassVisitor { 126 127 131 132 private Component component; 133 134 137 138 private Map fields; 139 140 146 147 public ClassAnalyzer (final Component component) { 148 this.component = component; 149 this.fields = new HashMap (); 150 } 151 152 public void visit ( 153 final int version, 154 final int access, 155 final String name, 156 final String superName, 157 final String [] interfaces, 158 final String 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 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 s = interfaces[i].substring(interfaces[i].lastIndexOf('/') + 1); 183 StringBuffer buf = new StringBuffer (); 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 name, 203 final String outerName, 204 final String innerName, 205 final int access) 206 { 207 } 209 210 public void visitField ( 211 final int access, 212 final String name, 213 final String desc, 214 final Object value, 215 final Attribute attrs) 216 { 217 if ((access & Constants.ACC_STATIC) != 0) { 218 if (name.endsWith("_BINDING") && value instanceof String ) { 219 ClientInterface citf = factory.createClientInterface(component); 220 citf.setName((String )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 name, 231 final String desc, 232 final String [] exceptions, 233 final Attribute attrs) 234 { 235 return null; 236 } 237 238 public void visitAttribute (final Attribute attribute) { 239 } 241 242 public void visitEnd () { 243 List itfs = component.getClientInterfaces(); 244 for (int i = 0; i < itfs.size(); ++i) { 245 Interface itf = (Interface)itfs.get(i); 246 String desc = (String )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 |