|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| GenericMBeanMethodDialog.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 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;
|
|
| 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 |
/**
|
|
| 39 |
* Description of the Class
|
|
| 40 |
*
|
|
| 41 |
* @author Laurent Etiemble
|
|
| 42 |
* @version $Revision: 1.8 $
|
|
| 43 |
*/
|
|
| 44 |
public class GenericMBeanMethodDialog extends JDialog implements ActionListener |
|
| 45 |
{
|
|
| 46 |
/** Description of the Field */
|
|
| 47 |
private MBeanAccessor mConnector;
|
|
| 48 |
|
|
| 49 |
/** Description of the Field */
|
|
| 50 |
private Vector mEditors = new Vector(); |
|
| 51 |
/** Description of the Field */
|
|
| 52 |
private MBeanOperationInfo mOperation;
|
|
| 53 |
/** Description of the Field */
|
|
| 54 |
private ObjectName 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 | 0 |
public GenericMBeanMethodDialog(MBeanAccessor pConnector, ObjectName pService, MBeanOperationInfo pOperation, Frame pOwner)
|
| 66 |
{
|
|
| 67 | 0 |
super(pOwner, true); |
| 68 |
|
|
| 69 |
// Check the parameters
|
|
| 70 | 0 |
if (pConnector == null) |
| 71 |
{
|
|
| 72 | 0 |
throw new IllegalArgumentException("MBeanAccessor must be defined"); |
| 73 |
} |
|
| 74 | 0 |
if (pService == null) |
| 75 |
{
|
|
| 76 | 0 |
throw new IllegalArgumentException("Service must be defined"); |
| 77 |
} |
|
| 78 | 0 |
if (pOperation == null) |
| 79 |
{
|
|
| 80 | 0 |
throw new IllegalArgumentException("Operation must be defined"); |
| 81 |
} |
|
| 82 | 0 |
mConnector = pConnector; |
| 83 | 0 |
mService = pService; |
| 84 | 0 |
mOperation = pOperation; |
| 85 |
|
|
| 86 | 0 |
JPanel con = (JPanel) getContentPane(); |
| 87 | 0 |
con.setLayout(new GridBagLayout());
|
| 88 |
|
|
| 89 | 0 |
try
|
| 90 |
{
|
|
| 91 | 0 |
GridBagConstraints c = new GridBagConstraints();
|
| 92 | 0 |
c.insets = new Insets(3, 3, 3, 3);
|
| 93 | 0 |
c.anchor = GridBagConstraints.NORTH; |
| 94 | 0 |
c.weighty = 1; |
| 95 |
|
|
| 96 |
// Header
|
|
| 97 | 0 |
JLabel lName = new JLabel(mOperation.getName());
|
| 98 | 0 |
c.gridwidth = GridBagConstraints.REMAINDER; |
| 99 | 0 |
con.add(lName, c); |
| 100 |
|
|
| 101 |
// Parameters
|
|
| 102 | 0 |
Iterator i = Arrays.asList(mOperation.getSignature()).iterator(); |
| 103 | 0 |
while (i.hasNext())
|
| 104 |
{
|
|
| 105 | 0 |
MBeanParameterInfo lParameter = (MBeanParameterInfo) i.next(); |
| 106 | 0 |
Class cl = ClassTools.getClass(lParameter.getType()); |
| 107 |
|
|
| 108 | 0 |
lName = new JLabel(lParameter.getName() + ":", SwingConstants.RIGHT); |
| 109 | 0 |
c.gridwidth = GridBagConstraints.RELATIVE; |
| 110 | 0 |
c.fill = GridBagConstraints.NONE; |
| 111 | 0 |
c.weightx = 0; |
| 112 | 0 |
con.add(lName, c); |
| 113 |
|
|
| 114 | 0 |
if (cl != null) |
| 115 |
{
|
|
| 116 | 0 |
PropertyEditor lEditor = PropertyEditorManager.findEditor(cl); |
| 117 | 0 |
if (lEditor != null) |
| 118 |
{
|
|
| 119 |
// Set initial value
|
|
| 120 | 0 |
if (lEditor.getTags() != null) |
| 121 |
{
|
|
| 122 |
// Set to first value
|
|
| 123 | 0 |
lEditor.setAsText(lEditor.getTags()[0]); |
| 124 |
} |
|
| 125 |
|
|
| 126 | 0 |
Component lEditorComp; |
| 127 | 0 |
if (lEditor.supportsCustomEditor())
|
| 128 |
{
|
|
| 129 | 0 |
lEditorComp = lEditor.getCustomEditor(); |
| 130 |
} |
|
| 131 |
else
|
|
| 132 |
{
|
|
| 133 | 0 |
String[] lTags = lEditor.getTags(); |
| 134 | 0 |
if (lTags != null) |
| 135 |
{
|
|
| 136 | 0 |
lEditorComp = new GenericPropertyCustomizer(lEditor, lTags);
|
| 137 |
} |
|
| 138 |
else
|
|
| 139 |
{
|
|
| 140 | 0 |
lEditorComp = new GenericPropertyCustomizer(lEditor);
|
| 141 |
} |
|
| 142 |
} |
|
| 143 |
|
|
| 144 | 0 |
c.gridwidth = GridBagConstraints.REMAINDER; |
| 145 | 0 |
c.fill = GridBagConstraints.HORIZONTAL; |
| 146 | 0 |
c.weightx = 1; |
| 147 | 0 |
con.add(lEditorComp, c); |
| 148 |
|
|
| 149 | 0 |
mEditors.addElement(new Editor(lEditor, lParameter.getType()));
|
| 150 |
} |
|
| 151 |
} |
|
| 152 |
else
|
|
| 153 |
{
|
|
| 154 | 0 |
Component lEditorComp; |
| 155 | 0 |
lEditorComp = new JLabel("Unsupported class " + lParameter.getType()); |
| 156 |
|
|
| 157 | 0 |
c.gridwidth = GridBagConstraints.REMAINDER; |
| 158 | 0 |
c.fill = GridBagConstraints.HORIZONTAL; |
| 159 | 0 |
c.weightx = 1; |
| 160 | 0 |
con.add(lEditorComp, c); |
| 161 |
} |
|
| 162 | 0 |
c.weighty = 1; |
| 163 |
} |
|
| 164 |
|
|
| 165 |
// Button
|
|
| 166 | 0 |
c.gridwidth = GridBagConstraints.REMAINDER; |
| 167 | 0 |
JPanel p = new JPanel();
|
| 168 | 0 |
JButton ok = new JButton("Invoke"); |
| 169 | 0 |
p.add(ok); |
| 170 | 0 |
ok.addActionListener(this);
|
| 171 | 0 |
JButton cancel = new JButton("Cancel"); |
| 172 | 0 |
p.add(cancel); |
| 173 | 0 |
cancel.addActionListener(this);
|
| 174 | 0 |
con.add(p, c); |
| 175 |
|
|
| 176 | 0 |
pack(); |
| 177 | 0 |
if (getWidth() < 300)
|
| 178 |
{
|
|
| 179 | 0 |
setSize(new Dimension(300, getHeight()));
|
| 180 |
} |
|
| 181 | 0 |
setLocationRelativeTo(pOwner); |
| 182 | 0 |
setVisible(true);
|
| 183 |
} |
|
| 184 |
catch (Exception e)
|
|
| 185 |
{
|
|
| 186 | 0 |
System.out.println("Exception occurred");
|
| 187 | 0 |
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 | 0 |
public void actionPerformed(ActionEvent e) |
| 201 |
{
|
|
| 202 | 0 |
if (e.getActionCommand().equals("Ok")) |
| 203 |
{
|
|
| 204 | 0 |
Object[] params = new Object[mEditors.size()];
|
| 205 | 0 |
String[] lTypes = new String[mEditors.size()];
|
| 206 | 0 |
Iterator i = mEditors.iterator(); |
| 207 | 0 |
int j = 0;
|
| 208 | 0 |
while (i.hasNext())
|
| 209 |
{
|
|
| 210 | 0 |
Editor lEditor = (Editor) i.next(); |
| 211 | 0 |
params[j] = lEditor.getEditor().getValue(); |
| 212 | 0 |
lTypes[j] = lEditor.getType(); |
| 213 | 0 |
j++; |
| 214 |
} |
|
| 215 |
|
|
| 216 | 0 |
try
|
| 217 |
{
|
|
| 218 | 0 |
Object lReturn = mConnector.invoke( |
| 219 |
mOperation.getName(), |
|
| 220 |
params, |
|
| 221 |
lTypes |
|
| 222 |
); |
|
| 223 | 0 |
if (lReturn != null) |
| 224 |
{
|
|
| 225 | 0 |
JOptionPane.showMessageDialog( |
| 226 |
this,
|
|
| 227 |
lReturn.toString(), |
|
| 228 |
"Result",
|
|
| 229 |
JOptionPane.INFORMATION_MESSAGE |
|
| 230 |
); |
|
| 231 |
} |
|
| 232 |
} |
|
| 233 |
catch (Exception ex)
|
|
| 234 |
{
|
|
| 235 | 0 |
System.err.println(ex); |
| 236 | 0 |
JOptionPane.showMessageDialog( |
| 237 |
this,
|
|
| 238 |
"An exception occured. Check log for details",
|
|
| 239 |
"Error",
|
|
| 240 |
JOptionPane.ERROR_MESSAGE |
|
| 241 |
); |
|
| 242 |
} |
|
| 243 |
} |
|
| 244 | 0 |
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 mEditor;
|
|
| 258 |
/** Description of the Field */
|
|
| 259 |
private String 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 | 0 |
public Editor(PropertyEditor pEditor, String pType)
|
| 269 |
{
|
|
| 270 | 0 |
mEditor = pEditor; |
| 271 | 0 |
mType = pType; |
| 272 |
} |
|
| 273 |
|
|
| 274 |
|
|
| 275 |
/**
|
|
| 276 |
* Getter for the editor attribute
|
|
| 277 |
*
|
|
| 278 |
* @return The value of editor attribute
|
|
| 279 |
*/
|
|
| 280 | 0 |
public PropertyEditor getEditor()
|
| 281 |
{
|
|
| 282 | 0 |
return mEditor;
|
| 283 |
} |
|
| 284 |
|
|
| 285 |
|
|
| 286 |
/**
|
|
| 287 |
* Getter for the type attribute
|
|
| 288 |
*
|
|
| 289 |
* @return The value of type attribute
|
|
| 290 |
*/
|
|
| 291 | 0 |
public String getType()
|
| 292 |
{
|
|
| 293 | 0 |
return mType;
|
| 294 |
} |
|
| 295 |
} |
|
| 296 |
} |
|
| 297 |
|
|
| 298 |
|
|
||||||||||