1 package org.sape.carbon.services.console; 2 3 import java.beans.PropertyChangeEvent ; 4 import java.math.BigDecimal ; 5 import java.math.BigInteger ; 6 import java.util.HashMap ; 7 import java.util.HashSet ; 8 import java.util.Map ; 9 import java.util.Set ; 10 11 import javax.management.Attribute ; 12 import javax.management.MBeanAttributeInfo ; 13 import javax.management.MBeanServer ; 14 import javax.management.ObjectName ; 15 import javax.swing.JButton ; 16 import javax.swing.JLabel ; 17 import javax.swing.JPanel ; 18 import javax.swing.JTextArea ; 19 import javax.swing.JTextField ; 20 import javax.swing.border.CompoundBorder ; 21 import javax.swing.border.TitledBorder ; 22 import javax.swing.event.CellEditorListener ; 23 import javax.swing.event.ChangeEvent ; 24 25 import org.sape.carbon.core.config.type.ConfigurationTypeException; 26 import org.sape.carbon.core.config.type.ConfigurationTypeService; 27 import org.sape.carbon.core.config.type.ConfigurationTypeServiceFactory; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 42 public class AttributePanel extends JPanel { 43 44 public Log log = LogFactory.getLog(this.getClass()); 45 46 private ObjectName objectName; 47 private MBeanAttributeInfo attributeInfo; 48 private MBeanServer server; 49 50 private boolean dirty = false; 51 private Throwable currentError; 52 53 private AttributeCellEditor editor; 54 55 public AttributePanel(ObjectName objectName, MBeanAttributeInfo attributeInfo, MBeanServer server) { 56 this.objectName = objectName; 57 this.attributeInfo = attributeInfo; 58 this.server = server; 59 60 initComponents(); 61 62 ((TitledBorder )((CompoundBorder )this.getBorder()).getInsideBorder()).setTitle(this.attributeInfo.getName()); 63 64 refresh(); 65 } 66 67 public void refresh() { 68 Object value = null; 69 try { 70 this.descriptionValue.setText(this.attributeInfo.getDescription()); 71 value = this.server.getAttribute(objectName,this.attributeInfo.getName()); 72 73 ConfigurationTypeService typeService = 74 ConfigurationTypeServiceFactory.getInstance(); 75 76 Class typeClass = AttributePanel.findType(this.attributeInfo.getType()); 77 78 this.typeValue.setText(getTypeName(typeClass)); 79 80 AttributeCellEditor typeEditor = 81 new AttributeCellEditor(typeClass, value, attributeInfo.isWritable()); 83 if (typeEditor != null) { 84 this.editor = typeEditor; 85 typeEditor.addCellEditorListener(new CellEditorListener () { 86 public void editingStopped(ChangeEvent e) { 87 System.out.println("Stopped editing " + attributeInfo.getName()); 88 dirty = true; 89 updateInfoStatus(); 90 } 91 public void editingCanceled(ChangeEvent e) { } 92 }); 93 94 this.editingPanel.add(typeEditor.getComponent()); 95 } 96 97 if (AttributePanel.GRAPH_TYPES.contains(typeClass)) { 98 this.graphButton.setEnabled(true); 99 this.shouldGraphCheckbox.setEnabled(true); 100 } else { 101 this.graphingPanel.setVisible(false); 102 this.graphButton.setEnabled(false); 103 this.shouldGraphCheckbox.setEnabled(false); 104 } 105 106 updateInfoStatus(); 107 108 } catch (UnsupportedOperationException uoe) { 109 this.editingPanel.add(new JTextField ((value==null)?"null":value.toString())); 110 } catch (Exception e) { 111 SingletonExceptionViewer.getInstance().addException(e); 112 } 113 } 114 115 116 public String getTypeName(Class typeClass) { 117 118 if (typeClass.isArray()) { 119 return "Array of (" + getTypeName(typeClass.getComponentType()) + ")"; 120 } else { 121 String name = typeClass.getName(); 122 return name.substring(name.lastIndexOf('.')+1); 123 } 124 } 125 126 private static final Set GRAPH_TYPES = new HashSet (); 127 static { 128 GRAPH_TYPES.add(Short.TYPE); 129 GRAPH_TYPES.add(Short .class); 130 GRAPH_TYPES.add(Integer.TYPE); 131 GRAPH_TYPES.add(Integer .class); 132 GRAPH_TYPES.add(Long.TYPE); 133 GRAPH_TYPES.add(Long .class); 134 GRAPH_TYPES.add(Float.TYPE); 135 GRAPH_TYPES.add(Float .class); 136 GRAPH_TYPES.add(Double.TYPE); 137 GRAPH_TYPES.add(Double .class); 138 GRAPH_TYPES.add(BigInteger .class); 139 GRAPH_TYPES.add(BigDecimal .class); 140 } 141 142 private static final Map TYPES = new HashMap (); 143 static { 144 TYPES.put(Boolean.TYPE.getName(), Boolean.TYPE); 145 TYPES.put(Character.TYPE.getName(), Character.TYPE); 146 TYPES.put(Byte.TYPE.getName(), Byte.TYPE); 147 TYPES.put(Short.TYPE.getName(), Short.TYPE); 148 TYPES.put(Integer.TYPE.getName(), Integer.TYPE); 149 TYPES.put(Long.TYPE.getName(), Long.TYPE); 150 TYPES.put(Float.TYPE.getName(), Float.TYPE); 151 TYPES.put(Double.TYPE.getName(), Double.TYPE); 152 TYPES.put(Void.TYPE.getClass(), Void.TYPE); 153 } 154 155 private static Class findType(String className) throws ClassNotFoundException { 156 157 if (TYPES.containsKey(className)) { 158 return (Class ) TYPES.get(className); 159 } else { 160 return Class.forName(className, true, AttributePanel.class.getClassLoader()); 161 } 162 } 163 164 public MBeanAttributeInfo getAttributeInfo() { 165 return this.attributeInfo; 166 } 167 168 public String getFieldValue() { 169 return this.editor.getCellEditorValue().toString(); 170 } 171 172 public boolean isShouldGraph() { 173 return this.shouldGraphCheckbox.isSelected(); 174 } 175 176 private void updateInfoStatus() { 177 this.infoPanel.removeAll(); 178 if (!this.attributeInfo.isWritable()) { 179 JLabel lock = new JLabel (); 180 try { 181 lock.setIcon(new javax.swing.ImageIcon (getClass().getResource("/rsrc/icons/Lock24.gif"))); 182 } catch (Exception e) { e.printStackTrace(); } 183 this.infoPanel.add(lock); 184 } 185 186 if (this.dirty) { 187 this.infoPanel.add( 188 new JLabel ("Dirty")); 189 190 JButton setAttributeButton = new JButton ("Set"); 191 setAttributeButton.addActionListener(new java.awt.event.ActionListener () { 192 public void actionPerformed(java.awt.event.ActionEvent evt) { 193 setValueButtonActionPerformed(evt); 194 } 195 }); 196 this.infoPanel.add(setAttributeButton); 197 198 } 199 if (this.currentError!=null) { 200 this.infoPanel.add( 201 new JTextArea (this.currentError.toString())); 202 } 203 doLayout(); 204 this.repaint(); 205 206 } 207 208 213 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 215 216 typeLabel = new javax.swing.JLabel (); 217 typeValue = new javax.swing.JLabel (); 218 valueLabel = new javax.swing.JLabel (); 219 descriptionValue = new javax.swing.JTextArea (); 220 graphingPanel = new javax.swing.JPanel (); 221 graphButton = new javax.swing.JButton (); 222 shouldGraphCheckbox = new javax.swing.JCheckBox (); 223 infoPanel = new javax.swing.JPanel (); 224 editingPanel = new javax.swing.JPanel (); 225 226 setLayout(new java.awt.GridBagLayout ()); 227 228 setBorder(new javax.swing.border.CompoundBorder (new javax.swing.border.EmptyBorder (new java.awt.Insets (4, 4, 4, 4)), new javax.swing.border.TitledBorder ("Attribute Name"))); 229 typeLabel.setText("Type"); 230 gridBagConstraints = new java.awt.GridBagConstraints (); 231 gridBagConstraints.gridx = 0; 232 gridBagConstraints.gridy = 2; 233 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 234 gridBagConstraints.insets = new java.awt.Insets (0, 8, 0, 3); 235 add(typeLabel, gridBagConstraints); 236 237 typeValue.setFont(new java.awt.Font ("Courier New", 0, 12)); 238 typeValue.setText("jLabel4"); 239 typeValue.setMaximumSize(new java.awt.Dimension (400, 16)); 240 typeValue.setMinimumSize(new java.awt.Dimension (49, 16)); 241 typeValue.setPreferredSize(new java.awt.Dimension (220, 16)); 242 gridBagConstraints = new java.awt.GridBagConstraints (); 243 gridBagConstraints.gridx = 1; 244 gridBagConstraints.gridy = 2; 245 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 246 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 247 gridBagConstraints.weightx = 1.0; 248 gridBagConstraints.insets = new java.awt.Insets (1, 2, 1, 2); 249 add(typeValue, gridBagConstraints); 250 251 valueLabel.setText("Value"); 252 gridBagConstraints = new java.awt.GridBagConstraints (); 253 gridBagConstraints.gridx = 0; 254 gridBagConstraints.gridy = 3; 255 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 256 gridBagConstraints.insets = new java.awt.Insets (4, 8, 4, 3); 257 add(valueLabel, gridBagConstraints); 258 259 descriptionValue.setBackground(new java.awt.Color (204, 204, 204)); 260 descriptionValue.setEditable(false); 261 descriptionValue.setLineWrap(true); 262 descriptionValue.setText("The attribute description goes here"); 263 descriptionValue.setToolTipText("The attribute description goes here"); 264 descriptionValue.setWrapStyleWord(true); 265 gridBagConstraints = new java.awt.GridBagConstraints (); 266 gridBagConstraints.gridx = 0; 267 gridBagConstraints.gridy = 1; 268 gridBagConstraints.gridwidth = 2; 269 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 270 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 271 gridBagConstraints.insets = new java.awt.Insets (0, 11, 0, 0); 272 add(descriptionValue, gridBagConstraints); 273 274 graphingPanel.setBorder(new javax.swing.border.TitledBorder ("Graphing")); 275 graphButton.setFont(new java.awt.Font ("Dialog", 0, 8)); 276 graphButton.setIcon(new javax.swing.ImageIcon (getClass().getResource("/rsrc/icons/Graph24.gif"))); 277 graphButton.setMargin(new java.awt.Insets (2, 2, 2, 2)); 278 graphButton.setMaximumSize(new java.awt.Dimension (43, 28)); 279 graphButton.setMinimumSize(new java.awt.Dimension (43, 20)); 280 graphButton.setPreferredSize(new java.awt.Dimension (26, 26)); 281 graphButton.addActionListener(new java.awt.event.ActionListener () { 282 public void actionPerformed(java.awt.event.ActionEvent evt) { 283 graphButtonActionPerformed(evt); 284 } 285 }); 286 287 graphingPanel.add(graphButton); 288 289 shouldGraphCheckbox.setFont(new java.awt.Font ("Dialog", 0, 12)); 290 shouldGraphCheckbox.setText("Multi"); 291 shouldGraphCheckbox.addActionListener(new java.awt.event.ActionListener () { 292 public void actionPerformed(java.awt.event.ActionEvent evt) { 293 shouldGraphCheckboxActionPerformed(evt); 294 } 295 }); 296 297 graphingPanel.add(shouldGraphCheckbox); 298 299 gridBagConstraints = new java.awt.GridBagConstraints (); 300 gridBagConstraints.gridx = 2; 301 gridBagConstraints.gridy = 0; 302 gridBagConstraints.gridheight = 4; 303 gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL; 304 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 305 add(graphingPanel, gridBagConstraints); 306 307 infoPanel.setLayout(new java.awt.FlowLayout (java.awt.FlowLayout.LEFT)); 308 309 infoPanel.setBackground(new java.awt.Color (204, 204, 204)); 310 gridBagConstraints = new java.awt.GridBagConstraints (); 311 gridBagConstraints.gridx = 1; 312 gridBagConstraints.gridy = 0; 313 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 314 add(infoPanel, gridBagConstraints); 315 316 editingPanel.setLayout(new java.awt.BorderLayout ()); 317 318 editingPanel.setMinimumSize(null); 319 editingPanel.setPreferredSize(null); 320 gridBagConstraints = new java.awt.GridBagConstraints (); 321 gridBagConstraints.gridx = 1; 322 gridBagConstraints.gridy = 3; 323 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 324 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 325 gridBagConstraints.insets = new java.awt.Insets (1, 3, 3, 0); 326 add(editingPanel, gridBagConstraints); 327 328 } 330 private void setValueButtonActionPerformed(java.awt.event.ActionEvent evt) { String value = getFieldValue(); 333 334 try { 335 this.currentError = null; 336 Class type = 337 AttributePanel.findType(this.attributeInfo.getType()); 338 340 ConfigurationTypeService typeService = 341 ConfigurationTypeServiceFactory.getInstance(); 342 343 Object objValue = typeService.toObject(type,value); 344 345 this.server.setAttribute(this.objectName, 346 new Attribute (this.attributeInfo.getName(), objValue)); 347 348 this.dirty = false; 349 this.updateInfoStatus(); 350 log.trace( 351 "MBean Attribute [" + 352 this.attributeInfo.getName() + 353 "] set to [" + 354 objValue + "]"); 355 } catch (ClassNotFoundException cnfe) { 356 cnfe.printStackTrace(); 357 } catch (ConfigurationTypeException cte) { 358 cte.printStackTrace(); 359 this.currentError = cte; 360 } catch (Exception e) { 361 e.printStackTrace(); 362 this.currentError = e; 363 } 364 doLayout(); 365 } 367 public void valueTextFieldPropertyChange(PropertyChangeEvent evt) { 368 this.dirty = true; 369 updateInfoStatus(); 370 doLayout(); 371 repaint(); 372 } 373 374 private void shouldGraphCheckboxActionPerformed(java.awt.event.ActionEvent evt) { } 378 private void graphButtonActionPerformed(java.awt.event.ActionEvent evt) { AttributeGraph attrGraph = 381 new AttributeGraph(this.objectName, this.attributeInfo, this.server); 382 attrGraph.show(); 383 384 } 386 387 private javax.swing.JCheckBox shouldGraphCheckbox; 389 private javax.swing.JPanel infoPanel; 390 private javax.swing.JButton graphButton; 391 private javax.swing.JLabel typeLabel; 392 private javax.swing.JLabel typeValue; 393 private javax.swing.JTextArea descriptionValue; 394 private javax.swing.JPanel graphingPanel; 395 private javax.swing.JPanel editingPanel; 396 private javax.swing.JLabel valueLabel; 397 399 } 400 | Popular Tags |