KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > adwt > jmx > MBeanMethodDialog


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.jmx;
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.PropertyEditor JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.ResourceBundle JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import javax.management.MBeanOperationInfo JavaDoc;
24 import javax.management.MBeanParameterInfo JavaDoc;
25 import javax.swing.JButton JavaDoc;
26 import javax.swing.JDialog JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import javax.swing.JOptionPane JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30 import javax.swing.SwingConstants JavaDoc;
31
32 import org.ejtools.beans.CustomPropertyEditorManager;
33 import org.ejtools.jmx.MBeanInvokeAccessor;
34 import org.ejtools.util.ClassTools;
35
36 import com.dreambean.awt.GenericPropertyCustomizer;
37
38 /**
39  * Description of the Class
40  *
41  * @author Laurent Etiemble
42  * @version $Revision: 1.3 $
43  */

44 public class MBeanMethodDialog extends JDialog JavaDoc implements ActionListener JavaDoc
45 {
46    /** Description of the Field */
47    private Vector JavaDoc editors = new Vector JavaDoc();
48    /** Description of the Field */
49    private MBeanOperationInfo JavaDoc operationInfo = null;
50    /** Description of the Field */
51    private MBeanInvokeAccessor resource = null;
52    /** Description of the Field */
53    private static ResourceBundle JavaDoc resources = ResourceBundle.getBundle("org.ejtools.adwt.jmx.Resources");
54
55
56    /**
57     * Create a new dialog.
58     *
59     * @param resource Description of the Parameter
60     * @param operationInfo Description of the Parameter
61     * @param parent Description of the Parameter
62     */

63    public MBeanMethodDialog(MBeanInvokeAccessor resource, MBeanOperationInfo JavaDoc operationInfo, Frame JavaDoc parent)
64    {
65       super(parent, true);
66       this.setTitle(resources.getString("invoke.dialog.title"));
67
68       // Check the parameters
69
if (resource == null)
70       {
71          throw new IllegalArgumentException JavaDoc("MBeanAccessor must be defined");
72       }
73       if (operationInfo == null)
74       {
75          throw new IllegalArgumentException JavaDoc("Operation must be defined");
76       }
77       this.resource = resource;
78       this.operationInfo = operationInfo;
79
80       JPanel JavaDoc panel = (JPanel JavaDoc) this.getContentPane();
81       panel.setLayout(new GridBagLayout JavaDoc());
82
83       try
84       {
85          GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
86          constraints.insets = new Insets JavaDoc(2, 2, 2, 2);
87          constraints.anchor = GridBagConstraints.NORTH;
88          constraints.weighty = 1.0d;
89
90          // Header
91
StringBuffer JavaDoc display = new StringBuffer JavaDoc();
92          display.append(resources.getString("invoke.dialog.text.operation") + " : ");
93          display.append(this.operationInfo.getName());
94          JLabel JavaDoc nameLabel = new JLabel JavaDoc(display.toString());
95          constraints.gridwidth = GridBagConstraints.REMAINDER;
96          panel.add(nameLabel, constraints);
97
98          // Parameters
99
Iterator JavaDoc i = Arrays.asList(this.operationInfo.getSignature()).iterator();
100          while (i.hasNext())
101          {
102             MBeanParameterInfo JavaDoc parameter = (MBeanParameterInfo JavaDoc) i.next();
103             Class JavaDoc clazz = ClassTools.getClass(parameter.getType());
104
105             nameLabel = new JLabel JavaDoc(parameter.getName() + ":", SwingConstants.RIGHT);
106             constraints.gridwidth = GridBagConstraints.RELATIVE;
107             constraints.fill = GridBagConstraints.NONE;
108             constraints.weightx = 0.0d;
109             panel.add(nameLabel, constraints);
110
111             PropertyEditor JavaDoc editor = null;
112             if (clazz != null)
113             {
114                editor = CustomPropertyEditorManager.findEditor(clazz);
115                if (editor != null)
116                {
117                   // Set initial value
118
if (editor.getTags() != null)
119                   {
120                      // Set to first value
121
editor.setAsText(editor.getTags()[0]);
122                   }
123
124                   Component JavaDoc component;
125                   if (editor.supportsCustomEditor())
126                   {
127                      component = editor.getCustomEditor();
128                   }
129                   else
130                   {
131                      String JavaDoc[] lTags = editor.getTags();
132                      if (lTags != null)
133                      {
134                         component = new GenericPropertyCustomizer(editor, lTags);
135                      }
136                      else
137                      {
138                         component = new GenericPropertyCustomizer(editor);
139                      }
140                   }
141
142                   constraints.gridwidth = GridBagConstraints.REMAINDER;
143                   constraints.fill = GridBagConstraints.HORIZONTAL;
144                   constraints.weightx = 1.0d;
145                   panel.add(component, constraints);
146
147                   this.editors.addElement(new Editor JavaDoc(editor, parameter.getType()));
148                }
149             }
150             if (editor == null)
151             {
152                Component JavaDoc component;
153                component = new JLabel JavaDoc(resources.getString("invoke.dialog.text.unloadable.class") + " " + parameter.getType());
154
155                constraints.gridwidth = GridBagConstraints.REMAINDER;
156                constraints.fill = GridBagConstraints.HORIZONTAL;
157                constraints.weightx = 1.0d;
158                panel.add(component, constraints);
159             }
160             constraints.weighty = 1.0d;
161          }
162
163          // Button
164
constraints.gridwidth = GridBagConstraints.REMAINDER;
165          JPanel JavaDoc buttons = new JPanel JavaDoc();
166          JButton JavaDoc invokeButton = new JButton JavaDoc(resources.getString("invoke.dialog.button.invoke"));
167          invokeButton.setActionCommand("INVOKE");
168          buttons.add(invokeButton);
169          invokeButton.addActionListener(this);
170          JButton JavaDoc cancelButton = new JButton JavaDoc(resources.getString("invoke.dialog.button.cancel"));
171          buttons.add(cancelButton);
172          cancelButton.addActionListener(this);
173          panel.add(buttons, constraints);
174
175          if (this.editors.size() != this.operationInfo.getSignature().length)
176          {
177             invokeButton.setEnabled(false);
178          }
179
180          this.pack();
181          if (this.getWidth() < 300)
182          {
183             this.setSize(new Dimension JavaDoc(300, getHeight()));
184          }
185          this.setLocationRelativeTo(parent);
186       }
187       catch (Exception JavaDoc e)
188       {
189          e.printStackTrace();
190       }
191    }
192
193
194    /**
195     * Ok or Cancel has been pressed.
196     *
197     * @param e the event
198     */

199    public void actionPerformed(ActionEvent JavaDoc e)
200    {
201       if (e.getActionCommand().equals("INVOKE"))
202       {
203          Object JavaDoc[] parameters = new Object JavaDoc[this.editors.size()];
204          String JavaDoc[] types = new String JavaDoc[this.editors.size()];
205          int j = 0;
206          for (Iterator JavaDoc i = this.editors.iterator(); i.hasNext(); j++)
207          {
208             Editor JavaDoc editor = (Editor JavaDoc) i.next();
209             parameters[j] = editor.getEditor().getValue();
210             types[j] = editor.getType();
211          }
212
213          try
214          {
215             Object JavaDoc result = resource.invoke(this.operationInfo.getName(), parameters, types);
216          }
217          catch (Exception JavaDoc ex)
218          {
219             JOptionPane.showMessageDialog(this, resources.getString("invoke.dialog.text.error"), resources.getString("invoke.dialog.title.error"), JOptionPane.ERROR_MESSAGE);
220          }
221       }
222       this.setVisible(false);
223    }
224
225
226    /**
227     * Description of the Class
228     *
229     * @author letiembl
230     * @version $Revision: 1.3 $
231     */

232    private class Editor
233    {
234       /** Description of the Field */
235       private PropertyEditor JavaDoc editor;
236       /** Description of the Field */
237       private String JavaDoc type;
238
239
240       /**
241        * Constructor for the Editor object
242        *
243        * @param editor Description of the Parameter
244        * @param type Description of the Parameter
245        */

246       public Editor(PropertyEditor JavaDoc editor, String JavaDoc type)
247       {
248          this.editor = editor;
249          this.type = type;
250       }
251
252
253       /**
254        * Getter for the editor attribute
255        *
256        * @return The value of editor attribute
257        */

258       public PropertyEditor JavaDoc getEditor()
259       {
260          return this.editor;
261       }
262
263
264       /**
265        * Getter for the type attribute
266        *
267        * @return The value of type attribute
268        */

269       public String JavaDoc getType()
270       {
271          return this.type;
272       }
273    }
274
275 }
276
277
Popular Tags