From b3d92ad4bdcd3568db2c01478bdfaa48e915a90c Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Mon, 18 Apr 2011 14:14:35 +0000 Subject: [PATCH] #6860 introduced AbstractProperty with common functionality of most property implementations (listener management etc.), TextFileProperty now implements ReadOnlyStatusChangeListener svn changeset:18360/svn branch:6.6 --- .../vaadin/data/util/AbstractProperty.java | 171 +++++++++++++++++ src/com/vaadin/data/util/MethodProperty.java | 167 +---------------- src/com/vaadin/data/util/ObjectProperty.java | 175 +----------------- .../vaadin/data/util/PropertyFormatter.java | 156 +--------------- .../vaadin/data/util/TextFileProperty.java | 95 ++-------- 5 files changed, 191 insertions(+), 573 deletions(-) create mode 100644 src/com/vaadin/data/util/AbstractProperty.java diff --git a/src/com/vaadin/data/util/AbstractProperty.java b/src/com/vaadin/data/util/AbstractProperty.java new file mode 100644 index 0000000000..9db9013d70 --- /dev/null +++ b/src/com/vaadin/data/util/AbstractProperty.java @@ -0,0 +1,171 @@ +package com.vaadin.data.util; + +import java.util.LinkedList; + +import com.vaadin.data.Property; + +/** + * Abstract base class for {@link Property} implementations. + * + * Handles listener management for {@link ValueChangeListener}s and + * {@link ReadOnlyStatusChangeListener}s. + * + * @since 6.6 + */ +public abstract class AbstractProperty implements Property, + Property.ValueChangeNotifier, Property.ReadOnlyStatusChangeNotifier { + + /** + * List of listeners who are interested in the read-only status changes of + * the Property + */ + private LinkedList readOnlyStatusChangeListeners = null; + + /** + * List of listeners who are interested in the value changes of the Property + */ + private LinkedList valueChangeListeners = null; + + /** + * Returns the value of the Property in human readable textual + * format. The return value should be assignable to the + * setValue method if the Property is not in read-only mode. + * + * @return String representation of the value stored in the Property + */ + @Override + public String toString() { + final Object value = getValue(); + if (value == null) { + return null; + } + return value.toString(); + } + + /* Events */ + + /** + * An Event object specifying the Property whose read-only + * status has been changed. + */ + protected class ReadOnlyStatusChangeEvent extends java.util.EventObject + implements Property.ReadOnlyStatusChangeEvent { + + /** + * Constructs a new read-only status change event for this object. + * + * @param source + * source object of the event. + */ + protected ReadOnlyStatusChangeEvent(Property source) { + super(source); + } + + /** + * Gets the Property whose read-only state has changed. + * + * @return source Property of the event. + */ + public Property getProperty() { + return (Property) getSource(); + } + + } + + /** + * Registers a new read-only status change listener for this Property. + * + * @param listener + * the new Listener to be registered. + */ + public void addListener(Property.ReadOnlyStatusChangeListener listener) { + if (readOnlyStatusChangeListeners == null) { + readOnlyStatusChangeListeners = new LinkedList(); + } + readOnlyStatusChangeListeners.add(listener); + } + + /** + * Removes a previously registered read-only status change listener. + * + * @param listener + * the listener to be removed. + */ + public void removeListener(Property.ReadOnlyStatusChangeListener listener) { + if (readOnlyStatusChangeListeners != null) { + readOnlyStatusChangeListeners.remove(listener); + } + } + + /** + * Sends a read only status change event to all registered listeners. + */ + protected void fireReadOnlyStatusChange() { + if (readOnlyStatusChangeListeners != null) { + final Object[] l = readOnlyStatusChangeListeners.toArray(); + final Property.ReadOnlyStatusChangeEvent event = new ReadOnlyStatusChangeEvent( + this); + for (int i = 0; i < l.length; i++) { + ((Property.ReadOnlyStatusChangeListener) l[i]) + .readOnlyStatusChange(event); + } + } + } + + /** + * An Event object specifying the Property whose value has been + * changed. + */ + private class ValueChangeEvent extends java.util.EventObject implements + Property.ValueChangeEvent { + + /** + * Constructs a new value change event for this object. + * + * @param source + * source object of the event. + */ + protected ValueChangeEvent(Property source) { + super(source); + } + + /** + * Gets the Property whose value has changed. + * + * @return source Property of the event. + */ + public Property getProperty() { + return (Property) getSource(); + } + + } + + public void addListener(ValueChangeListener listener) { + if (valueChangeListeners == null) { + valueChangeListeners = new LinkedList(); + } + valueChangeListeners.add(listener); + + } + + public void removeListener(ValueChangeListener listener) { + if (valueChangeListeners != null) { + valueChangeListeners.remove(listener); + } + + } + + /** + * Sends a value change event to all registered listeners. + */ + protected void fireValueChange() { + if (valueChangeListeners != null) { + final Object[] l = valueChangeListeners.toArray(); + final Property.ValueChangeEvent event = new ValueChangeEvent(this); + for (int i = 0; i < l.length; i++) { + ((Property.ValueChangeListener) l[i]).valueChange(event); + } + } + } + +} diff --git a/src/com/vaadin/data/util/MethodProperty.java b/src/com/vaadin/data/util/MethodProperty.java index 12b69858da..b04fc77b61 100644 --- a/src/com/vaadin/data/util/MethodProperty.java +++ b/src/com/vaadin/data/util/MethodProperty.java @@ -8,7 +8,6 @@ import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.LinkedList; import java.util.logging.Level; import java.util.logging.Logger; @@ -48,8 +47,7 @@ import com.vaadin.util.SerializerHelper; * @since 3.0 */ @SuppressWarnings("serial") -public class MethodProperty implements Property, - Property.ValueChangeNotifier, Property.ReadOnlyStatusChangeNotifier { +public class MethodProperty extends AbstractProperty { private static final Logger logger = Logger.getLogger(MethodProperty.class .getName()); @@ -85,18 +83,6 @@ public class MethodProperty implements Property, */ private transient Class type; - /** - * List of listeners who are interested in the read-only status changes of - * the MethodProperty - */ - private LinkedList readOnlyStatusChangeListeners = null; - - /** - * List of listeners who are interested in the value changes of the - * MethodProperty - */ - private LinkedList valueChangeListeners = null; - /* Special serialization to handle method references */ private void writeObject(java.io.ObjectOutputStream out) throws IOException { out.defaultWriteObject(); @@ -628,22 +614,6 @@ public class MethodProperty implements Property, } } - /** - * Returns the value of the MethodProperty in human readable - * textual format. The return value should be assignable to the - * setValue method if the Property is not in read-only mode. - * - * @return String representation of the value stored in the Property - */ - @Override - public String toString() { - final Object value = getValue(); - if (value == null) { - return null; - } - return value.toString(); - } - /** *

* Sets the setter method and getter method argument lists. @@ -852,140 +822,15 @@ public class MethodProperty implements Property, } } - /* Events */ - - /** - * An Event object specifying the Property whose read-only - * status has been changed. - * - * @author IT Mill Ltd. - * @version - * @VERSION@ - * @since 3.0 - */ - private class ReadOnlyStatusChangeEvent extends java.util.EventObject - implements Property.ReadOnlyStatusChangeEvent { - - /** - * Constructs a new read-only status change event for this object. - * - * @param source - * source object of the event. - */ - protected ReadOnlyStatusChangeEvent(MethodProperty source) { - super(source); - } - - /** - * Gets the Property whose read-only state has changed. - * - * @return source Property of the event. - */ - public Property getProperty() { - return (Property) getSource(); - } - - } - - /** - * Registers a new read-only status change listener for this Property. - * - * @param listener - * the new Listener to be registered. - */ - public void addListener(Property.ReadOnlyStatusChangeListener listener) { - if (readOnlyStatusChangeListeners == null) { - readOnlyStatusChangeListeners = new LinkedList(); - } - readOnlyStatusChangeListeners.add(listener); - } - - /** - * Removes a previously registered read-only status change listener. - * - * @param listener - * the listener to be removed. - */ - public void removeListener(Property.ReadOnlyStatusChangeListener listener) { - if (readOnlyStatusChangeListeners != null) { - readOnlyStatusChangeListeners.remove(listener); - } - } - - /** - * Sends a read only status change event to all registered listeners. - */ - private void fireReadOnlyStatusChange() { - if (readOnlyStatusChangeListeners != null) { - final Object[] l = readOnlyStatusChangeListeners.toArray(); - final Property.ReadOnlyStatusChangeEvent event = new ReadOnlyStatusChangeEvent( - this); - for (int i = 0; i < l.length; i++) { - ((Property.ReadOnlyStatusChangeListener) l[i]) - .readOnlyStatusChange(event); - } - } - } - - /** - * An Event object specifying the Property whose value has been - * changed. - * - * @author IT Mill Ltd. - * @version - * @VERSION@ - * @since 5.3 - */ - private class ValueChangeEvent extends java.util.EventObject implements - Property.ValueChangeEvent { - - /** - * Constructs a new value change event for this object. - * - * @param source - * source object of the event. - */ - protected ValueChangeEvent(MethodProperty source) { - super(source); - } - - /** - * Gets the Property whose value has changed. - * - * @return source Property of the event. - */ - public Property getProperty() { - return (Property) getSource(); - } - - } - - public void addListener(ValueChangeListener listener) { - if (valueChangeListeners == null) { - valueChangeListeners = new LinkedList(); - } - valueChangeListeners.add(listener); - - } - - public void removeListener(ValueChangeListener listener) { - if (valueChangeListeners != null) { - valueChangeListeners.remove(listener); - } - - } - /** * Sends a value change event to all registered listeners. + * + * Public for backwards compatibility, visibility may be reduced in future + * versions. */ + @Override public void fireValueChange() { - if (valueChangeListeners != null) { - final Object[] l = valueChangeListeners.toArray(); - final Property.ValueChangeEvent event = new ValueChangeEvent(this); - for (int i = 0; i < l.length; i++) { - ((Property.ValueChangeListener) l[i]).valueChange(event); - } - } + super.fireValueChange(); } } diff --git a/src/com/vaadin/data/util/ObjectProperty.java b/src/com/vaadin/data/util/ObjectProperty.java index 6724f1d40f..ee38bc0797 100644 --- a/src/com/vaadin/data/util/ObjectProperty.java +++ b/src/com/vaadin/data/util/ObjectProperty.java @@ -5,7 +5,6 @@ package com.vaadin.data.util; import java.lang.reflect.Constructor; -import java.util.LinkedList; import com.vaadin.data.Property; @@ -20,8 +19,7 @@ import com.vaadin.data.Property; * @since 3.0 */ @SuppressWarnings("serial") -public class ObjectProperty implements Property, - Property.ValueChangeNotifier, Property.ReadOnlyStatusChangeNotifier { +public class ObjectProperty extends AbstractProperty { /** * A boolean value storing the Property's read-only status information. @@ -38,16 +36,6 @@ public class ObjectProperty implements Property, */ private final Class type; - /** - * Internal list of registered value change listeners. - */ - private LinkedList valueChangeListeners = null; - - /** - * Internal list of registered read-only status change listeners. - */ - private LinkedList readOnlyStatusChangeListeners = null; - /** * Creates a new instance of ObjectProperty with the given value. The type * of the property is automatically initialized to be the type of the given @@ -124,24 +112,6 @@ public class ObjectProperty implements Property, return value; } - /** - * Returns the value of the ObjectProperty in human readable textual format. - * The return value should be assignable to the setValue method - * if the Property is not in read-only mode. - * - * @return String representation of the value stored in the - * ObjectProperty - */ - @Override - public String toString() { - final Object value = getValue(); - if (value != null) { - return value.toString(); - } else { - return null; - } - } - /** * Tests if the Property is in read-only mode. In read-only mode calls to * the method setValue will throw @@ -214,147 +184,4 @@ public class ObjectProperty implements Property, fireValueChange(); } - /* Events */ - - /** - * An Event object specifying the ObjectProperty whose value - * has changed. - * - * @author IT Mill Ltd. - * @version - * @VERSION@ - * @since 3.0 - */ - private class ValueChangeEvent extends java.util.EventObject implements - Property.ValueChangeEvent { - - /** - * Constructs a new value change event for this object. - * - * @param source - * the source object of the event. - */ - protected ValueChangeEvent(ObjectProperty source) { - super(source); - } - - /** - * Gets the Property whose read-only state has changed. - * - * @return source the Property of the event. - */ - public Property getProperty() { - return (Property) getSource(); - } - } - - /** - * An Event object specifying the Property whose read-only - * status has been changed. - * - * @author IT Mill Ltd. - * @version - * @VERSION@ - * @since 3.0 - */ - private class ReadOnlyStatusChangeEvent extends java.util.EventObject - implements Property.ReadOnlyStatusChangeEvent { - - /** - * Constructs a new read-only status change event for this object. - * - * @param source - * source object of the event - */ - protected ReadOnlyStatusChangeEvent(ObjectProperty source) { - super(source); - } - - /** - * Gets the Property whose read-only state has changed. - * - * @return source Property of the event. - */ - public Property getProperty() { - return (Property) getSource(); - } - } - - /** - * Removes a previously registered value change listener. - * - * @param listener - * the listener to be removed. - */ - public void removeListener(Property.ValueChangeListener listener) { - if (valueChangeListeners != null) { - valueChangeListeners.remove(listener); - } - } - - /** - * Registers a new value change listener for this ObjectProperty. - * - * @param listener - * the new Listener to be registered - */ - public void addListener(Property.ValueChangeListener listener) { - if (valueChangeListeners == null) { - valueChangeListeners = new LinkedList(); - } - valueChangeListeners.add(listener); - } - - /** - * Registers a new read-only status change listener for this Property. - * - * @param listener - * the new Listener to be registered - */ - public void addListener(Property.ReadOnlyStatusChangeListener listener) { - if (readOnlyStatusChangeListeners == null) { - readOnlyStatusChangeListeners = new LinkedList(); - } - readOnlyStatusChangeListeners.add(listener); - } - - /** - * Removes a previously registered read-only status change listener. - * - * @param listener - * the listener to be removed. - */ - public void removeListener(Property.ReadOnlyStatusChangeListener listener) { - if (readOnlyStatusChangeListeners != null) { - readOnlyStatusChangeListeners.remove(listener); - } - } - - /** - * Sends a value change event to all registered listeners. - */ - private void fireValueChange() { - if (valueChangeListeners != null) { - final Object[] l = valueChangeListeners.toArray(); - final Property.ValueChangeEvent event = new ValueChangeEvent(this); - for (int i = 0; i < l.length; i++) { - ((Property.ValueChangeListener) l[i]).valueChange(event); - } - } - } - - /** - * Sends a read only status change event to all registered listeners. - */ - private void fireReadOnlyStatusChange() { - if (readOnlyStatusChangeListeners != null) { - final Object[] l = readOnlyStatusChangeListeners.toArray(); - final Property.ReadOnlyStatusChangeEvent event = new ReadOnlyStatusChangeEvent( - this); - for (int i = 0; i < l.length; i++) { - ((Property.ReadOnlyStatusChangeListener) l[i]) - .readOnlyStatusChange(event); - } - } - } } diff --git a/src/com/vaadin/data/util/PropertyFormatter.java b/src/com/vaadin/data/util/PropertyFormatter.java index 23e4fb324f..c43a4771dc 100644 --- a/src/com/vaadin/data/util/PropertyFormatter.java +++ b/src/com/vaadin/data/util/PropertyFormatter.java @@ -3,8 +3,6 @@ */ package com.vaadin.data.util; -import java.util.LinkedList; - import com.vaadin.data.Property; /** @@ -35,20 +33,8 @@ import com.vaadin.data.Property; * @since 5.3.0 */ @SuppressWarnings("serial") -public abstract class PropertyFormatter implements Property, - Property.ValueChangeNotifier, Property.ValueChangeListener, - Property.ReadOnlyStatusChangeListener, - Property.ReadOnlyStatusChangeNotifier { - - /** - * Internal list of registered value change listeners. - */ - private LinkedList valueChangeListeners = null; - - /** - * Internal list of registered read-only status change listeners. - */ - private LinkedList readOnlyStatusChangeListeners = null; +public abstract class PropertyFormatter extends AbstractProperty implements + Property.ValueChangeListener, Property.ReadOnlyStatusChangeListener { /** Datasource that stores the actual value. */ Property dataSource; @@ -236,144 +222,6 @@ public abstract class PropertyFormatter implements Property, } } - /** - * An Event object specifying the ObjectProperty whose value - * has changed. - * - * @author IT Mill Ltd. - * @since 5.3.0 - */ - private class ValueChangeEvent extends java.util.EventObject implements - Property.ValueChangeEvent { - - /** - * Constructs a new value change event for this object. - * - * @param source - * the source object of the event. - */ - protected ValueChangeEvent(PropertyFormatter source) { - super(source); - } - - /** - * Gets the Property whose read-only state has changed. - * - * @return source the Property of the event. - */ - public Property getProperty() { - return (Property) getSource(); - } - } - - /** - * An Event object specifying the Property whose read-only - * status has been changed. - * - * @author IT Mill Ltd. - * @since 5.3.0 - */ - private class ReadOnlyStatusChangeEvent extends java.util.EventObject - implements Property.ReadOnlyStatusChangeEvent { - - /** - * Constructs a new read-only status change event for this object. - * - * @param source - * source object of the event - */ - protected ReadOnlyStatusChangeEvent(PropertyFormatter source) { - super(source); - } - - /** - * Gets the Property whose read-only state has changed. - * - * @return source Property of the event. - */ - public Property getProperty() { - return (Property) getSource(); - } - } - - /** - * Removes a previously registered value change listener. - * - * @param listener - * the listener to be removed. - */ - public void removeListener(Property.ValueChangeListener listener) { - if (valueChangeListeners != null) { - valueChangeListeners.remove(listener); - } - } - - /** - * Registers a new value change listener for this ObjectProperty. - * - * @param listener - * the new Listener to be registered - */ - public void addListener(Property.ValueChangeListener listener) { - if (valueChangeListeners == null) { - valueChangeListeners = new LinkedList(); - } - valueChangeListeners.add(listener); - } - - /** - * Registers a new read-only status change listener for this Property. - * - * @param listener - * the new Listener to be registered - */ - public void addListener(Property.ReadOnlyStatusChangeListener listener) { - if (readOnlyStatusChangeListeners == null) { - readOnlyStatusChangeListeners = new LinkedList(); - } - readOnlyStatusChangeListeners.add(listener); - } - - /** - * Removes a previously registered read-only status change listener. - * - * @param listener - * the listener to be removed. - */ - public void removeListener(Property.ReadOnlyStatusChangeListener listener) { - if (readOnlyStatusChangeListeners != null) { - readOnlyStatusChangeListeners.remove(listener); - } - } - - /** - * Sends a value change event to all registered listeners. - */ - private void fireValueChange() { - if (valueChangeListeners != null) { - final Object[] l = valueChangeListeners.toArray(); - final Property.ValueChangeEvent event = new ValueChangeEvent(this); - for (int i = 0; i < l.length; i++) { - ((Property.ValueChangeListener) l[i]).valueChange(event); - } - } - } - - /** - * Sends a read only status change event to all registered listeners. - */ - private void fireReadOnlyStatusChange() { - if (readOnlyStatusChangeListeners != null) { - final Object[] l = readOnlyStatusChangeListeners.toArray(); - final Property.ReadOnlyStatusChangeEvent event = new ReadOnlyStatusChangeEvent( - this); - for (int i = 0; i < l.length; i++) { - ((Property.ReadOnlyStatusChangeListener) l[i]) - .readOnlyStatusChange(event); - } - } - } - /** * Listens for changes in the datasource. * diff --git a/src/com/vaadin/data/util/TextFileProperty.java b/src/com/vaadin/data/util/TextFileProperty.java index 8d99112338..005372a04b 100644 --- a/src/com/vaadin/data/util/TextFileProperty.java +++ b/src/com/vaadin/data/util/TextFileProperty.java @@ -14,23 +14,23 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.nio.charset.Charset; -import java.util.LinkedList; - -import com.vaadin.data.Property; /** * Property implementation for wrapping a text file. * - * Supports reading and writing of a File from/to String. ValueChangeNotifiers - * are supported, but only fire when setValue(Object) is explicitly called. + * Supports reading and writing of a File from/to String. + * + * {@link ValueChangeListener}s are supported, but only fire when + * setValue(Object) is explicitly called. {@link ReadOnlyStatusChangeListener}s + * are supported but only fire when setReadOnly(boolean) is explicitly called. + * */ @SuppressWarnings("serial") -public class TextFileProperty implements Property, Property.ValueChangeNotifier { +public class TextFileProperty extends AbstractProperty { private File file; private boolean readonly; private Charset charset = null; - private LinkedList valueChangeListeners = null; /** * Wrap given file with property interface. @@ -115,7 +115,11 @@ public class TextFileProperty implements Property, Property.ValueChangeNotifier * @see com.vaadin.data.Property#setReadOnly(boolean) */ public void setReadOnly(boolean newStatus) { + boolean oldStatus = readonly; readonly = newStatus; + if (isReadOnly() != oldStatus) { + fireReadOnlyStatusChange(); + } } /* @@ -147,81 +151,4 @@ public class TextFileProperty implements Property, Property.ValueChangeNotifier } } - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return (String) getValue(); - } - - /* Events */ - - /** - * An Event object specifying the TextFileProperty whose value - * has changed. - */ - private class ValueChangeEvent extends java.util.EventObject implements - Property.ValueChangeEvent { - - /** - * Constructs a new value change event for this object. - * - * @param source - * the source object of the event. - */ - protected ValueChangeEvent(TextFileProperty source) { - super(source); - } - - /** - * Gets the Property whose read-only state has changed. - * - * @return source the Property of the event. - */ - public Property getProperty() { - return (Property) getSource(); - } - } - - /** - * Removes a previously registered value change listener. - * - * @param listener - * the listener to be removed. - */ - public void removeListener(Property.ValueChangeListener listener) { - if (valueChangeListeners != null) { - valueChangeListeners.remove(listener); - } - } - - /** - * Registers a new value change listener for this TextFileProperty. - * - * @param listener - * the new Listener to be registered - */ - public void addListener(Property.ValueChangeListener listener) { - if (valueChangeListeners == null) { - valueChangeListeners = new LinkedList(); - } - valueChangeListeners.add(listener); - } - - /** - * Sends a value change event to all registered listeners. - */ - private void fireValueChange() { - if (valueChangeListeners != null) { - final Object[] l = valueChangeListeners.toArray(); - final ValueChangeEvent event = new ValueChangeEvent(this); - for (int i = 0; i < l.length; i++) { - ((ValueChangeListener) l[i]).valueChange(event); - } - } - } - } -- 2.39.5