]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add Property and Item change notifiers for generated properties (#13334)
authorTeemu Suo-Anttila <teemusa@vaadin.com>
Fri, 19 Sep 2014 09:30:57 +0000 (12:30 +0300)
committerJohannes Dahlström <johannesd@vaadin.com>
Fri, 19 Sep 2014 13:40:24 +0000 (16:40 +0300)
Change-Id: I35a212b7f32f7c75333ee394f6dc102802d4d46e

server/src/com/vaadin/data/util/GeneratedPropertyContainer.java
server/tests/src/com/vaadin/data/util/GeneratedPropertyContainerTest.java

index 979f62230002868ae551698954fcb4b36a56953c..4dd190908e46a3580141c23da49394b396a439fe 100644 (file)
@@ -39,8 +39,9 @@ import com.vaadin.ui.components.grid.sort.SortOrder;
  * @since
  * @author Vaadin Ltd
  */
-public class GeneratedPropertyContainer implements Container.Indexed,
-        Container.Sortable, Container.Filterable {
+public class GeneratedPropertyContainer extends AbstractContainer implements
+        Container.Indexed, Container.Sortable, Container.Filterable,
+        Container.PropertySetChangeNotifier, Container.ItemSetChangeNotifier {
 
     private final Container.Indexed wrappedContainer;
     private final Map<Object, PropertyValueGenerator<?>> propertyGenerators;
@@ -148,15 +149,43 @@ public class GeneratedPropertyContainer implements Container.Indexed,
     public GeneratedPropertyContainer(Container.Indexed container) {
         wrappedContainer = container;
         propertyGenerators = new HashMap<Object, PropertyValueGenerator<?>>();
+
         if (wrappedContainer instanceof Sortable) {
             sortableContainer = (Sortable) wrappedContainer;
         }
+
         if (wrappedContainer instanceof Filterable) {
             activeFilters = new HashMap<Filter, List<Filter>>();
             filterableContainer = (Filterable) wrappedContainer;
         } else {
             activeFilters = null;
         }
+
+        // ItemSetChangeEvents
+        if (wrappedContainer instanceof ItemSetChangeNotifier) {
+            ((ItemSetChangeNotifier) wrappedContainer)
+                    .addItemSetChangeListener(new ItemSetChangeListener() {
+
+                        @Override
+                        public void containerItemSetChange(
+                                ItemSetChangeEvent event) {
+                            fireItemSetChange();
+                        }
+                    });
+        }
+
+        // PropertySetChangeEvents
+        if (wrappedContainer instanceof PropertySetChangeNotifier) {
+            ((PropertySetChangeNotifier) wrappedContainer)
+                    .addPropertySetChangeListener(new PropertySetChangeListener() {
+
+                        @Override
+                        public void containerPropertySetChange(
+                                PropertySetChangeEvent event) {
+                            fireContainerPropertySetChange();
+                        }
+                    });
+        }
     }
 
     /* Functions related to generated properties */
@@ -174,7 +203,7 @@ public class GeneratedPropertyContainer implements Container.Indexed,
     public void addGeneratedProperty(Object propertyId,
             PropertyValueGenerator<?> generator) {
         propertyGenerators.put(propertyId, generator);
-        // TODO: Fire event
+        fireContainerPropertySetChange();
     }
 
     /**
@@ -187,7 +216,7 @@ public class GeneratedPropertyContainer implements Container.Indexed,
     public void removeGeneratedProperty(Object propertyId) {
         if (propertyGenerators.containsKey(propertyId)) {
             propertyGenerators.remove(propertyId);
-            // TODO: Fire event
+            fireContainerPropertySetChange();
         }
     }
 
@@ -218,6 +247,49 @@ public class GeneratedPropertyContainer implements Container.Indexed,
         }
     }
 
+    /* Listener functionality */
+
+    @Override
+    public void addItemSetChangeListener(ItemSetChangeListener listener) {
+        super.addItemSetChangeListener(listener);
+    }
+
+    @Override
+    public void addListener(ItemSetChangeListener listener) {
+        super.addListener(listener);
+    }
+
+    @Override
+    public void removeItemSetChangeListener(ItemSetChangeListener listener) {
+        super.removeItemSetChangeListener(listener);
+    }
+
+    @Override
+    public void removeListener(ItemSetChangeListener listener) {
+        super.removeListener(listener);
+    }
+
+    @Override
+    public void addPropertySetChangeListener(PropertySetChangeListener listener) {
+        super.addPropertySetChangeListener(listener);
+    }
+
+    @Override
+    public void addListener(PropertySetChangeListener listener) {
+        super.addListener(listener);
+    }
+
+    @Override
+    public void removePropertySetChangeListener(
+            PropertySetChangeListener listener) {
+        super.removePropertySetChangeListener(listener);
+    }
+
+    @Override
+    public void removeListener(PropertySetChangeListener listener) {
+        super.removeListener(listener);
+    }
+
     /* Filtering functionality */
 
     @Override
index 04d08a6f68acdda3bdc742a4d184c1c70040983e..9cdafebbc7fbf20a5905c12fa1bd7326d1c70bed 100644 (file)
@@ -25,6 +25,10 @@ import org.junit.Test;
 
 import com.vaadin.data.Container.Filter;
 import com.vaadin.data.Container.Indexed;
+import com.vaadin.data.Container.ItemSetChangeEvent;
+import com.vaadin.data.Container.ItemSetChangeListener;
+import com.vaadin.data.Container.PropertySetChangeEvent;
+import com.vaadin.data.Container.PropertySetChangeListener;
 import com.vaadin.data.Item;
 import com.vaadin.data.util.filter.Compare;
 import com.vaadin.data.util.filter.UnsupportedFilterException;
@@ -35,6 +39,41 @@ public class GeneratedPropertyContainerTest {
     GeneratedPropertyContainer container;
     private static double MILES_CONVERSION = 0.6214d;
 
+    private class GeneratedPropertyListener implements
+            PropertySetChangeListener {
+
+        private int callCount = 0;
+
+        public int getCallCount() {
+            return callCount;
+        }
+
+        @Override
+        public void containerPropertySetChange(PropertySetChangeEvent event) {
+            ++callCount;
+            assertEquals(
+                    "Container for event was not GeneratedPropertyContainer",
+                    event.getContainer(), container);
+        }
+    }
+
+    private class GeneratedItemSetListener implements ItemSetChangeListener {
+
+        private int callCount = 0;
+
+        public int getCallCount() {
+            return callCount;
+        }
+
+        @Override
+        public void containerItemSetChange(ItemSetChangeEvent event) {
+            ++callCount;
+            assertEquals(
+                    "Container for event was not GeneratedPropertyContainer",
+                    event.getContainer(), container);
+        }
+    }
+
     @Before
     public void setUp() {
         container = new GeneratedPropertyContainer(createContainer());
@@ -183,6 +222,50 @@ public class GeneratedPropertyContainerTest {
         }
     }
 
+    @Test
+    public void testPropertySetChangeNotifier() {
+        GeneratedPropertyListener listener = new GeneratedPropertyListener();
+        GeneratedPropertyListener removedListener = new GeneratedPropertyListener();
+        container.addPropertySetChangeListener(listener);
+        container.addPropertySetChangeListener(removedListener);
+
+        container.addGeneratedProperty("foo",
+                new PropertyValueGenerator<String>() {
+
+                    @Override
+                    public String getValue(Item item, Object itemId,
+                            Object propertyId) {
+                        return "";
+                    }
+
+                    @Override
+                    public Class<String> getType() {
+                        return String.class;
+                    }
+                });
+        container.addContainerProperty("baz", String.class, "");
+        container.removePropertySetChangeListener(removedListener);
+        container.removeGeneratedProperty("foo");
+
+        assertEquals("Listener was not called correctly.", 3,
+                listener.getCallCount());
+        assertEquals("Removed listener was not called correctly.", 2,
+                removedListener.getCallCount());
+    }
+
+    @Test
+    public void testItemSetChangeNotifier() {
+        GeneratedItemSetListener listener = new GeneratedItemSetListener();
+        container.addItemSetChangeListener(listener);
+
+        container.sort(new Object[] { "foo" }, new boolean[] { true });
+        container.sort(new Object[] { "foo" }, new boolean[] { false });
+
+        assertEquals("Listener was not called correctly.", 2,
+                listener.getCallCount());
+
+    }
+
     private Indexed createContainer() {
         Indexed container = new IndexedContainer();
         container.addContainerProperty("foo", String.class, "foo");