You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by me...@locus.apache.org on 2000/11/10 21:41:19 UTC
cvs commit: jakarta-ant/src/antidote/org/apache/tools/ant/gui/customizer AbstractPropertyEditor.java DoublePropertyEditor.java DynamicCustomizer.java IntegerPropertyEditor.java StringPropertyEditor.java
metasim 00/11/10 12:41:18
Added: src/antidote/org/apache/tools/ant/gui/customizer
AbstractPropertyEditor.java
DoublePropertyEditor.java DynamicCustomizer.java
IntegerPropertyEditor.java
StringPropertyEditor.java
Log:
Started addition of a generic Bean editor/property sheet and supporting
infrastructure for Ant Elements.
Revision Changes Path
1.1 jakarta-ant/src/antidote/org/apache/tools/ant/gui/customizer/AbstractPropertyEditor.java
Index: AbstractPropertyEditor.java
===================================================================
/*
* 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
* <http://www.apache.org/>.
*/
package org.apache.tools.ant.gui.customizer;
import java.beans.*;
import java.awt.Graphics;
import java.awt.Component;
import java.awt.Rectangle;
import javax.swing.JComponent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusAdapter;
/**
* Abstract base class for the custom type property editors.
*
* @version $Revision: 1.1 $
* @author Simeon Fitch
*/
public abstract class AbstractPropertyEditor implements PropertyEditor {
/** Bean property change property name. */
public static final String BEAN_PROP = "BeanEditorProperty";
private PropertyChangeSupport _listeners = new PropertyChangeSupport(this);
/**
* Default constructor.
*
*/
protected AbstractPropertyEditor() {
}
/**
* Paint a representation of the value into a given area of screen
* real estate. Note that the propertyEditor is responsible for doing
* its own clipping so that it fits into the given rectangle.
* <p>
* If the PropertyEditor doesn't honor paint requests (see isPaintable)
* this method should be a silent noop.
* <p>
* The given Graphics object will have the default font, color, etc of
* the parent container. The PropertyEditor may change graphics attributes
* such as font and color and doesn't need to restore the old values.
*
* @param gfx Graphics object to paint into.
* @param box Rectangle within graphics object into which we should paint.
*/
public void paintValue(Graphics gfx, Rectangle box) {
Object o = getValue();
String s = o == null ? "<null>" : o.toString();
gfx.drawString(s, box.x, box.y);
}
/**
* Fire a property change event to listeners.
*
* @param oldValue Old value.
* @param newValue New value.
*/
public void firePropertyChange(Object oldValue, Object newValue) {
_listeners.firePropertyChange(BEAN_PROP, oldValue, newValue);
}
/**
* Add a property change listener. XXX This may cause undesired
* side affects with merging property changes with JPanel class.
* Need to test for a while.
*
* @param l Change listener.
*/
public void addPropertyChangeListener(PropertyChangeListener l) {
_listeners.addPropertyChangeListener(l);
}
/**
* Remove a property change listener.
*
* @param l Change listener.
*/
public void removePropertyChangeListener(PropertyChangeListener l) {
_listeners.removePropertyChangeListener(l);
}
/**
* @return True if the class will honor the paintValue method.
*/
public boolean isPaintable() {
return true;
}
/**
* If the property value must be one of a set of known tagged values,
* then this method should return an array of the tags. This can
* be used to represent (for example) enum values. If a PropertyEditor
* supports tags, then it should support the use of setAsText with
* a tag value as a way of setting the value and the use of getAsText
* to identify the current value.
*
* @return The tag values for this property. May be null if this
* property cannot be represented as a tagged value.
*
*/
public String[] getTags() {
return null;
}
/**
* A PropertyEditor may choose to make available a full custom Component
* that edits its property value. It is the responsibility of the
* PropertyEditor to hook itself up to its editor Component itself and
* to report property value changes by firing a PropertyChange event.
* <P>
* The higher-level code that calls getCustomEditor may either embed
* the Component in some larger property sheet, or it may put it in
* its own individual dialog, or ...
*
* @return A java.awt.Component that will allow a human to directly
* edit the current property value. May be null if this is
* not supported.
*/
public Component getCustomEditor() {
return getChild();
}
/**
* @return True if the propertyEditor can provide a custom editor.
*/
public boolean supportsCustomEditor() {
return true;
}
/**
* 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.
* <p>
* 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 "";
}
/**
* Get the child editing component.
*
* @return Child editing component.
*/
protected abstract Component getChild();
/** Helper class for detecting changes and generating change events
* on a focus lost event. */
protected static class FocusHandler extends FocusAdapter {
/** Last value of the editor. */
private Object _value = null;
/** Editor of interest. */
private AbstractPropertyEditor _editor = null;
/**
* Standard constructor.
*
* @param editor Editor of interest.
*/
public FocusHandler(AbstractPropertyEditor editor) {
_editor = editor;
}
/**
* Called when focus is gained.
*
* @param e Focus event.
*/
public void focusGained(FocusEvent e) {
_value = _editor.getValue();
}
/**
* Called when focus is lost. Checks to see if value changed and
* fires a change event if needed.
*
* @param e Focus event.
*/
public void focusLost(FocusEvent e) {
if((_value != null && !_value.equals(_editor.getValue())) ||
(_value == null && _editor.getValue() != null)) {
_editor.firePropertyChange(_value, _editor.getValue());
}
}
}
}
1.1 jakarta-ant/src/antidote/org/apache/tools/ant/gui/customizer/DoublePropertyEditor.java
Index: DoublePropertyEditor.java
===================================================================
/*
* 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
* <http://www.apache.org/>.
*/
package org.apache.tools.ant.gui.customizer;
import javax.swing.*;
import java.awt.Component;
/**
* Custom property editor for editing double values.
*
* @version $Revision: 1.1 $
* @author Simeon Fitch
*/
public class DoublePropertyEditor extends AbstractPropertyEditor {
/** Editing widget. */
private JTextField _widget = null;
/**
* Default ctor.
*
*/
public DoublePropertyEditor() {
_widget = new JTextField();
_widget.addFocusListener(new FocusHandler(this));
}
/**
* 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.
* <p>
* 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 "new Double(" + 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) {
Object old = _widget.getText();
if(!(value instanceof Double)) {
value = new Double(0);
}
_widget.setText(value.toString());
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() {
Double retval = null;
try {
retval = new Double(_widget.getText());
}
catch(NumberFormatException ex) {
retval = new Double(0);
_widget.setText(retval.toString());
}
return retval;
}
/**
* 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) {
Object old = _widget.getText();
Double val = null;
try {
val = new Double(text);
}
catch(NumberFormatException ex) {
val = new Double(0);
}
text = val.toString();
_widget.setText(text);
firePropertyChange(old, text);
}
/**
* @return The property value as a human editable string.
* <p> Returns null if the value can't be expressed
* as an editable string.
* <p> If a non-null value is returned, then the PropertyEditor should
* be prepared to parse that string back in setAsText().
*/
public String getAsText() {
return _widget.getText();
}
}
1.1 jakarta-ant/src/antidote/org/apache/tools/ant/gui/customizer/DynamicCustomizer.java
Index: DynamicCustomizer.java
===================================================================
/*
* 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
* <http://www.apache.org/>.
*/
package org.apache.tools.ant.gui.customizer;
import org.apache.tools.ant.gui.LabelFieldGBC;
import java.lang.reflect.*;
import java.beans.*;
import javax.swing.*;
import java.util.Hashtable;
import java.util.Enumeration;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Component;
/**
* Widget for dynamically constructing a property editor based on the
* an Object's BeanInfo. Essentially a property sheet.
*
* @version $Revision: 1.1 $
* @author Simeon Fitch
*/
public class DynamicCustomizer extends JPanel {
static {
PropertyEditorManager.registerEditor(
String.class, StringPropertyEditor.class);
PropertyEditorManager.registerEditor(
int.class, IntegerPropertyEditor.class);
PropertyEditorManager.registerEditor(
Integer.class, IntegerPropertyEditor.class);
PropertyEditorManager.registerEditor(
double.class, DoublePropertyEditor.class);
PropertyEditorManager.registerEditor(
Double.class, DoublePropertyEditor.class);
}
/** The type that this editor instance can handle. */
private Class _type = null;
/** The value currently being edited. */
private Object _value = null;
/** Mapping from PropertyDescriptor to PropertyEditor. */
private Hashtable _prop2Editor = new Hashtable();
/** Mapping from PropertyEditor to field PropertyDescriptor. */
private Hashtable _editor2Prop = new Hashtable();
/** Listener for receiving change events from the editors. */
private EditorChangeListener _eListener = new EditorChangeListener();
/** Read-only flag. */
private boolean _readOnly = false;
/**
* Standard constructor.
*
* @param type Type that you are going to be creating and editor for.
*/
public DynamicCustomizer(Class type) {
this(type, false);
}
/**
* Standard constructor.
*
* @param type Type that you are going to be creating and editor for.
* @param readOnly Set to true to create a read-only customizer.
*/
public DynamicCustomizer(Class type, boolean readOnly) {
super(new GridBagLayout());
_readOnly = readOnly;
_type = type;
LabelFieldGBC gbc = new LabelFieldGBC();
try {
BeanInfo info = Introspector.getBeanInfo(type);
setBorder(BorderFactory.createTitledBorder(
info.getBeanDescriptor().getDisplayName()));
PropertyDescriptor[] props = info.getPropertyDescriptors();
for(int i = 0; i < props.length; i++) {
if(props[i].getName().equals("class")) continue;
JLabel label = new JLabel(props[i].getDisplayName() + ":");
// Lookup the editor.
PropertyEditor editor = getEditorForProperty(props[i]);
if(editor == null) continue;
// Add a listener to the editor so we know when to update
// the bean's fields.
editor.addPropertyChangeListener(_eListener);
// XXX What we need to do right here is provide a component
// that makes use of the "paintable" capability of the editor.
Component comp = editor.getCustomEditor();
if(comp == null) {
comp = new JLabel("<<null editor>>");
}
// See if it is a read-only property. If so, then just
// display it.
if(_readOnly || props[i].getWriteMethod() == null) {
comp.setEnabled(false);
}
// Setup the accellerator key.
label.setLabelFor(comp);
label.setDisplayedMnemonic(label.getText().charAt(0));
// Set the tool tip text, if any.
String tip = props[i].getShortDescription();
if(tip != null) {
label.setToolTipText(tip);
if(comp instanceof JComponent) {
((JComponent)comp).setToolTipText(tip);
}
}
// Add the label and fields.
add(label, gbc.forLabel());
add(comp, gbc.forField());
// Set the mappings between editor and property, etc. for
// quick lookup later.
_prop2Editor.put(props[i], editor);
_editor2Prop.put(editor, props[i]);
}
// Filler...
add(new JLabel(), gbc.forLastLabel());
}
catch(Exception ex) {
ex.printStackTrace();
}
}
/**
* Set the object to be edited.
*
* @param value The object to be edited.
*/
public void setObject(Object value) {
if(!(_type.isInstance(value))) {
throw new IllegalArgumentException(
value.getClass() + " is not of type " + _type);
}
_value = value;
// Iterate over each property, doing a lookup on the associated editor
// and setting the editor's value to the value of the property.
Enumeration enum = _prop2Editor.keys();
while(enum.hasMoreElements()) {
PropertyDescriptor desc = (PropertyDescriptor) enum.nextElement();
PropertyEditor editor = (PropertyEditor) _prop2Editor.get(desc);
Method reader = desc.getReadMethod();
if(reader != null) {
try {
Object val = reader.invoke(_value, null);
editor.setValue(val);
}
catch(IllegalAccessException ex) {
ex.printStackTrace();
}
catch(InvocationTargetException ex) {
ex.getTargetException().printStackTrace();
}
}
}
}
private PropertyEditor getEditorForProperty(PropertyDescriptor prop) {
PropertyEditor retval = null;
Class type = prop.getPropertyEditorClass();
if(type != null) {
try {
retval = (PropertyEditor) type.newInstance();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
// Handle case where there is no special editor
// associated with the property. In that case we ask the
// PropertyEditor manager for the editor registered for the
// given property type.
if(retval == null) {
retval = PropertyEditorManager.findEditor(prop.getPropertyType());
}
return retval;
}
/** Class for receiving change events from teh PropertyEditor objects. */
private class EditorChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
PropertyEditor editor = (PropertyEditor) e.getSource();
PropertyDescriptor prop =
(PropertyDescriptor) _editor2Prop.get(editor);
Method writer = prop.getWriteMethod();
if(writer != null) {
try {
Object[] params = { editor.getValue() };
writer.invoke(_value, params);
//firePropertyChange(
//prop.getName(), null, editor.getValue());
}
catch(IllegalAccessException ex) {
ex.printStackTrace();
}
catch(InvocationTargetException ex) {
ex.getTargetException().printStackTrace();
}
}
}
}
/**
* Test code.
*
* @param args First arg is the class name to create
*/
public static void main(String[] args) {
try {
Class c = Class.forName(args[0]);
JFrame f = new JFrame(c.getName());
DynamicCustomizer custom =
new DynamicCustomizer(c);
custom.setObject(c.newInstance());
f.getContentPane().add(custom);
f.pack();
f.setVisible(true);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
1.1 jakarta-ant/src/antidote/org/apache/tools/ant/gui/customizer/IntegerPropertyEditor.java
Index: IntegerPropertyEditor.java
===================================================================
/*
* 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
* <http://www.apache.org/>.
*/
package org.apache.tools.ant.gui.customizer;
import javax.swing.*;
import java.awt.Component;
/**
* Custom property editor for integer types.
*
* @version $Revision: 1.1 $
* @author Simeon Fitch
*/
public class IntegerPropertyEditor extends AbstractPropertyEditor {
/** Editing widget. */
private JTextField _widget = null;
/**
* Default ctor.
*
*/
public IntegerPropertyEditor() {
_widget = new JTextField();
_widget.addFocusListener(new FocusHandler(this));
}
/**
* 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.
* <p>
* 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 "new Integer(" + 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) {
Object old = _widget.getText();
if(!(value instanceof Integer)) {
value = new Integer(0);
}
_widget.setText(value.toString());
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() {
Integer retval = null;
try {
retval = new Integer(_widget.getText());
}
catch(NumberFormatException ex) {
retval = new Integer(0);
_widget.setText(retval.toString());
}
return retval;
}
/**
* 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) {
Object old = _widget.getText();
Integer val = null;
try {
val = new Integer(text);
}
catch(NumberFormatException ex) {
val = new Integer(0);
}
text = val.toString();
_widget.setText(text);
firePropertyChange(old, text);
}
/**
* @return The property value as a human editable string.
* <p> Returns null if the value can't be expressed
* as an editable string.
* <p> If a non-null value is returned, then the PropertyEditor should
* be prepared to parse that string back in setAsText().
*/
public String getAsText() {
return _widget.getText();
}
}
1.1 jakarta-ant/src/antidote/org/apache/tools/ant/gui/customizer/StringPropertyEditor.java
Index: StringPropertyEditor.java
===================================================================
/*
* 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
* <http://www.apache.org/>.
*/
package org.apache.tools.ant.gui.customizer;
import javax.swing.*;
import java.awt.Component;
/**
* Custom property editor for string types.
*
* @version $Revision: 1.1 $
* @author Simeon Fitch
*/
public class StringPropertyEditor extends AbstractPropertyEditor {
private JTextField _widget = null;
/**
* Default ctor.
*
*/
public StringPropertyEditor() {
_widget = new JTextField();
_widget.addFocusListener(new FocusHandler(this));
}
/**
* 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.
* <p>
* 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) {
Object old = _widget.getText();
_widget.setText(String.valueOf(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 _widget.getText();
}
/**
* 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 {
Object old = _widget.getText();
_widget.setText(text);
firePropertyChange(old, text);
}
/**
* @return The property value as a human editable string.
* <p> Returns null if the value can't be expressed
* as an editable string.
* <p> If a non-null value is returned, then the PropertyEditor should
* be prepared to parse that string back in setAsText().
*/
public String getAsText() {
return _widget.getText();
}
}