1 5 package net.sf.panoptes.view.swing.editors; 6 7 import java.awt.Frame ; 8 import java.awt.HeadlessException ; 9 import java.awt.event.ActionEvent ; 10 import java.awt.event.ActionListener ; 11 import java.beans.PropertyEditorManager ; 12 import java.lang.reflect.Method ; 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 import javax.management.MBeanOperationInfo ; 17 import javax.management.MBeanParameterInfo ; 18 import javax.swing.JFrame ; 19 import javax.swing.JOptionPane ; 20 import javax.swing.UIManager ; 21 22 import net.sf.panoptes.component.jmx.model.MBeanWrapper; 23 import net.sf.panoptes.controller.MainController; 24 import net.sf.panoptes.swing.DefaultFormBuilder; 25 import net.sf.panoptes.swing.dialogs.BannerDialog; 26 import net.sf.panoptes.swing.dialogs.DialogUtil; 27 import net.sf.panoptes.swing.panels.OKCancelButtonPanel; 28 import net.sf.panoptes.swing.propeditors.PropertyEditor; 29 import net.sf.panoptes.swing.propeditors.PropertyEditorRegistry; 30 31 37 public class MethodInvocationDialog extends BannerDialog { 38 39 private ArrayList editors = new ArrayList (); 40 private Object node; 41 private Method operation; 42 43 public MethodInvocationDialog( 44 Frame owner, 45 String title, final Object node, final Method operation) 46 throws HeadlessException { 47 super(owner, title, "MBean Operation Invocation", "Enter parameters for the invocation or drag attributes / MBeans onto the form", "icons/banners/new_wiz.gif", true, false); 48 49 this.node = node; 50 this.operation = operation; 51 52 OKCancelButtonPanel buttonPanel = new OKCancelButtonPanel(); 53 buttonPanel.getOKButton().addActionListener(new ActionListener () { 54 public void actionPerformed(ActionEvent e) { 55 doInvocation(); 56 } 57 }); 58 buttonPanel.getCancelButton().addActionListener(new ActionListener () { 59 public void actionPerformed(ActionEvent e) { 60 setVisible(false); 61 } 62 }); 63 64 setButtonPanel(buttonPanel.getPanel()); 65 66 67 DefaultFormBuilder builder = new DefaultFormBuilder(DialogUtil.defaultOneColumnLayout()); 68 69 builder.appendSeparator("Parameters:"); 70 Class [] sig = operation.getParameterTypes(); 71 for (int i = 0; i < sig.length; i++) { 72 Class [] ed = PropertyEditorRegistry.getEditor(sig[i].getName()); 73 if (ed.length > 0) try { 74 PropertyEditor editor = (PropertyEditor) ed[0].newInstance(); 75 editors.add(editor); 76 builder.append(sig[i].getName() + ":", editor.getComponent()); 77 } catch (Exception e) { 78 e.printStackTrace(); 79 } 80 } 81 82 setMainPanel(builder.getPanel()); 83 84 } 85 86 private void doInvocation() { 87 Object [] params = new Object [editors.size()]; 88 String [] types = new String [editors.size()]; 89 int j = 0; 90 for (Iterator i = editors.iterator(); i.hasNext(); ) { 91 params[j] = ((PropertyEditor) i.next()).getValue(); 92 types[j] = operation.getParameterTypes()[j].getName(); 93 j++; 94 95 } 96 try { 97 Object result = operation.invoke(node, params); 98 setVisible(false); 99 String s; 100 if (result == null) s = null; 101 else s = result.toString(); 102 JOptionPane.showMessageDialog(this, s, "Invocation result", JOptionPane.INFORMATION_MESSAGE); 103 } catch (Exception e) { 104 MainController.getInstance().displayError("Exception while invoking", e.getMessage(), e); 105 } 106 107 } 108 109 public static void main(String [] args) { 110 111 System.out.println(PropertyEditorManager.findEditor(Integer .class)); 112 113 try { 114 UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel"); 115 } catch (Exception e) { 116 e.printStackTrace(); 117 } 118 119 MethodInvocationDialog d = 120 new MethodInvocationDialog( 121 new JFrame (), "Invoke operation X", null, null); 122 d.pack(); 123 d.show(); 124 System.exit(0); 125 } 126 127 } 128 | Popular Tags |