diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-01-29 15:51:33 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-02-02 18:22:57 +0200 |
commit | 918ad6f8bf40fda89c60b8b680e02d5c79efdffd (patch) | |
tree | b3ced303cfed47f3188e1d72901887d43ea6969b | |
parent | d366c3d35160d7c7b8f9cbdd374b0f760beac908 (diff) | |
download | vaadin-framework-918ad6f8bf40fda89c60b8b680e02d5c79efdffd.tar.gz vaadin-framework-918ad6f8bf40fda89c60b8b680e02d5c79efdffd.zip |
Add simple data add/remove support to DataProvider
Removal is currently based on the content of the object. Multiple
objects with same content will cause problems. This should be fixed by
adding a simple key mapping for objects.
Change-Id: Ie6fd1c6bcb7e8eaa73469f8f794f5365b1590fe2
5 files changed, 109 insertions, 7 deletions
diff --git a/client/src/com/vaadin/client/connectors/data/typed/DataSourceConnector.java b/client/src/com/vaadin/client/connectors/data/typed/DataSourceConnector.java index bfd0a30ff1..a2fc1db8e6 100644 --- a/client/src/com/vaadin/client/connectors/data/typed/DataSourceConnector.java +++ b/client/src/com/vaadin/client/connectors/data/typed/DataSourceConnector.java @@ -61,6 +61,22 @@ public class DataSourceConnector extends AbstractExtensionConnector { } } } + + @Override + public void add(JsonObject dataObject) { + ds.asList().add(dataObject); + } + + @Override + public void drop(JsonObject dataObject) { + List<JsonObject> l = ds.asList(); + for (int i = 0; i < l.size(); ++i) { + if (l.get(i).toJson().equals(dataObject.toJson())) { + l.remove(i); + return; + } + } + } }); ServerConnector parent = getParent(); diff --git a/server/src/com/vaadin/server/communication/data/typed/DataProvider.java b/server/src/com/vaadin/server/communication/data/typed/DataProvider.java index 60cf17ab57..41cdb95dba 100644 --- a/server/src/com/vaadin/server/communication/data/typed/DataProvider.java +++ b/server/src/com/vaadin/server/communication/data/typed/DataProvider.java @@ -79,6 +79,7 @@ public class DataProvider<T> extends AbstractExtension { private Collection<T> data; private Collection<TypedDataGenerator<T>> generators = new LinkedHashSet<TypedDataGenerator<T>>(); + private DataProviderClientRpc rpc; /** * Creates a new DataProvider with the given Collection. @@ -88,6 +89,8 @@ public class DataProvider<T> extends AbstractExtension { */ protected DataProvider(Collection<T> data) { this.data = data; + + rpc = getRpcProxy(DataProviderClientRpc.class); registerRpc(createRpc()); } @@ -142,7 +145,7 @@ public class DataProvider<T> extends AbstractExtension { data.set(i++, getDataObject(item)); } - getRpcProxy(DataProviderClientRpc.class).setData(firstIndex, data); + rpc.setData(firstIndex, data); } /** @@ -173,4 +176,25 @@ public class DataProvider<T> extends AbstractExtension { return new DataRequestRpcImpl(); } + /** + * Informs the DataProvider that an item has been added. It is assumed to be + * the last item in the collection. + * + * @param item + * item added to collection + */ + public void add(T item) { + rpc.add(getDataObject(item)); + } + + /** + * Informs the DataProvider that an item has been removed. + * + * @param item + * item removed from collection + */ + public void remove(T item) { + rpc.drop(getDataObject(item)); + } + } diff --git a/shared/src/com/vaadin/shared/data/DataProviderClientRpc.java b/shared/src/com/vaadin/shared/data/DataProviderClientRpc.java index 94010e5aca..de23e1c3ed 100644 --- a/shared/src/com/vaadin/shared/data/DataProviderClientRpc.java +++ b/shared/src/com/vaadin/shared/data/DataProviderClientRpc.java @@ -18,6 +18,7 @@ package com.vaadin.shared.data; import com.vaadin.shared.communication.ClientRpc; import elemental.json.JsonArray; +import elemental.json.JsonObject; /** * RPC interface used by DataProvider to send data to the client-side. @@ -48,4 +49,21 @@ public interface DataProviderClientRpc extends ClientRpc { */ void setData(long firstIndex, JsonArray data); + /** + * Adds a new data object to the client-side DataSource. The new data object + * is added at the end of the data source. + * + * @param dataObject + * single added data object + */ + void add(JsonObject dataObject); + + /** + * Removes data from the client-side DataSource. + * + * @param dataObject + * single removed data object + */ + void drop(JsonObject dataObject); + } diff --git a/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderUI.java b/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderUI.java index 85ff2e7398..62aa8c2241 100644 --- a/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderUI.java +++ b/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderUI.java @@ -15,8 +15,9 @@ */ package com.vaadin.tests.dataprovider; +import java.util.ArrayList; import java.util.Collection; -import java.util.LinkedHashSet; +import java.util.List; import java.util.Random; import com.vaadin.annotations.Widgetset; @@ -27,6 +28,10 @@ import com.vaadin.tests.components.AbstractTestUI; import com.vaadin.tests.fieldgroup.ComplexPerson; import com.vaadin.tests.widgetset.TestingWidgetSet; import com.vaadin.ui.AbstractComponent; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Button.ClickListener; +import com.vaadin.ui.HorizontalLayout; import elemental.json.JsonObject; @@ -36,8 +41,10 @@ public class DummyDataProviderUI extends AbstractTestUI { public static class DummyDataComponent extends AbstractComponent { private DataProvider<ComplexPerson> dataProvider; + private Collection<ComplexPerson> data; public DummyDataComponent(Collection<ComplexPerson> data) { + this.data = data; dataProvider = DataProvider.create(data, this); dataProvider .addDataGenerator(new TypedDataGenerator<ComplexPerson>() { @@ -52,16 +59,47 @@ public class DummyDataProviderUI extends AbstractTestUI { }); } + void addItem(ComplexPerson person) { + if (data.add(person)) { + dataProvider.add(person); + } + } + + void removeItem(ComplexPerson person) { + if (data.remove(person)) { + dataProvider.remove(person); + } + } } + private Random r = new Random(1337); + private List<ComplexPerson> persons = getPersons(20); + private DummyDataComponent dummy; + @Override protected void setup(VaadinRequest request) { - addComponent(new DummyDataComponent(getPersons(20))); + dummy = new DummyDataComponent(persons); + + Button remove = new Button("Remove third", new ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + dummy.removeItem(persons.get(2)); + } + }); + Button add = new Button("Add new", new ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + dummy.addItem(ComplexPerson.create(r)); + } + }); + addComponent(new HorizontalLayout(add, remove)); + addComponent(dummy); } - private Collection<ComplexPerson> getPersons(int count) { - Random r = new Random(1337); - Collection<ComplexPerson> c = new LinkedHashSet<ComplexPerson>(); + private List<ComplexPerson> getPersons(int count) { + List<ComplexPerson> c = new ArrayList<ComplexPerson>(); for (int i = 0; i < count; ++i) { c.add(ComplexPerson.create(r)); } diff --git a/uitest/src/com/vaadin/tests/widgetset/client/dataprovider/DummyDataConnector.java b/uitest/src/com/vaadin/tests/widgetset/client/dataprovider/DummyDataConnector.java index 4733026bd7..c64f30ab57 100644 --- a/uitest/src/com/vaadin/tests/widgetset/client/dataprovider/DummyDataConnector.java +++ b/uitest/src/com/vaadin/tests/widgetset/client/dataprovider/DummyDataConnector.java @@ -48,6 +48,13 @@ public class DummyDataConnector extends AbstractComponentConnector implements dataSource.setDataChangeHandler(new AbstractDataChangeHandler() { @Override + public void dataRemoved(int firstRowIndex, int numberOfRows) { + for (int i = 0; i < numberOfRows; ++i) { + getWidget().remove(firstRowIndex); + } + } + + @Override public void dataAdded(int firstRowIndex, int numberOfRows) { for (int i = 0; i < numberOfRows; ++i) { getWidget().add( @@ -57,5 +64,4 @@ public class DummyDataConnector extends AbstractComponentConnector implements } }); } - } |