From d093358925707616b3e683e0c851e211cfc2087d Mon Sep 17 00:00:00 2001 From: Teemu Suo-Anttila Date: Fri, 5 Feb 2016 17:40:39 +0200 Subject: Refactor DataSource from SimpleDataProvider to super class Change-Id: I4fcb1dcb2a7d4369e68bc2abc0c1168ad21c8201 --- .../communication/data/typed/DataProvider.java | 15 ++++-- .../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 extends AbstractExtension { protected ActiveDataHandler handler = new ActiveDataHandler(); protected DataProviderClientRpc rpc; - // TODO: Add a "BackEnd" API - // protected BackEnd data; + protected DataSource dataSource; - protected DataProvider() { + protected DataProvider(DataSource dataSource) { addDataGenerator(handler); + this.dataSource = dataSource; rpc = getRpcProxy(DataProviderClientRpc.class); registerRpc(createRpc()); + this.dataSource.addDataChangeHandler(createDataChangeHandler()); } /** @@ -277,4 +279,11 @@ public abstract class DataProvider 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 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 extends DataProvider { } // Use the whole data as the ones sent to the client. - handler.cleanUp(data); + handler.cleanUp(dataSource); } } private boolean reset = false; private final Set updatedData = new HashSet(); - private DataSource data; // TODO: Allow customizing the used key mapper private DataKeyMapper keyMapper = new KeyMapper(); @@ -76,29 +75,7 @@ public class SimpleDataProvider extends DataProvider { * collection of data to use */ protected SimpleDataProvider(DataSource data) { - this.data = data; - this.data.addDataChangeHandler(new DataChangeHandler() { - - @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 extends DataProvider { 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 extends DataProvider { return new SimpleDataRequestRpc(); } + @Override + protected DataChangeHandler createDataChangeHandler() { + return new DataChangeHandler() { + + @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); + } + }; + } } -- cgit v1.2.3