]> source.dussan.org Git - vaadin-framework.git/commitdiff
Now implements ValueChangeNotifier. Fixes #1978.
authorMarc Englund <marc.englund@itmill.com>
Wed, 20 Aug 2008 07:44:17 +0000 (07:44 +0000)
committerMarc Englund <marc.englund@itmill.com>
Wed, 20 Aug 2008 07:44:17 +0000 (07:44 +0000)
svn changeset:5221/svn branch:trunk

src/com/itmill/toolkit/data/util/MethodProperty.java

index 6bedad2729c5a9f6a2769e557d2e90f56df6523d..f5ed070960f8cab01c8161ba332e5105207cd4f7 100644 (file)
@@ -31,12 +31,20 @@ import com.itmill.toolkit.data.Property;
  * resulting MethodProperty is read-only.
  * </p>
  * 
+ * <p>
+ * MethodProperty implements Property.ValueChangeNotifier, but does not
+ * automatically know whether or not the getter method will actually return a
+ * new value - value change listeners are always notified when setValue is
+ * called, without verifying what the getter returns.
+ * </p>
+ * 
  * @author IT Mill Ltd.
  * @version
  * @VERSION@
  * @since 3.0
  */
-public class MethodProperty implements Property {
+public class MethodProperty implements Property, Property.ValueChangeNotifier,
+        Property.ReadOnlyStatusChangeNotifier {
 
     /**
      * The object that includes the property the MethodProperty is bound to.
@@ -76,6 +84,12 @@ public class MethodProperty implements Property {
      */
     private LinkedList readOnlyStatusChangeListeners = null;
 
+    /**
+     * List of listeners who are interested in the value changes of the
+     * MethodProperty
+     */
+    private LinkedList valueChangeListeners = null;
+
     /**
      * <p>
      * Creates a new instance of <code>MethodProperty</code> from a named bean
@@ -628,6 +642,7 @@ public class MethodProperty implements Property {
             // Creates new object from the string
             invokeSetMethod(value);
         }
+        fireValueChange();
     }
 
     /**
@@ -816,4 +831,66 @@ public class MethodProperty implements Property {
         }
     }
 
+    /**
+     * An <code>Event</code> 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.
+     */
+    private void fireValueChange() {
+        if (valueChangeListeners != null) {
+            final Object[] l = valueChangeListeners.toArray();
+            final Property.ValueChangeEvent event = new MethodProperty.ValueChangeEvent(
+                    this);
+            for (int i = 0; i < l.length; i++) {
+                ((Property.ValueChangeListener) l[i]).valueChange(event);
+            }
+        }
+    }
+
 }