diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2014-09-19 12:30:57 +0300 |
---|---|---|
committer | Johannes Dahlström <johannesd@vaadin.com> | 2014-09-19 16:40:24 +0300 |
commit | 48e208c78e1ca2227d422985e4084a8b1ed7304f (patch) | |
tree | 63e283d543bf6bf8037ce31c79b77711f37aae80 | |
parent | bb286a0c177551269a9d1eccc02978beeb71a4ee (diff) | |
download | vaadin-framework-48e208c78e1ca2227d422985e4084a8b1ed7304f.tar.gz vaadin-framework-48e208c78e1ca2227d422985e4084a8b1ed7304f.zip |
Add Property and Item change notifiers for generated properties (#13334)
Change-Id: I35a212b7f32f7c75333ee394f6dc102802d4d46e
-rw-r--r-- | server/src/com/vaadin/data/util/GeneratedPropertyContainer.java | 80 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/data/util/GeneratedPropertyContainerTest.java | 83 |
2 files changed, 159 insertions, 4 deletions
diff --git a/server/src/com/vaadin/data/util/GeneratedPropertyContainer.java b/server/src/com/vaadin/data/util/GeneratedPropertyContainer.java index 979f622300..4dd190908e 100644 --- a/server/src/com/vaadin/data/util/GeneratedPropertyContainer.java +++ b/server/src/com/vaadin/data/util/GeneratedPropertyContainer.java @@ -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 diff --git a/server/tests/src/com/vaadin/data/util/GeneratedPropertyContainerTest.java b/server/tests/src/com/vaadin/data/util/GeneratedPropertyContainerTest.java index 04d08a6f68..9cdafebbc7 100644 --- a/server/tests/src/com/vaadin/data/util/GeneratedPropertyContainerTest.java +++ b/server/tests/src/com/vaadin/data/util/GeneratedPropertyContainerTest.java @@ -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"); |