From 414e0ad17819770b1557dfd56814dd30a3e58e5c Mon Sep 17 00:00:00 2001 From: metasim Date: Fri, 17 Nov 2000 22:39:05 +0000 Subject: [PATCH] Started work on a Properties Bean property editor. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268205 13f79535-47bb-0310-9956-ffa450edef68 --- .../tools/ant/gui/acs/ACSTaskElement.java | 63 +++++- .../ant/gui/acs/ACSTaskElementBeanInfo.java | 4 + .../ant/gui/customizer/DynamicCustomizer.java | 14 ++ .../customizer/PropertiesPropertyEditor.java | 210 ++++++++++++++++++ .../ant/gui/resources/antidote.properties | 1 + 5 files changed, 291 insertions(+), 1 deletion(-) create mode 100644 src/antidote/org/apache/tools/ant/gui/customizer/PropertiesPropertyEditor.java diff --git a/src/antidote/org/apache/tools/ant/gui/acs/ACSTaskElement.java b/src/antidote/org/apache/tools/ant/gui/acs/ACSTaskElement.java index e58d917d3..e3911c3a2 100644 --- a/src/antidote/org/apache/tools/ant/gui/acs/ACSTaskElement.java +++ b/src/antidote/org/apache/tools/ant/gui/acs/ACSTaskElement.java @@ -53,7 +53,9 @@ */ package org.apache.tools.ant.gui.acs; -import com.sun.xml.tree.ElementNode; +import org.w3c.dom.Node; +import org.w3c.dom.NamedNodeMap; +import java.util.*; /** * Element containing a property definition. @@ -64,6 +66,9 @@ import com.sun.xml.tree.ElementNode; public class ACSTaskElement extends ACSTreeNodeElement { /** Property name for the task type. */ public static final String TASK_TYPE = "taskType"; + /** Property name for attributes. It's called "namedValues" so + * it doesn't collide with the Node.getAttributes() method. */ + public static final String NAMED_VALUES = "namedValues"; /** * Default ctor. @@ -81,4 +86,60 @@ public class ACSTaskElement extends ACSTreeNodeElement { return getTagName(); } + + /** + * Get the attributes (named value mappings). This method is not named + * getAttributes() because there is already a method of that name in + * the Node interface. + * + * @return Name-value mappings. + */ + public Properties getNamedValues() { + Properties retval = new Properties(); + + NamedNodeMap attribs = getAttributes(); + for(int i = 0, len = attribs.getLength(); i < len; i++) { + Node n = attribs.item(i); + retval.setProperty(n.getNodeName(), n.getNodeValue()); + } + return retval; + } + + + /** + * Set the attributes. This method sets the Node attirbutes using + * the given Map containing name-value pairs. + * + * @param attributes New attribute set. + */ + public void setNamedValues(Properties props) { + // XXX this code really sucks. It is really annoying that the + // DOM interfaces don't have a general "setAttributes()" or + // "removeAllAttributes()" method, but instead make you + // remove each attribute individually, or require you to figure + // out what the differences are between the two. + + // Although this is very inefficient, I'm taking the conceptually + // simplistic approach to this and brute force removing the existing + // set and replacing it with a brand new set. If this becomes a + // performance concern (which I doubt it will) it can be optimized + // later. + + Properties old = getNamedValues(); + + Enumeration enum = old.propertyNames(); + while(enum.hasMoreElements()) { + String name = (String) enum.nextElement(); + removeAttribute(name); + } + + enum = props.propertyNames(); + while(enum.hasMoreElements()) { + String key = (String) enum.nextElement(); + setAttribute(key, props.getProperty(key)); + } + + firePropertyChange(NAMED_VALUES, old, props); + } + } diff --git a/src/antidote/org/apache/tools/ant/gui/acs/ACSTaskElementBeanInfo.java b/src/antidote/org/apache/tools/ant/gui/acs/ACSTaskElementBeanInfo.java index 2edc606ca..7061e8b64 100644 --- a/src/antidote/org/apache/tools/ant/gui/acs/ACSTaskElementBeanInfo.java +++ b/src/antidote/org/apache/tools/ant/gui/acs/ACSTaskElementBeanInfo.java @@ -101,6 +101,8 @@ public class ACSTaskElementBeanInfo extends BaseBeanInfo { new PropertyDescriptor(ACSTaskElement.TASK_TYPE, ACSTaskElement.class, "getTaskType", null), + new PropertyDescriptor(ACSTaskElement.NAMED_VALUES, + ACSTaskElement.class), new PropertyDescriptor(ACSTaskElement.XML_STRING, ACSTaskElement.class, "getXMLString", null) @@ -109,6 +111,8 @@ public class ACSTaskElementBeanInfo extends BaseBeanInfo { retval[0].setDisplayName(getResources().getString( getClass(),ACSTaskElement.TASK_TYPE)); retval[1].setDisplayName(getResources().getString( + getClass(),ACSTaskElement.NAMED_VALUES)); + retval[2].setDisplayName(getResources().getString( getClass(),ACSTaskElement.XML_STRING)); setSortingOrder(retval); diff --git a/src/antidote/org/apache/tools/ant/gui/customizer/DynamicCustomizer.java b/src/antidote/org/apache/tools/ant/gui/customizer/DynamicCustomizer.java index ed78d2f97..8b4cad807 100644 --- a/src/antidote/org/apache/tools/ant/gui/customizer/DynamicCustomizer.java +++ b/src/antidote/org/apache/tools/ant/gui/customizer/DynamicCustomizer.java @@ -83,6 +83,8 @@ public class DynamicCustomizer extends JPanel implements Customizer { double.class, DoublePropertyEditor.class); PropertyEditorManager.registerEditor( Double.class, DoublePropertyEditor.class); + PropertyEditorManager.registerEditor( + Properties.class, PropertiesPropertyEditor.class); } /** Property name that PropertyDescriptors can save in their property @@ -105,6 +107,9 @@ public class DynamicCustomizer extends JPanel implements Customizer { /** List of property change listeners interested when the bean * being edited has been changed. */ private List _changeListeners = new LinkedList(); + /** Flag to trun off event propogation. */ + private boolean _squelchChangeEvents = false; + /** @@ -209,6 +214,9 @@ public class DynamicCustomizer extends JPanel implements Customizer { } _value = value; + // Disable event generation. + _squelchChangeEvents = true; + // Iterate over each property, doing a lookup on the associated editor // and setting the editor's value to the value of the property. Iterator it = _prop2Editor.keySet().iterator(); @@ -229,6 +237,10 @@ public class DynamicCustomizer extends JPanel implements Customizer { } } } + + // Enable event generation. + _squelchChangeEvents = false; + } /** @@ -306,6 +318,8 @@ public class DynamicCustomizer extends JPanel implements Customizer { /** Class for receiving change events from the PropertyEditor objects. */ private class EditorChangeListener implements PropertyChangeListener { public void propertyChange(PropertyChangeEvent e) { + if(_squelchChangeEvents) return; + PropertyEditor editor = (PropertyEditor) e.getSource(); PropertyDescriptor prop = (PropertyDescriptor) _editor2Prop.get(editor); diff --git a/src/antidote/org/apache/tools/ant/gui/customizer/PropertiesPropertyEditor.java b/src/antidote/org/apache/tools/ant/gui/customizer/PropertiesPropertyEditor.java new file mode 100644 index 000000000..25b4c6eb9 --- /dev/null +++ b/src/antidote/org/apache/tools/ant/gui/customizer/PropertiesPropertyEditor.java @@ -0,0 +1,210 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +package org.apache.tools.ant.gui.customizer; + +import javax.swing.*; +import javax.swing.table.AbstractTableModel; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.BorderLayout; +import java.util.*; + +/** + * Custom property editor for the Properties class. + * + * @version $Revision$ + * @author Simeon Fitch + */ +public class PropertiesPropertyEditor extends AbstractPropertyEditor { + private JPanel _widget = null; + private Properties _value = null; + private JTable _table = null; + + /** + * Default ctor. + * + */ + public PropertiesPropertyEditor() { + _widget = new JPanel(new BorderLayout()); + _widget.addFocusListener(new FocusHandler(this)); + + _table = new JTable(); + _table.setPreferredScrollableViewportSize(new Dimension(300, 100)); + JScrollPane scroller = new JScrollPane(_table); + _widget.add(BorderLayout.CENTER, scroller); + } + + /** + * Get the child editing component. Uses JComponent so we can have tool + * tips, etc. + * + * @return Child editing component. + */ + protected Component getChild() { + return _widget; + } + + /** + * This method is intended for use when generating Java code to set + * the value of the property. It should return a fragment of Java code + * that can be used to initialize a variable with the current property + * value. + *

+ * Example results are "2", "new Color(127,127,34)", "Color.orange", etc. + * + * @return A fragment of Java code representing an initializer for the + * current value. + */ + public String getJavaInitializationString() { + return getAsText(); + } + + /** + * Set (or change) the object that is to be edited. Builtin types such + * as "int" must be wrapped as the corresponding object type such as + * "java.lang.Integer". + * + * @param value The new target object to be edited. Note that this + * object should not be modified by the PropertyEditor, rather + * the PropertyEditor should create a new object to hold any + * modified value. + */ + public void setValue(Object value) { + if(value != null && !(value instanceof Properties)) { + throw new IllegalArgumentException( + value.getClass().getName() + " is not of type Properties."); + } + + Object old = _value; + _value = (Properties) value; + + _table.setModel(new PropertiesTableModel(_value)); + + firePropertyChange(old, value); + } + + /** + * @return The value of the property. Builtin types such as "int" will + * be wrapped as the corresponding object type such as "java.lang.Integer". + */ + public Object getValue() { + return _value; + } + + /** + * Set the property value by parsing a given String. May raise + * java.lang.IllegalArgumentException if either the String is + * badly formatted or if this kind of property can't be expressed + * as text. + * @param text The string to be parsed. + */ + public void setAsText(String text) throws IllegalArgumentException { + throw new IllegalArgumentException("Cannot be expressed as a String"); + } + + /** + * @return The property value as a human editable string. + *

Returns null if the value can't be expressed + * as an editable string. + *

If a non-null value is returned, then the PropertyEditor should + * be prepared to parse that string back in setAsText(). + */ + public String getAsText() { + return null; + } + + /** Table model view of the Properties object. */ + private static class PropertiesTableModel extends AbstractTableModel { + private static final int NAME = 0; + private static final int VALUE = 1; + + private Properties _properties = null; + private String[] _keys = null; + + public PropertiesTableModel(Properties props) { + _properties = props; + + Enumeration enum = _properties.keys(); + _keys = new String[_properties.size()]; + for(int i = 0; enum.hasMoreElements(); i++) { + String key = (String) enum.nextElement(); + _keys[i] = key; + } + } + + public int getRowCount() { + return _keys.length; + } + + public int getColumnCount() { + return 2; + } + + public String getColumnName(int column) { + // XXX fix me. + return column == NAME ? "Name" : "Value"; + } + public Object getValueAt(int row, int column) { + switch(column) { + case NAME: return _keys[row]; + case VALUE: return _properties.getProperty(_keys[row]); + } + return null; + } + } + + +} + + diff --git a/src/antidote/org/apache/tools/ant/gui/resources/antidote.properties b/src/antidote/org/apache/tools/ant/gui/resources/antidote.properties index 7240af0d1..d199188dd 100644 --- a/src/antidote/org/apache/tools/ant/gui/resources/antidote.properties +++ b/src/antidote/org/apache/tools/ant/gui/resources/antidote.properties @@ -100,6 +100,7 @@ org.apache.tools.ant.gui.acs.ACSTaskElementBeanInfo.beanName=Task org.apache.tools.ant.gui.acs.ACSTaskElementBeanInfo.beanDescription=\ A scoped property org.apache.tools.ant.gui.acs.ACSTaskElementBeanInfo.taskType=Type +org.apache.tools.ant.gui.acs.ACSTaskElementBeanInfo.namedValues=Attributes org.apache.tools.ant.gui.acs.ACSTaskElementBeanInfo.xmlString=XML Code org.apache.tools.ant.gui.acs.ACSTaskElementBeanInfo.icon=task.gif