KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > adwt > GenericMethodDialog


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.adwt;
8
9 import java.awt.Component JavaDoc;
10 import java.awt.Dimension JavaDoc;
11 import java.awt.Frame JavaDoc;
12 import java.awt.GridBagConstraints JavaDoc;
13 import java.awt.GridBagLayout JavaDoc;
14 import java.awt.Insets JavaDoc;
15 import java.awt.event.ActionEvent JavaDoc;
16 import java.awt.event.ActionListener JavaDoc;
17 import java.beans.MethodDescriptor JavaDoc;
18 import java.beans.ParameterDescriptor JavaDoc;
19 import java.beans.PropertyEditor JavaDoc;
20 import java.beans.PropertyEditorManager JavaDoc;
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 import javax.swing.JButton JavaDoc;
25 import javax.swing.JDialog JavaDoc;
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JOptionPane JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29
30 import com.dreambean.awt.GenericPropertyCustomizer;
31
32
33 /**
34  * @author Laurent Etiemble
35  * @version $Revision: 1.5 $
36  */

37 public class GenericMethodDialog extends JDialog JavaDoc
38     implements ActionListener JavaDoc
39 {
40    /** Description of the Field */
41    protected Vector JavaDoc editors;
42    /** Description of the Field */
43    protected MethodDescriptor JavaDoc md;
44    /** Description of the Field */
45    protected Object JavaDoc obj;
46
47
48    /** Constructor for GenericMethodDialog. */
49    public GenericMethodDialog()
50    {
51       super();
52    }
53
54
55    /**
56     *Constructor for the GenericMethodDialog object
57     *
58     * @param obj1 Description of the Parameter
59     * @param methoddescriptor Description of the Parameter
60     * @param frame Description of the Parameter
61     */

62    public GenericMethodDialog(Object JavaDoc obj1, MethodDescriptor JavaDoc methoddescriptor, Frame JavaDoc frame)
63    {
64       super(frame, true);
65       editors = new Vector JavaDoc();
66       obj = obj1;
67       md = methoddescriptor;
68       JPanel JavaDoc jpanel = (JPanel JavaDoc) getContentPane();
69       jpanel.setLayout(new GridBagLayout JavaDoc());
70       try
71       {
72          GridBagConstraints JavaDoc gridbagconstraints = new GridBagConstraints JavaDoc();
73          gridbagconstraints.insets = new Insets JavaDoc(5, 5, 5, 5);
74          gridbagconstraints.anchor = 11;
75          gridbagconstraints.weighty = 1.0D;
76          ParameterDescriptor JavaDoc aparameterdescriptor[] = null;
77          Class JavaDoc aclass[] = methoddescriptor.getMethod().getParameterTypes();
78          if (methoddescriptor.getParameterDescriptors() != null)
79          {
80             aparameterdescriptor = methoddescriptor.getParameterDescriptors();
81          }
82          else
83          {
84             aparameterdescriptor = new ParameterDescriptor JavaDoc[aclass.length];
85             for (int i = 0; i < aclass.length; i++)
86             {
87                aparameterdescriptor[i] = new ParameterDescriptor JavaDoc();
88                aparameterdescriptor[i].setName(aclass[i].getName());
89             }
90
91          }
92          JLabel JavaDoc jlabel = new JLabel JavaDoc(methoddescriptor.getDisplayName());
93          gridbagconstraints.gridwidth = 0;
94          ((GridBagLayout JavaDoc) jpanel.getLayout()).setConstraints(jlabel, gridbagconstraints);
95          jpanel.add(jlabel);
96          for (int j = 0; j < aparameterdescriptor.length; j++)
97          {
98             PropertyEditor JavaDoc propertyeditor = null;
99             String JavaDoc s = (String JavaDoc) aparameterdescriptor[j].getValue("propertyeditor");
100             if (s != null)
101             {
102                try
103                {
104                   propertyeditor = (PropertyEditor JavaDoc) Class.forName(s).newInstance();
105                }
106                catch (Exception JavaDoc _ex)
107                {
108                   continue;
109                }
110             }
111             else
112             {
113                propertyeditor = PropertyEditorManager.findEditor(aclass[j]);
114             }
115             if (propertyeditor != null)
116             {
117                JLabel JavaDoc jlabel1 = new JLabel JavaDoc(aparameterdescriptor[j].getDisplayName() + ":", 4);
118                gridbagconstraints.gridwidth = -1;
119                gridbagconstraints.fill = 0;
120                gridbagconstraints.weightx = 0.0D;
121                ((GridBagLayout JavaDoc) 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 JavaDoc _ex)
134                   {
135                   }
136                }
137                Object JavaDoc obj2;
138                if (propertyeditor.supportsCustomEditor())
139                {
140                   obj2 = propertyeditor.getCustomEditor();
141                }
142                else
143                {
144                   String JavaDoc 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 JavaDoc) jpanel.getLayout()).setConstraints(((Component JavaDoc) (obj2)), gridbagconstraints);
158                jpanel.add(((Component JavaDoc) (obj2)));
159                editors.addElement(propertyeditor);
160             }
161             gridbagconstraints.weighty = 1.0D;
162          }
163          gridbagconstraints.gridwidth = 0;
164          JPanel JavaDoc jpanel1 = new JPanel JavaDoc();
165          // TODO I18N
166
JButton JavaDoc jbutton = new JButton JavaDoc("Ok");
167          jpanel1.add(jbutton);
168          jbutton.addActionListener(this);
169          JButton JavaDoc jbutton1 = new JButton JavaDoc("Cancel");
170          jpanel1.add(jbutton1);
171          jbutton1.addActionListener(this);
172          ((GridBagLayout JavaDoc) jpanel.getLayout()).setConstraints(jpanel1, gridbagconstraints);
173          jpanel.add(jpanel1);
174          pack();
175          if (getWidth() < 300)
176          {
177             setSize(new Dimension JavaDoc(300, getHeight()));
178          }
179          setLocationRelativeTo(frame);
180          setVisible(true);
181       }
182       catch (Exception JavaDoc exception)
183       {
184          System.out.println("Exception occurred");
185          exception.printStackTrace();
186       }
187    }
188
189
190    /**
191     * Description of the Method
192     *
193     * @param actionevent Description of the Parameter
194     */

195    public void actionPerformed(ActionEvent JavaDoc actionevent)
196    {
197       if (actionevent.getActionCommand().equals("Ok"))
198       {
199          Object JavaDoc aobj[] = new Object JavaDoc[editors.size()];
200          for (int i = 0; i < aobj.length; i++)
201          {
202             aobj[i] = ((PropertyEditor JavaDoc) editors.elementAt(i)).getValue();
203          }
204          try
205          {
206             Object JavaDoc obj1 = md.getMethod().invoke(obj, aobj);
207             if (obj1 != null)
208             {
209                JOptionPane.showMessageDialog(this, obj1.toString(), "Result", 1);
210             }
211          }
212          catch (InvocationTargetException JavaDoc invocationtargetexception)
213          {
214             invocationtargetexception.getTargetException().printStackTrace();
215             JOptionPane.showMessageDialog(this, invocationtargetexception.getTargetException().getMessage(), "Error", 0);
216          }
217          catch (Exception JavaDoc 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