1 26 27 package org.objectweb.util.browser.core.panel; 28 29 30 import javax.swing.JPanel ; 31 import javax.swing.JButton ; 32 import java.awt.Dimension ; 33 import java.awt.Color ; 34 import java.lang.reflect.Method ; 35 import java.awt.event.ActionListener ; 36 import java.awt.event.ActionEvent ; 37 import javax.swing.border.TitledBorder ; 38 import javax.swing.BoxLayout ; 39 import javax.swing.Box ; 40 import java.awt.Component ; 41 42 43 import org.objectweb.util.browser.api.TreeView; 44 45 51 public class MethodGUI extends Box { 52 53 54 protected TreeView treeView_; 55 56 57 protected Object object_; 58 59 60 protected Method method_; 61 62 63 protected ParameterGUI[] parametersGUI_; 64 65 69 protected Box getHeader() { 70 Box header = Box.createHorizontalBox(); 71 JButton button = new JButton ("Invoke"); 74 button.setAlignmentX(Component.LEFT_ALIGNMENT); 75 button.setPreferredSize(new Dimension (90, 20)); 76 button.addActionListener(new InvokeAction()); 77 header.add(button); 78 return header; 80 } 81 82 87 public MethodGUI(Method method, TreeView treeView) { 88 super(BoxLayout.X_AXIS); 89 method_ = method; 90 treeView_ = treeView; 91 object_ = treeView_.getSelectedObject(); 92 Class [] args = method_.getParameterTypes(); 93 JPanel panel = new JPanel (); 94 panel.setBackground(Color.white); 95 panel.setBorder(new TitledBorder (null, getUMLRepresentation(), TitledBorder.LEFT, TitledBorder.TOP)); 96 Box 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 111 protected String getNameWithoutPackage(String type) { 112 int lastIndex = type.lastIndexOf('.'); 113 if (lastIndex != -1) 114 return type.substring(lastIndex + 1); 115 return type; 116 } 117 118 123 public String getUMLRepresentation() { 124 StringBuffer str = new StringBuffer (); 125 str.append(method_.getName() + "("); 126 Class [] 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 [] 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 148 protected class InvokeAction implements ActionListener { 149 public void actionPerformed(ActionEvent e) { 150 Object [] args = new Object [parametersGUI_.length]; 151 for (int i = 0; i < parametersGUI_.length; i++) { 152 try { 153 args[i] = new Integer (parametersGUI_[i].getParameterValue()); 154 } catch (java.lang.NumberFormatException 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 ex) { 162 } catch (java.lang.IllegalArgumentException ex) { 164 } catch (java.lang.reflect.InvocationTargetException ex) { 166 } 168 } 169 } 170 } 171 | Popular Tags |