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.PropertyEditor ; 18 import java.beans.PropertyEditorManager ; 19 import java.util.Arrays ; 20 import java.util.Iterator ; 21 import java.util.Vector ; 22 23 import javax.management.MBeanOperationInfo ; 24 import javax.management.MBeanParameterInfo ; 25 import javax.management.ObjectName ; 26 import javax.swing.JButton ; 27 import javax.swing.JDialog ; 28 import javax.swing.JLabel ; 29 import javax.swing.JOptionPane ; 30 import javax.swing.JPanel ; 31 import javax.swing.SwingConstants ; 32 33 import org.ejtools.jmx.MBeanAccessor; 34 import org.ejtools.util.ClassTools; 35 36 import com.dreambean.awt.GenericPropertyCustomizer; 37 38 44 public class GenericMBeanMethodDialog extends JDialog implements ActionListener 45 { 46 47 private MBeanAccessor mConnector; 48 49 50 private Vector mEditors = new Vector (); 51 52 private MBeanOperationInfo mOperation; 53 54 private ObjectName mService; 55 56 57 65 public GenericMBeanMethodDialog(MBeanAccessor pConnector, ObjectName pService, MBeanOperationInfo pOperation, Frame pOwner) 66 { 67 super(pOwner, true); 68 69 if (pConnector == null) 71 { 72 throw new IllegalArgumentException ("MBeanAccessor must be defined"); 73 } 74 if (pService == null) 75 { 76 throw new IllegalArgumentException ("Service must be defined"); 77 } 78 if (pOperation == null) 79 { 80 throw new IllegalArgumentException ("Operation must be defined"); 81 } 82 mConnector = pConnector; 83 mService = pService; 84 mOperation = pOperation; 85 86 JPanel con = (JPanel ) getContentPane(); 87 con.setLayout(new GridBagLayout ()); 88 89 try 90 { 91 GridBagConstraints c = new GridBagConstraints (); 92 c.insets = new Insets (3, 3, 3, 3); 93 c.anchor = GridBagConstraints.NORTH; 94 c.weighty = 1; 95 96 JLabel lName = new JLabel (mOperation.getName()); 98 c.gridwidth = GridBagConstraints.REMAINDER; 99 con.add(lName, c); 100 101 Iterator i = Arrays.asList(mOperation.getSignature()).iterator(); 103 while (i.hasNext()) 104 { 105 MBeanParameterInfo lParameter = (MBeanParameterInfo ) i.next(); 106 Class cl = ClassTools.getClass(lParameter.getType()); 107 108 lName = new JLabel (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 lEditor = PropertyEditorManager.findEditor(cl); 117 if (lEditor != null) 118 { 119 if (lEditor.getTags() != null) 121 { 122 lEditor.setAsText(lEditor.getTags()[0]); 124 } 125 126 Component lEditorComp; 127 if (lEditor.supportsCustomEditor()) 128 { 129 lEditorComp = lEditor.getCustomEditor(); 130 } 131 else 132 { 133 String [] 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 (lEditor, lParameter.getType())); 150 } 151 } 152 else 153 { 154 Component lEditorComp; 155 lEditorComp = new JLabel ("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 c.gridwidth = GridBagConstraints.REMAINDER; 167 JPanel p = new JPanel (); 168 JButton ok = new JButton ("Invoke"); 169 p.add(ok); 170 ok.addActionListener(this); 171 JButton cancel = new JButton ("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 (300, getHeight())); 180 } 181 setLocationRelativeTo(pOwner); 182 setVisible(true); 183 } 184 catch (Exception e) 185 { 186 System.out.println("Exception occurred"); 187 e.printStackTrace(); 188 } 189 } 190 191 192 194 200 public void actionPerformed(ActionEvent e) 201 { 202 if (e.getActionCommand().equals("Ok")) 203 { 204 Object [] params = new Object [mEditors.size()]; 205 String [] lTypes = new String [mEditors.size()]; 206 Iterator i = mEditors.iterator(); 207 int j = 0; 208 while (i.hasNext()) 209 { 210 Editor lEditor = (Editor ) i.next(); 211 params[j] = lEditor.getEditor().getValue(); 212 lTypes[j] = lEditor.getType(); 213 j++; 214 } 215 216 try 217 { 218 Object 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 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 254 private class Editor 255 { 256 257 private PropertyEditor mEditor; 258 259 private String mType; 260 261 262 268 public Editor(PropertyEditor pEditor, String pType) 269 { 270 mEditor = pEditor; 271 mType = pType; 272 } 273 274 275 280 public PropertyEditor getEditor() 281 { 282 return mEditor; 283 } 284 285 286 291 public String getType() 292 { 293 return mType; 294 } 295 } 296 } 297 298 | Popular Tags |