1 24 25 package org.objectweb.cjdbc.console.gui.frames.jmxdesktop; 26 27 import java.awt.Color ; 28 import java.awt.GridBagConstraints ; 29 import java.awt.GridBagLayout ; 30 import java.awt.GridLayout ; 31 import java.awt.HeadlessException ; 32 import java.awt.event.ActionEvent ; 33 import java.awt.event.ActionListener ; 34 import java.io.PrintWriter ; 35 36 import javax.management.MBeanOperationInfo ; 37 import javax.management.MBeanParameterInfo ; 38 import javax.management.ObjectName ; 39 import javax.swing.BorderFactory ; 40 import javax.swing.JButton ; 41 import javax.swing.JDialog ; 42 import javax.swing.JLabel ; 43 import javax.swing.JPanel ; 44 import javax.swing.JScrollPane ; 45 import javax.swing.JTextArea ; 46 import javax.swing.JTextField ; 47 48 import org.objectweb.cjdbc.common.jmx.JmxConstants; 49 import org.objectweb.cjdbc.console.gui.CjdbcGui; 50 import org.objectweb.cjdbc.console.gui.constants.GuiCommands; 51 import org.objectweb.cjdbc.console.gui.constants.GuiConstants; 52 import org.objectweb.cjdbc.console.gui.jtools.JTextAreaWriter; 53 54 60 public class OperationCallDialog extends JDialog implements ActionListener 61 { 62 63 CjdbcGui gui; 64 ObjectName objectN; 65 MBeanOperationInfo info; 66 private JTextArea area; 67 private JScrollPane scrollPane; 68 private MBeanParameterInfo [] params; 69 private int length; 70 private JTextField [] fields; 71 72 80 public OperationCallDialog(CjdbcGui gui, ObjectName name, 81 MBeanOperationInfo info) throws HeadlessException 82 { 83 super(gui, "Operation Call Dialog", true); 84 GuiConstants.centerComponent(this, 400, 500); 85 this.gui = gui; 86 this.objectN = name; 87 this.info = info; 88 89 GridBagLayout gbl = new GridBagLayout (); 91 this.getContentPane().setLayout(gbl); 92 this.setFont(GuiConstants.CENTER_PANE_FONT); 93 GridBagConstraints gbc = new GridBagConstraints (); 94 gbc.fill = GridBagConstraints.BOTH; 95 gbc.weightx = 1.0; 96 gbc.gridheight = 1; 97 gbc.gridwidth = GridBagConstraints.REMAINDER; 98 this.getContentPane().setBackground(Color.white); 99 100 JLabel objectName = new JLabel (name.toString()); 101 objectName.setBorder(BorderFactory.createTitledBorder( 102 GuiConstants.LINE_BORDER, "MBean Name")); 103 gbl.setConstraints(objectName, gbc); 104 this.getContentPane().add(objectName); 105 106 scrollPane = new JScrollPane (); 107 scrollPane.setBackground(Color.white); 108 scrollPane.setViewportBorder(BorderFactory.createTitledBorder( 109 GuiConstants.LINE_BORDER, "Operation Result")); 110 area = new JTextArea (); 111 area.setBackground(Color.white); 112 scrollPane.getViewport().add(area); 113 114 JLabel operation = new JLabel (info.getName()); 115 operation.setBorder(BorderFactory.createTitledBorder( 116 GuiConstants.LINE_BORDER, "Operation Name")); 117 gbl.setConstraints(operation, gbc); 118 this.getContentPane().add(operation); 119 120 JPanel operationPane = new JPanel (); 121 operationPane.setBackground(Color.white); 122 operationPane.setBorder(BorderFactory.createTitledBorder( 123 GuiConstants.LINE_BORDER, "MBean parameters")); 124 params = info.getSignature(); 125 length = params.length; 126 GridLayout gl = new GridLayout (info.getSignature().length, 2); 127 operationPane.setLayout(gl); 128 fields = new JTextField [length]; 129 for (int i = 0; i < length; i++) 130 { 131 operationPane.add(new JLabel (params[i].getType())); 132 fields[i] = new JTextField (""); 133 operationPane.add(fields[i]); 134 } 135 gbl.setConstraints(operationPane, gbc); 136 this.getContentPane().add(operationPane); 137 138 JButton ok = new JButton ("Run Jmx operation"); 139 ok.setBackground(new Color (198, 226, 255)); 140 ok.setActionCommand(GuiCommands.COMMAND_CONFIRM_ACTION); 141 ok.addActionListener(this); 142 gbl.setConstraints(ok, gbc); 143 this.getContentPane().add(ok); 144 145 gbc.gridheight = GridBagConstraints.REMAINDER; 146 gbc.weighty = 2.0; 147 gbl.setConstraints(scrollPane, gbc); 148 this.getContentPane().add(scrollPane); 149 150 this.validate(); 151 152 } 153 154 157 public void actionPerformed(ActionEvent event) 158 { 159 try 160 { 161 area.setForeground(Color.BLACK); 162 if (gui.getCurrentJmxClient().isSubjectSet() == false 163 && JmxConstants.mbeanNeedAuthentication(objectN)) 164 new SetSubjectDialog(gui); 165 Object [] args = new Object [length]; 166 for (int i = 0; i < length; i++) 167 { 168 args[i] = getParameter(i); 169 } 170 Object result = gui.getCurrentJmxClient().invokeOperation(objectN, info, 171 args); 172 if (result != null) 173 area.setText(result.toString()); 174 else 175 area.setText("Command did not return a result"); 176 177 area.validate(); 178 scrollPane.validate(); 179 } 180 catch (Exception e) 181 { 182 area.setForeground(Color.RED); 183 area.setText(e.getMessage()); 184 JTextAreaWriter areaWriter = new JTextAreaWriter(area); 185 PrintWriter writer = new PrintWriter (areaWriter); 186 e.printStackTrace(writer); 187 } 188 } 189 190 private Object getParameter(int i) 191 { 192 String value = fields[i].getText(); 193 String type = params[i].getType(); 194 return GuiConstants.convertType(value, type); 195 } 196 197 } 198 | Popular Tags |