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.event.ActionEvent ; 32 import java.awt.event.ActionListener ; 33 34 import javax.management.MBeanAttributeInfo ; 35 import javax.management.ObjectName ; 36 import javax.swing.BorderFactory ; 37 import javax.swing.JButton ; 38 import javax.swing.JDialog ; 39 import javax.swing.JLabel ; 40 import javax.swing.JPanel ; 41 import javax.swing.JScrollPane ; 42 import javax.swing.JTextArea ; 43 import javax.swing.JTextField ; 44 45 import org.objectweb.cjdbc.console.gui.CjdbcGui; 46 import org.objectweb.cjdbc.console.gui.constants.GuiCommands; 47 import org.objectweb.cjdbc.console.gui.constants.GuiConstants; 48 49 55 public class AttributeChangeDialog extends JDialog implements ActionListener 56 { 57 58 private static final Color BUTTON_COLOR = new Color (198, 226, 255); 59 private static final Color WARNING_COLOR = new Color (238, 169, 184); 60 private JScrollPane scrollPane; 61 private JTextArea area; 62 private MBeanAttributeInfo info; 63 private CjdbcGui gui; 64 private ObjectName objectName; 65 private JTextField newValue; 66 67 74 public AttributeChangeDialog(CjdbcGui gui, ObjectName name, 75 MBeanAttributeInfo info) 76 { 77 78 super(gui, "Attribute Change", true); 79 80 this.info = info; 81 this.gui = gui; 82 this.objectName = name; 83 GuiConstants.centerComponent(this, 400, 300); 84 85 this.setFont(GuiConstants.CENTER_PANE_FONT); 86 87 GridBagLayout gbl = new GridBagLayout (); 89 this.getContentPane().setLayout(gbl); 90 GridBagConstraints gbc = new GridBagConstraints (); 91 gbc.fill = GridBagConstraints.BOTH; 92 gbc.weightx = 1.0; 93 gbc.gridheight = 1; 94 gbc.gridwidth = GridBagConstraints.REMAINDER; 95 this.getContentPane().setBackground(Color.white); 96 97 JLabel object = new JLabel (name.toString()); 98 object.setBorder(BorderFactory.createTitledBorder(GuiConstants.LINE_BORDER, 99 "MBean Name")); 100 gbl.setConstraints(object, gbc); 101 this.getContentPane().add(object); 102 103 JLabel attribute = new JLabel (info.getName()); 104 attribute.setBorder(BorderFactory.createTitledBorder( 105 GuiConstants.LINE_BORDER, "Attribute Name (" + info.getType() + ")")); 106 gbl.setConstraints(attribute, gbc); 107 this.getContentPane().add(attribute); 108 109 JPanel values = new JPanel (); 110 values.setBorder(BorderFactory.createTitledBorder(GuiConstants.LINE_BORDER, 111 "Attribute Value")); 112 values.setBackground(Color.white); 113 values.setLayout(new GridLayout (2, 2)); 114 values.add(new JLabel ("Old Value")); 115 116 JTextField oldValue = new JTextField (); 117 Object attributeValue = null; 118 try 119 { 120 attributeValue = gui.getCurrentJmxClient().getAttributeValue(name, 121 info.getName()); 122 } 123 catch (Exception e) 124 { 125 oldValue.setForeground(WARNING_COLOR); 126 attributeValue = "<error>"; 127 } 128 finally 129 { 130 if (attributeValue == null) 131 attributeValue = ""; 132 } 133 oldValue.setText(attributeValue.toString()); 134 oldValue.setEditable(false); 135 values.add(oldValue); 136 values.add(new JLabel ("New Value")); 137 newValue = new JTextField (); 138 if (!info.isWritable()) 139 { 140 newValue.setText(" "); 141 newValue.setEnabled(false); 142 } 143 values.add(newValue); 144 gbl.setConstraints(values, gbc); 145 this.getContentPane().add(values); 146 147 JButton ok; 148 if (!info.isWritable()) 149 { 150 ok = new JButton ("Cannot change attribute value"); 151 ok.setBackground(WARNING_COLOR); 152 ok.setEnabled(false); 153 } 154 else 155 { 156 ok = new JButton ("Change attribute value"); 157 ok.setBackground(BUTTON_COLOR); 158 ok.setEnabled(true); 159 } 160 ok.setActionCommand(GuiCommands.COMMAND_CONFIRM_ACTION); 161 ok.addActionListener(this); 162 gbl.setConstraints(ok, gbc); 163 this.getContentPane().add(ok); 164 165 scrollPane = new JScrollPane (); 166 scrollPane.setBackground(Color.white); 167 scrollPane.setViewportBorder(BorderFactory.createTitledBorder( 168 GuiConstants.LINE_BORDER, "Attribute Change Result")); 169 area = new JTextArea (); 170 area.setBackground(Color.white); 171 scrollPane.getViewport().add(area); 172 gbc.gridheight = GridBagConstraints.REMAINDER; 173 gbc.weighty = 2.0; 174 gbl.setConstraints(scrollPane, gbc); 175 this.getContentPane().add(scrollPane); 176 177 this.validate(); 178 179 } 180 181 184 public void actionPerformed(ActionEvent e) 185 { 186 if (!info.isWritable()) 187 { 188 area.setForeground(WARNING_COLOR); 189 area.setText("This attribute cannot be changed"); 190 } 191 else 192 { 193 try 194 { 195 Object o = GuiConstants.convertType(newValue.getText(), info.getType()); 196 gui.getCurrentJmxClient().setAttributeValue(objectName, info.getName(), 197 o); 198 area.setForeground(Color.BLACK); 199 area.setText("Attribute value chaned"); 200 } 201 catch (Exception e1) 202 { 203 area.setForeground(WARNING_COLOR); 204 area.setText(e1.getMessage()); 205 } 206 } 207 } 208 } 209 | Popular Tags |