KickJava   Java API By Example, From Geeks To Geeks.

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


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.PropertyEditor JavaDoc;
18 import java.beans.PropertyEditorManager JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import javax.management.MBeanOperationInfo JavaDoc;
24 import javax.management.MBeanParameterInfo JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26 import javax.swing.JButton JavaDoc;
27 import javax.swing.JDialog JavaDoc;
28 import javax.swing.JLabel JavaDoc;
29 import javax.swing.JOptionPane JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.SwingConstants JavaDoc;
32
33 import org.ejtools.jmx.MBeanAccessor;
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.8 $
43  */

44 public class GenericMBeanMethodDialog extends JDialog JavaDoc implements ActionListener JavaDoc
45 {
46    /** Description of the Field */
47    private MBeanAccessor mConnector;
48
49    /** Description of the Field */
50    private Vector JavaDoc mEditors = new Vector JavaDoc();
51    /** Description of the Field */
52    private MBeanOperationInfo JavaDoc mOperation;
53    /** Description of the Field */
54    private ObjectName JavaDoc mService;
55
56
57    /**
58     * Create a new dialog.
59     *
60     * @param pConnector Connector to the remote MBean Server
61     * @param pService Remote Service Bean on which the method should be invoked
62     * @param pOperation Operation to be invoked if accepted
63     * @param pOwner Owner of the dialog controlling its appearance
64     */

65    public GenericMBeanMethodDialog(MBeanAccessor pConnector, ObjectName JavaDoc pService, MBeanOperationInfo JavaDoc pOperation, Frame JavaDoc pOwner)
66    {
67       super(pOwner, true);
68
69       // Check the parameters
70
if (pConnector == null)
71       {
72          throw new IllegalArgumentException JavaDoc("MBeanAccessor must be defined");
73       }
74       if (pService == null)
75       {
76          throw new IllegalArgumentException JavaDoc("Service must be defined");
77       }
78       if (pOperation == null)
79       {
80          throw new IllegalArgumentException JavaDoc("Operation must be defined");
81       }
82       mConnector = pConnector;
83       mService = pService;
84       mOperation = pOperation;
85
86       JPanel JavaDoc con = (JPanel JavaDoc) getContentPane();
87       con.setLayout(new GridBagLayout JavaDoc());
88
89       try
90       {
91          GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
92          c.insets = new Insets JavaDoc(3, 3, 3, 3);
93          c.anchor = GridBagConstraints.NORTH;
94          c.weighty = 1;
95
96          // Header
97
JLabel JavaDoc lName = new JLabel JavaDoc(mOperation.getName());
98          c.gridwidth = GridBagConstraints.REMAINDER;
99          con.add(lName, c);
100
101          // Parameters
102
Iterator JavaDoc i = Arrays.asList(mOperation.getSignature()).iterator();
103          while (i.hasNext())
104          {
105             MBeanParameterInfo JavaDoc lParameter = (MBeanParameterInfo JavaDoc) i.next();
106             Class JavaDoc cl = ClassTools.getClass(lParameter.getType());
107
108             lName = new JLabel JavaDoc(lParameter.getName() + ":", SwingConstants.RIGHT);
109             c.gridwidth = GridBagConstraints.RELATIVE;
110             c.fill = GridBagConstraints.NONE;
111             c.weightx = 0;
112             con.add(lName, c);
113
114             if (cl != null)
115             {
116                PropertyEditor JavaDoc lEditor = PropertyEditorManager.findEditor(cl);
117                if (lEditor != null)
118                {
119                   // Set initial value
120
if (lEditor.getTags() != null)
121                   {
122                      // Set to first value
123
lEditor.setAsText(lEditor.getTags()[0]);
124                   }
125
126                   Component JavaDoc lEditorComp;
127                   if (lEditor.supportsCustomEditor())
128                   {
129                      lEditorComp = lEditor.getCustomEditor();
130                   }
131                   else
132                   {
133                      String JavaDoc[] lTags = lEditor.getTags();
134                      if (lTags != null)
135                      {
136                         lEditorComp = new GenericPropertyCustomizer(lEditor, lTags);
137                      }
138                      else
139                      {
140                         lEditorComp = new GenericPropertyCustomizer(lEditor);
141                      }
142                   }
143
144                   c.gridwidth = GridBagConstraints.REMAINDER;
145                   c.fill = GridBagConstraints.HORIZONTAL;
146                   c.weightx = 1;
147                   con.add(lEditorComp, c);
148
149                   mEditors.addElement(new Editor JavaDoc(lEditor, lParameter.getType()));
150                }
151             }
152             else
153             {
154                Component JavaDoc lEditorComp;
155                lEditorComp = new JLabel JavaDoc("Unsupported class " + lParameter.getType());
156
157                c.gridwidth = GridBagConstraints.REMAINDER;
158                c.fill = GridBagConstraints.HORIZONTAL;
159                c.weightx = 1;
160                con.add(lEditorComp, c);
161             }
162             c.weighty = 1;
163          }
164
165          // Button
166
c.gridwidth = GridBagConstraints.REMAINDER;
167          JPanel JavaDoc p = new JPanel JavaDoc();
168          JButton JavaDoc ok = new JButton JavaDoc("Invoke");
169          p.add(ok);
170          ok.addActionListener(this);
171          JButton JavaDoc cancel = new JButton JavaDoc("Cancel");
172          p.add(cancel);
173          cancel.addActionListener(this);
174          con.add(p, c);
175
176          pack();
177          if (getWidth() < 300)
178          {
179             setSize(new Dimension JavaDoc(300, getHeight()));
180          }
181          setLocationRelativeTo(pOwner);
182          setVisible(true);
183       }
184       catch (Exception JavaDoc e)
185       {
186          System.out.println("Exception occurred");
187          e.printStackTrace();
188       }
189    }
190
191
192    // Public --------------------------------------------------------
193

194    // ActionListener implementation ---------------------------------
195
/**
196     * Ok or Cancel has been pressed.
197     *
198     * @param e the event
199     */

200    public void actionPerformed(ActionEvent JavaDoc e)
201    {
202       if (e.getActionCommand().equals("Ok"))
203       {
204          Object JavaDoc[] params = new Object JavaDoc[mEditors.size()];
205          String JavaDoc[] lTypes = new String JavaDoc[mEditors.size()];
206          Iterator JavaDoc i = mEditors.iterator();
207          int j = 0;
208          while (i.hasNext())
209          {
210             Editor JavaDoc lEditor = (Editor JavaDoc) i.next();
211             params[j] = lEditor.getEditor().getValue();
212             lTypes[j] = lEditor.getType();
213             j++;
214          }
215
216          try
217          {
218             Object JavaDoc lReturn = mConnector.invoke(
219                mOperation.getName(),
220                params,
221                lTypes
222                );
223             if (lReturn != null)
224             {
225                JOptionPane.showMessageDialog(
226                   this,
227                   lReturn.toString(),
228                   "Result",
229                   JOptionPane.INFORMATION_MESSAGE
230                   );
231             }
232          }
233          catch (Exception JavaDoc ex)
234          {
235             System.err.println(ex);
236             JOptionPane.showMessageDialog(
237                this,
238                "An exception occured. Check log for details",
239                "Error",
240                JOptionPane.ERROR_MESSAGE
241                );
242          }
243       }
244       setVisible(false);
245    }
246
247
248    /**
249     * Description of the Class
250     *
251     * @author letiembl
252     * @version $Revision: 1.8 $
253     */

254    private class Editor
255    {
256       /** Description of the Field */
257       private PropertyEditor JavaDoc mEditor;
258       /** Description of the Field */
259       private String JavaDoc mType;
260
261
262       /**
263        * Constructor for the Editor object
264        *
265        * @param pEditor Description of Parameter
266        * @param pType Description of Parameter
267        */

268       public Editor(PropertyEditor JavaDoc pEditor, String JavaDoc pType)
269       {
270          mEditor = pEditor;
271          mType = pType;
272       }
273
274
275       /**
276        * Getter for the editor attribute
277        *
278        * @return The value of editor attribute
279        */

280       public PropertyEditor JavaDoc getEditor()
281       {
282          return mEditor;
283       }
284
285
286       /**
287        * Getter for the type attribute
288        *
289        * @return The value of type attribute
290        */

291       public String JavaDoc getType()
292       {
293          return mType;
294       }
295    }
296 }
297
298
Popular Tags