diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-02-05 17:40:39 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-02-05 18:37:27 +0200 |
commit | d093358925707616b3e683e0c851e211cfc2087d (patch) | |
tree | 6ed8ccecd5f6ce84d6058ddaa1bc34ab637354e8 | |
parent | aedebc2b57bbf1311af0f97010be8aeeeb22aa19 (diff) | |
download | vaadin-framework-d093358925707616b3e683e0c851e211cfc2087d.tar.gz vaadin-framework-d093358925707616b3e683e0c851e211cfc2087d.zip |
Refactor DataSource from SimpleDataProvider to super class
Change-Id: I4fcb1dcb2a7d4369e68bc2abc0c1168ad21c8201
-rw-r--r-- | server/src/com/vaadin/server/communication/data/typed/DataProvider.java | 15 | ||||
-rw-r--r-- | server/src/com/vaadin/server/communication/data/typed/SimpleDataProvider.java | 57 |
2 files changed, 42 insertions, 30 deletions
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 40cb147e9e..7cba66aa64 100644 --- a/server/src/com/vaadin/server/communication/data/typed/DataProvider.java +++ b/server/src/com/vaadin/server/communication/data/typed/DataProvider.java @@ -22,6 +22,7 @@ import java.util.LinkedHashSet; import java.util.Set; import com.vaadin.server.AbstractExtension; +import com.vaadin.server.communication.data.typed.DataSource.DataChangeHandler; import com.vaadin.shared.data.DataProviderClientRpc; import com.vaadin.shared.data.DataProviderConstants; import com.vaadin.shared.data.DataRequestRpc; @@ -176,13 +177,14 @@ public abstract class DataProvider<T> extends AbstractExtension { protected ActiveDataHandler handler = new ActiveDataHandler(); protected DataProviderClientRpc rpc; - // TODO: Add a "BackEnd" API - // protected BackEnd data; + protected DataSource<T> dataSource; - protected DataProvider() { + protected DataProvider(DataSource<T> dataSource) { addDataGenerator(handler); + this.dataSource = dataSource; rpc = getRpcProxy(DataProviderClientRpc.class); registerRpc(createRpc()); + this.dataSource.addDataChangeHandler(createDataChangeHandler()); } /** @@ -277,4 +279,11 @@ public abstract class DataProvider<T> extends AbstractExtension { * @return data request rpc implementation */ protected abstract DataRequestRpc createRpc(); + + /** + * Creates a {@link DataChangeHandler} to use with the {@link DataSource}. + * + * @return data change handler + */ + protected abstract DataChangeHandler<T> createDataChangeHandler(); } diff --git a/server/src/com/vaadin/server/communication/data/typed/SimpleDataProvider.java b/server/src/com/vaadin/server/communication/data/typed/SimpleDataProvider.java index 91a7772ae3..a6a0e193db 100644 --- a/server/src/com/vaadin/server/communication/data/typed/SimpleDataProvider.java +++ b/server/src/com/vaadin/server/communication/data/typed/SimpleDataProvider.java @@ -58,14 +58,13 @@ public class SimpleDataProvider<T> extends DataProvider<T> { } // Use the whole data as the ones sent to the client. - handler.cleanUp(data); + handler.cleanUp(dataSource); } } private boolean reset = false; private final Set<T> updatedData = new HashSet<T>(); - private DataSource<T> data; // TODO: Allow customizing the used key mapper private DataKeyMapper<T> keyMapper = new KeyMapper<T>(); @@ -76,29 +75,7 @@ public class SimpleDataProvider<T> extends DataProvider<T> { * collection of data to use */ protected SimpleDataProvider(DataSource<T> data) { - this.data = data; - this.data.addDataChangeHandler(new DataChangeHandler<T>() { - - @Override - public void onDataChange() { - reset(); - } - - @Override - public void onDataAdd(T data) { - add(data); - } - - @Override - public void onDataRemove(T data) { - remove(data); - } - - @Override - public void onDataUpdate(T data) { - refresh(data); - } - }); + super(data); } /** @@ -110,8 +87,9 @@ public class SimpleDataProvider<T> extends DataProvider<T> { super.beforeClientResponse(initial); if (initial || reset) { - getRpcProxy(DataProviderClientRpc.class).resetSize(data.size()); - pushData(0, data); + getRpcProxy(DataProviderClientRpc.class).resetSize( + dataSource.size()); + pushData(0, dataSource); } else if (!updatedData.isEmpty()) { JsonArray dataArray = Json.createArray(); int i = 0; @@ -185,4 +163,29 @@ public class SimpleDataProvider<T> extends DataProvider<T> { return new SimpleDataRequestRpc(); } + @Override + protected DataChangeHandler<T> createDataChangeHandler() { + return new DataChangeHandler<T>() { + + @Override + public void onDataChange() { + reset(); + } + + @Override + public void onDataAdd(T data) { + add(data); + } + + @Override + public void onDataRemove(T data) { + remove(data); + } + + @Override + public void onDataUpdate(T data) { + refresh(data); + } + }; + } } |