[Mc4j-cvs] mc4j/src/org/mc4j/console/bean/attribute AttributePropertyFactory.java,NONE,1.1 Attribute
Brought to you by:
ghinkl
From: Greg H. <gh...@us...> - 2004-04-02 03:39:59
|
Update of /cvsroot/mc4j/mc4j/src/org/mc4j/console/bean/attribute In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16558/src/org/mc4j/console/bean/attribute Modified Files: AttributeNode.java AttributeSetNode.java Added Files: AttributePropertyFactory.java Log Message: Moving to a single attribute property sheet supported by the attribute node. Index: AttributeNode.java =================================================================== RCS file: /cvsroot/mc4j/mc4j/src/org/mc4j/console/bean/attribute/AttributeNode.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** AttributeNode.java 7 Feb 2004 16:10:40 -0000 1.8 --- AttributeNode.java 2 Apr 2004 03:27:55 -0000 1.9 *************** *** 20,24 **** package org.mc4j.console.bean.attribute; ! import java.beans.PropertyEditorManager; import java.lang.reflect.InvocationTargetException; --- 20,24 ---- package org.mc4j.console.bean.attribute; ! import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; *************** *** 40,45 **** import org.mc4j.console.Refreshable; - import org.mc4j.console.bean.MBeanNode; - import org.mc4j.console.swing.editor.ObjectNameEditor; /** --- 40,43 ---- *************** *** 55,59 **** protected ObjectName objectName; protected MBeanAttributeInfo attributeInfo; ! protected Object currentValue; --- 53,59 ---- protected ObjectName objectName; protected MBeanAttributeInfo attributeInfo; ! ! protected Node.Property valuePropertyNode; ! protected Object currentValue; *************** *** 89,100 **** refresh(); ! setDisplayName(this.attributeInfo.getName() + " = " + this.currentValue); setShortDescription(this.attributeInfo.getDescription()); } ! public void setValue(Object newValue) { this.currentValue = newValue; ! setDisplayName(this.attributeInfo.getName() + " = " + this.currentValue); } --- 89,115 ---- refresh(); ! setDisplayName(this.attributeInfo.getName()); setShortDescription(this.attributeInfo.getDescription()); } ! public void updateValue(Object newValue) { ! Object oldValue = this.currentValue; this.currentValue = newValue; ! //setDisplayName(this.attributeInfo.getName() + " = " + this.currentValue); ! // TODO GH: How to refresh the property view? ! firePropertyChange("value", oldValue, newValue); ! } ! ! ! public void setValue(Object newValue) throws Exception { ! ! try { ! server.setAttribute(objectName, ! new Attribute(attributeInfo.getName(), newValue)); ! } catch (Exception e) { ! throw new InvocationTargetException(e); ! } ! refresh(); } *************** *** 108,117 **** newValue = server.getAttribute(this.objectName, this.attributeInfo.getName()); } catch (Exception e) { newValue = "unknown type"; } ! if ((this.currentValue == null) || (!this.currentValue.equals(newValue))) { ! setDisplayName(this.attributeInfo.getName() + " = " + newValue); ! } } --- 123,138 ---- newValue = server.getAttribute(this.objectName, this.attributeInfo.getName()); } catch (Exception e) { + //e.printStackTrace(); newValue = "unknown type"; } ! //if ((this.currentValue == null) || (!this.currentValue.equals(newValue))) { ! // setDisplayName(this.attributeInfo.getName() + " = " + newValue); ! //} ! Object oldValue = this.currentValue; ! this.currentValue = newValue; ! ! // TODO GH: How to refresh the property view? ! firePropertyChange(getValueProperty().getName(), oldValue, newValue); } *************** *** 203,216 **** }); ! Node.Property attributeValueProperty = buildAttributeValueProperty(); if (attributeValueProperty != null) props.put(attributeValueProperty); return sheet; } ! private Node.Property buildAttributeValueProperty() { Node.Property property = null; ! PropertyEditorManager.registerEditor( ObjectName.class, --- 224,303 ---- }); ! Node.Property attributeValueProperty = getValueProperty(); if (attributeValueProperty != null) props.put(attributeValueProperty); + props.put(buildAttributeValueAsStringProperty()); return sheet; } ! ! private Node.Property buildAttributeValueAsStringProperty() { ! Node.Property property = null; ! property = new AttributeNodeStringProperty(this); ! ! /* ! new PropertySupport.ReadOnly( ! "valueAsText", ! String.class, ! "String Value", ! attributeInfo.getDescription()) { ! ! public Object getValue() ! throws IllegalAccessException, ! InvocationTargetException { ! try { ! Object val = getValue(); ! if (val == null) { ! return "null"; ! } if (val.getClass().isArray()) { ! int length = Array.getLength(val); ! ! StringBuffer value = new StringBuffer(); ! value.append("{ "); ! ! for (int i = 0; i < length; i++) { ! Object o = Array.get(val,i); ! if (i >0) ! value.append(", "); ! value.append(o); ! } ! value.append(" }"); ! return value.toString(); ! } else { ! return val.toString(); ! } ! } catch (Exception e) { ! return null; ! //throw new InvocationTargetException(e); ! } ! } ! }; ! */ ! ! return property; ! } ! ! /** ! * ! * @since MC4J 1.2b5 ! * @return ! */ ! public synchronized Node.Property getValueProperty() { ! if (this.valuePropertyNode == null) { ! this.valuePropertyNode = buildAttributeValueProperty(); ! } ! return this.valuePropertyNode; ! } ! private Node.Property buildAttributeValueProperty() { + return AttributePropertyFactory.getInstance().buildProperty(this); + } + + + /* + private Node.Property buildAttributeValuePropertyOld() { Node.Property property = null; ! ! PropertyEditorManager.registerEditor( ObjectName.class, *************** *** 220,227 **** MBeanNode.findType(attributeInfo.getType()); ! if (attributeInfo.isReadable() && attributeInfo.isWritable()) { property = new PropertySupport.ReadWrite( ! attributeInfo.getName(), MBeanNode.findType(attributeInfo.getType()), "Value", --- 307,314 ---- MBeanNode.findType(attributeInfo.getType()); ! if (true) { //attributeInfo.isReadable() && attributeInfo.isWritable()) { property = new PropertySupport.ReadWrite( ! "value", MBeanNode.findType(attributeInfo.getType()), "Value", *************** *** 239,246 **** --- 326,342 ---- } + public Object getValue(String attributeName) { + if ("inplaceEditor".equals(attributeName)) { + return new DateEditor(); + } else { + return super.getValue(attributeName); + } + } public Object getValue() throws IllegalAccessException, InvocationTargetException { try { + //if (getValueType().equals(Date.class)) + // return new DateEditor(); return server.getAttribute(objectName, attributeInfo.getName()); } catch (Exception e) { *************** *** 249,260 **** } } }; } else { ! property = ! new PropertySupport.ReadOnly( ! attributeInfo.getName(), MBeanNode.findType(attributeInfo.getType()), "Value", attributeInfo.getDescription()) { public Object getValue() --- 345,375 ---- } } + + public PropertyEditor getPropertyEditor() { + + return new PropertyEditorSupport() { + public String getAsText() { + return "hello"; + } + + }; + + } + }; } else { ! property = ! new PropertySupport.ReadOnly ( ! "value", MBeanNode.findType(attributeInfo.getType()), "Value", attributeInfo.getDescription()) { + public Object getValue(String attributeName) { + if ("inplaceEditor".equals(attributeName)) { + return new DateEditor(); + } else { + return super.getValue(attributeName); + } + } public Object getValue() *************** *** 262,265 **** --- 377,381 ---- InvocationTargetException { try { + return server.getAttribute(objectName, attributeInfo.getName()); } catch (Exception e) { *************** *** 268,271 **** --- 384,410 ---- } } + public PropertyEditor getPropertyEditor() { + return new ExPropertyEditorSupport() { + protected void attachEnvImpl(PropertyEnv env) { + env.registerInplaceEditorFactory(new InplaceEditor.Factory() { + public InplaceEditor getInplaceEditor() { + return new DateEditor(); + } + }); + } + + protected void validateEnv(PropertyEnv env) { + env.registerInplaceEditorFactory(new InplaceEditor.Factory() { + public InplaceEditor getInplaceEditor() { + return new DateEditor(); + } + }); + } + + public String getAsText() { + return "Bah"; + } + }; + } }; } *************** *** 275,278 **** --- 414,476 ---- return property; } + */ + + + public static class AttributeNodeStringProperty extends Node.Property { + + private AttributeNode attributeNode; + + public AttributeNodeStringProperty(AttributeNode attributeNode) { + super(String.class); + + this.attributeNode = attributeNode; + setName("valueAsText"); + setDisplayName("Value as String"); + } + + public boolean canRead() { + return true; + } + + public Object getValue() throws IllegalAccessException, InvocationTargetException { + + try { + Object val = this.attributeNode.getValue(); + if (val == null) { + return "null"; + } if (val.getClass().isArray()) { + int length = Array.getLength(val); + + StringBuffer value = new StringBuffer(); + value.append("{ "); + + for (int i = 0; i < length; i++) { + Object o = Array.get(val,i); + if (i >0) + value.append(", "); + value.append(o); + } + value.append(" }"); + return value.toString(); + } else { + return val.toString(); + } + } catch (Exception e) { + return null; + //throw new InvocationTargetException(e); + } + } + + public boolean canWrite() { + return false; + } + + public void setValue(Object val) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { + + } + } + + + } --- NEW FILE: AttributePropertyFactory.java --- /* * Author: Greg Hinkle * * The contents of this file are subject to the Sapient Public License Version 1.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy of the * License at http://mc4j.sf.net/License-SPL.html. * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, * either express or implied. See the License for the specific language governing rights and limitations * under the License. * * The Original Code is The MC4J Management Console * The Initial Developer of the Original Code is Greg Hinkle (gh...@us...) * Copyright (C) 2004 Greg Hinkle. All Rights Reserved. * * Redistributions of code or binary files using or based on this code must reproduce the * above copyright and disclaimer. For more information see <http://mc4j.sourceforge.net>. */ package org.mc4j.console.bean.attribute; import java.lang.reflect.InvocationTargetException; import javax.management.MBeanAttributeInfo; import org.openide.nodes.Node; import org.mc4j.console.bean.MBeanNode; /** * @author Greg Hinkle (gh...@us...), Mar 23, 2004 * @version $Revision: 1.1 $($Author: ghinkl $ / $Date: 2004/04/02 03:27:55 $) */ public class AttributePropertyFactory { private static AttributePropertyFactory factory; public static AttributePropertyFactory getInstance() { if (factory == null) { factory = new AttributePropertyFactory(); } return factory; } public Node.Property buildProperty(AttributeNode node) { MBeanAttributeInfo attributeInfo = node.getAttributeInfo(); Node.Property property = null; try { Class type = MBeanNode.findType(attributeInfo.getType()); property = new AttributeProperty(type, node); } catch (ClassNotFoundException cnfe) { } return property; } public static class AttributeProperty extends Node.Property { protected AttributeNode attributeNode; public AttributeProperty(Class type, AttributeNode attributeNode) { super(type); this.attributeNode = attributeNode; setName(this.attributeNode.getDisplayName()); } /** * Can read the value of the property. * Returns the value passed into constructor. * @return <CODE>true</CODE> if the read of the value is supported */ public boolean canRead() { return this.attributeNode.getAttributeInfo().isReadable(); } /** * Can write the value of the property. * Returns the value passed into constructor. * @return <CODE>true</CODE> if the read of the value is supported */ public boolean canWrite() { return this.attributeNode.getAttributeInfo().isWritable(); } public Object getValue() throws IllegalAccessException, InvocationTargetException { return this.attributeNode.getValue(); } public void setValue(Object val) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { try { this.attributeNode.setValue(val); } catch(Exception e) { throw new InvocationTargetException(e); } } } } Index: AttributeSetNode.java =================================================================== RCS file: /cvsroot/mc4j/mc4j/src/org/mc4j/console/bean/attribute/AttributeSetNode.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** AttributeSetNode.java 24 Feb 2004 04:01:17 -0000 1.8 --- AttributeSetNode.java 2 Apr 2004 03:27:55 -0000 1.9 *************** *** 73,79 **** public void refresh() { if (ExplorerUtil.isExpanded(this)) { - retrieveData(); - } } --- 73,77 ---- *************** *** 106,123 **** this.mbeanNode.getObjectName(), names); ! ! Map values = new HashMap(); ! Iterator iter = attributeList.iterator(); ! while (iter.hasNext()) { ! Attribute attr = (Attribute)iter.next(); ! values.put(attr.getName(),attr.getValue()); ! } ! for (int i = 0; i < children.length; i++) { ! AttributeNode node = (AttributeNode)children[i]; ! Object value = values.get(node.getName()); ! if (value != null) { ! node.setValue(value); ! } else { ! node.setValue("unknown type"); } } --- 104,128 ---- this.mbeanNode.getObjectName(), names); ! if (attributeList.size() == names.length) { ! Map values = new HashMap(); ! Iterator iter = attributeList.iterator(); ! while (iter.hasNext()) { ! Attribute attr = (Attribute)iter.next(); ! values.put(attr.getName(),attr.getValue()); ! } ! for (int i = 0; i < children.length; i++) { ! AttributeNode node = (AttributeNode)children[i]; ! Object value = values.get(node.getName()); ! if (value != null) { ! node.updateValue(value); ! } else { ! node.updateValue("unknown type"); ! } ! } ! } else { ! // If we still we're unable to load all the attributes at once ! // lets load as many as we can, one at a time. ! for (int i = 0; i < children.length; i++ ) { ! ((Refreshable)children[i]).refresh(); } } |