1 7 package org.ejtools.adwt.jmx; 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.util.Arrays ; 19 import java.util.Iterator ; 20 import java.util.ResourceBundle ; 21 import java.util.Vector ; 22 23 import javax.management.MBeanOperationInfo ; 24 import javax.management.MBeanParameterInfo ; 25 import javax.swing.JButton ; 26 import javax.swing.JDialog ; 27 import javax.swing.JLabel ; 28 import javax.swing.JOptionPane ; 29 import javax.swing.JPanel ; 30 import javax.swing.SwingConstants ; 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 44 public class MBeanMethodDialog extends JDialog implements ActionListener 45 { 46 47 private Vector editors = new Vector (); 48 49 private MBeanOperationInfo operationInfo = null; 50 51 private MBeanInvokeAccessor resource = null; 52 53 private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.adwt.jmx.Resources"); 54 55 56 63 public MBeanMethodDialog(MBeanInvokeAccessor resource, MBeanOperationInfo operationInfo, Frame parent) 64 { 65 super(parent, true); 66 this.setTitle(resources.getString("invoke.dialog.title")); 67 68 if (resource == null) 70 { 71 throw new IllegalArgumentException ("MBeanAccessor must be defined"); 72 } 73 if (operationInfo == null) 74 { 75 throw new IllegalArgumentException ("Operation must be defined"); 76 } 77 this.resource = resource; 78 this.operationInfo = operationInfo; 79 80 JPanel panel = (JPanel ) this.getContentPane(); 81 panel.setLayout(new GridBagLayout ()); 82 83 try 84 { 85 GridBagConstraints constraints = new GridBagConstraints (); 86 constraints.insets = new Insets (2, 2, 2, 2); 87 constraints.anchor = GridBagConstraints.NORTH; 88 constraints.weighty = 1.0d; 89 90 StringBuffer display = new StringBuffer (); 92 display.append(resources.getString("invoke.dialog.text.operation") + " : "); 93 display.append(this.operationInfo.getName()); 94 JLabel nameLabel = new JLabel (display.toString()); 95 constraints.gridwidth = GridBagConstraints.REMAINDER; 96 panel.add(nameLabel, constraints); 97 98 Iterator i = Arrays.asList(this.operationInfo.getSignature()).iterator(); 100 while (i.hasNext()) 101 { 102 MBeanParameterInfo parameter = (MBeanParameterInfo ) i.next(); 103 Class clazz = ClassTools.getClass(parameter.getType()); 104 105 nameLabel = new JLabel (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 editor = null; 112 if (clazz != null) 113 { 114 editor = CustomPropertyEditorManager.findEditor(clazz); 115 if (editor != null) 116 { 117 if (editor.getTags() != null) 119 { 120 editor.setAsText(editor.getTags()[0]); 122 } 123 124 Component component; 125 if (editor.supportsCustomEditor()) 126 { 127 component = editor.getCustomEditor(); 128 } 129 else 130 { 131 String [] 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 (editor, parameter.getType())); 148 } 149 } 150 if (editor == null) 151 { 152 Component component; 153 component = new JLabel (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 constraints.gridwidth = GridBagConstraints.REMAINDER; 165 JPanel buttons = new JPanel (); 166 JButton invokeButton = new JButton (resources.getString("invoke.dialog.button.invoke")); 167 invokeButton.setActionCommand("INVOKE"); 168 buttons.add(invokeButton); 169 invokeButton.addActionListener(this); 170 JButton cancelButton = new JButton (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 (300, getHeight())); 184 } 185 this.setLocationRelativeTo(parent); 186 } 187 catch (Exception e) 188 { 189 e.printStackTrace(); 190 } 191 } 192 193 194 199 public void actionPerformed(ActionEvent e) 200 { 201 if (e.getActionCommand().equals("INVOKE")) 202 { 203 Object [] parameters = new Object [this.editors.size()]; 204 String [] types = new String [this.editors.size()]; 205 int j = 0; 206 for (Iterator i = this.editors.iterator(); i.hasNext(); j++) 207 { 208 Editor editor = (Editor ) i.next(); 209 parameters[j] = editor.getEditor().getValue(); 210 types[j] = editor.getType(); 211 } 212 213 try 214 { 215 Object result = resource.invoke(this.operationInfo.getName(), parameters, types); 216 } 217 catch (Exception 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 232 private class Editor 233 { 234 235 private PropertyEditor editor; 236 237 private String type; 238 239 240 246 public Editor(PropertyEditor editor, String type) 247 { 248 this.editor = editor; 249 this.type = type; 250 } 251 252 253 258 public PropertyEditor getEditor() 259 { 260 return this.editor; 261 } 262 263 264 269 public String getType() 270 { 271 return this.type; 272 } 273 } 274 275 } 276 277 | Popular Tags |