1 7 package org.ejtools.adwt; 8 9 import java.awt.Component ; 10 import java.awt.Dimension ; 11 import java.awt.Frame ; 12 import java.awt.GridBagConstraints ; 13 import java.awt.GridBagLayout ; 14 import java.awt.Insets ; 15 import java.awt.event.ActionEvent ; 16 import java.awt.event.ActionListener ; 17 import java.beans.MethodDescriptor ; 18 import java.beans.ParameterDescriptor ; 19 import java.beans.PropertyEditor ; 20 import java.beans.PropertyEditorManager ; 21 import java.lang.reflect.InvocationTargetException ; 22 import java.util.Vector ; 23 24 import javax.swing.JButton ; 25 import javax.swing.JDialog ; 26 import javax.swing.JLabel ; 27 import javax.swing.JOptionPane ; 28 import javax.swing.JPanel ; 29 30 import com.dreambean.awt.GenericPropertyCustomizer; 31 32 33 37 public class GenericMethodDialog extends JDialog 38 implements ActionListener 39 { 40 41 protected Vector editors; 42 43 protected MethodDescriptor md; 44 45 protected Object obj; 46 47 48 49 public GenericMethodDialog() 50 { 51 super(); 52 } 53 54 55 62 public GenericMethodDialog(Object obj1, MethodDescriptor methoddescriptor, Frame frame) 63 { 64 super(frame, true); 65 editors = new Vector (); 66 obj = obj1; 67 md = methoddescriptor; 68 JPanel jpanel = (JPanel ) getContentPane(); 69 jpanel.setLayout(new GridBagLayout ()); 70 try 71 { 72 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 73 gridbagconstraints.insets = new Insets (5, 5, 5, 5); 74 gridbagconstraints.anchor = 11; 75 gridbagconstraints.weighty = 1.0D; 76 ParameterDescriptor aparameterdescriptor[] = null; 77 Class aclass[] = methoddescriptor.getMethod().getParameterTypes(); 78 if (methoddescriptor.getParameterDescriptors() != null) 79 { 80 aparameterdescriptor = methoddescriptor.getParameterDescriptors(); 81 } 82 else 83 { 84 aparameterdescriptor = new ParameterDescriptor [aclass.length]; 85 for (int i = 0; i < aclass.length; i++) 86 { 87 aparameterdescriptor[i] = new ParameterDescriptor (); 88 aparameterdescriptor[i].setName(aclass[i].getName()); 89 } 90 91 } 92 JLabel jlabel = new JLabel (methoddescriptor.getDisplayName()); 93 gridbagconstraints.gridwidth = 0; 94 ((GridBagLayout ) jpanel.getLayout()).setConstraints(jlabel, gridbagconstraints); 95 jpanel.add(jlabel); 96 for (int j = 0; j < aparameterdescriptor.length; j++) 97 { 98 PropertyEditor propertyeditor = null; 99 String s = (String ) aparameterdescriptor[j].getValue("propertyeditor"); 100 if (s != null) 101 { 102 try 103 { 104 propertyeditor = (PropertyEditor ) Class.forName(s).newInstance(); 105 } 106 catch (Exception _ex) 107 { 108 continue; 109 } 110 } 111 else 112 { 113 propertyeditor = PropertyEditorManager.findEditor(aclass[j]); 114 } 115 if (propertyeditor != null) 116 { 117 JLabel jlabel1 = new JLabel (aparameterdescriptor[j].getDisplayName() + ":", 4); 118 gridbagconstraints.gridwidth = -1; 119 gridbagconstraints.fill = 0; 120 gridbagconstraints.weightx = 0.0D; 121 ((GridBagLayout ) jpanel.getLayout()).setConstraints(jlabel1, gridbagconstraints); 122 jpanel.add(jlabel1); 123 if (propertyeditor.getTags() != null) 124 { 125 propertyeditor.setAsText(propertyeditor.getTags()[0]); 126 } 127 else 128 { 129 try 130 { 131 propertyeditor.setValue(aclass[j].newInstance()); 132 } 133 catch (Exception _ex) 134 { 135 } 136 } 137 Object obj2; 138 if (propertyeditor.supportsCustomEditor()) 139 { 140 obj2 = propertyeditor.getCustomEditor(); 141 } 142 else 143 { 144 String as[] = propertyeditor.getTags(); 145 if (as != null) 146 { 147 obj2 = new GenericPropertyCustomizer(propertyeditor, as); 148 } 149 else 150 { 151 obj2 = new GenericPropertyCustomizer(propertyeditor); 152 } 153 } 154 gridbagconstraints.gridwidth = 0; 155 gridbagconstraints.fill = 2; 156 gridbagconstraints.weightx = 1.0D; 157 ((GridBagLayout ) jpanel.getLayout()).setConstraints(((Component ) (obj2)), gridbagconstraints); 158 jpanel.add(((Component ) (obj2))); 159 editors.addElement(propertyeditor); 160 } 161 gridbagconstraints.weighty = 1.0D; 162 } 163 gridbagconstraints.gridwidth = 0; 164 JPanel jpanel1 = new JPanel (); 165 JButton jbutton = new JButton ("Ok"); 167 jpanel1.add(jbutton); 168 jbutton.addActionListener(this); 169 JButton jbutton1 = new JButton ("Cancel"); 170 jpanel1.add(jbutton1); 171 jbutton1.addActionListener(this); 172 ((GridBagLayout ) jpanel.getLayout()).setConstraints(jpanel1, gridbagconstraints); 173 jpanel.add(jpanel1); 174 pack(); 175 if (getWidth() < 300) 176 { 177 setSize(new Dimension (300, getHeight())); 178 } 179 setLocationRelativeTo(frame); 180 setVisible(true); 181 } 182 catch (Exception exception) 183 { 184 System.out.println("Exception occurred"); 185 exception.printStackTrace(); 186 } 187 } 188 189 190 195 public void actionPerformed(ActionEvent actionevent) 196 { 197 if (actionevent.getActionCommand().equals("Ok")) 198 { 199 Object aobj[] = new Object [editors.size()]; 200 for (int i = 0; i < aobj.length; i++) 201 { 202 aobj[i] = ((PropertyEditor ) editors.elementAt(i)).getValue(); 203 } 204 try 205 { 206 Object obj1 = md.getMethod().invoke(obj, aobj); 207 if (obj1 != null) 208 { 209 JOptionPane.showMessageDialog(this, obj1.toString(), "Result", 1); 210 } 211 } 212 catch (InvocationTargetException invocationtargetexception) 213 { 214 invocationtargetexception.getTargetException().printStackTrace(); 215 JOptionPane.showMessageDialog(this, invocationtargetexception.getTargetException().getMessage(), "Error", 0); 216 } 217 catch (Exception exception) 218 { 219 System.err.println(exception); 220 JOptionPane.showMessageDialog(this, "An exception occured. Check log for details", "Error", 0); 221 } 222 } 223 setVisible(false); 224 } 225 } 226 | Popular Tags |