diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-01-29 14:49:20 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-02-01 15:21:13 +0200 |
commit | 3b420bdeab5575765345dfdd3994a74a8cc999b2 (patch) | |
tree | 1408594ebb3587c9da93ba337c401a60666e5fb5 /server | |
parent | 4274350c31bb7bf4c3a8b5672bdac877f75cc069 (diff) | |
download | vaadin-framework-3b420bdeab5575765345dfdd3994a74a8cc999b2.tar.gz vaadin-framework-3b420bdeab5575765345dfdd3994a74a8cc999b2.zip |
Add data communication for DataProvider for Collections
After this patch, the object is passed to the client-side as a
JsonObject containing a key "k" with value from object.toString()
Change-Id: I3f83144a6c84dda9812739ff2f2cb74cb5577d5c
Diffstat (limited to 'server')
-rw-r--r-- | server/src/com/vaadin/server/communication/data/typed/DataProvider.java | 99 |
1 files changed, 87 insertions, 12 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 13755a5ce8..62d39d50ec 100644 --- a/server/src/com/vaadin/server/communication/data/typed/DataProvider.java +++ b/server/src/com/vaadin/server/communication/data/typed/DataProvider.java @@ -18,10 +18,13 @@ package com.vaadin.server.communication.data.typed; import java.util.Collection; import com.vaadin.server.AbstractExtension; +import com.vaadin.shared.data.DataProviderClientRpc; import com.vaadin.shared.data.DataRequestRpc; import com.vaadin.ui.AbstractComponent; +import elemental.json.Json; import elemental.json.JsonArray; +import elemental.json.JsonObject; /** * DataProvider for Collection "container". @@ -30,6 +33,27 @@ import elemental.json.JsonArray; */ public class DataProvider<T> extends AbstractExtension { + /** + * Simple implementation of collection data provider communication. All data + * is sent by server automatically and no data is requested by client. + */ + protected class DataRequestRpcImpl implements DataRequestRpc { + + @Override + public void requestRows(int firstRowIndex, int numberOfRows, + int firstCachedRowIndex, int cacheSize) { + throw new UnsupportedOperationException( + "Collection data provider sends all data from server." + + " It does not expect client to request anything."); + } + + @Override + public void dropRows(JsonArray rowKeys) { + // FIXME: What should I do with these? + } + + } + private Collection<T> data; /** @@ -45,18 +69,69 @@ public class DataProvider<T> extends AbstractExtension { this.data = data; extend(component); - registerRpc(new DataRequestRpc() { - @Override - public void requestRows(int firstRowIndex, int numberOfRows, - int firstCachedRowIndex, int cacheSize) { - throw new UnsupportedOperationException("Not implemented"); - } - - @Override - public void dropRows(JsonArray rowKeys) { - throw new UnsupportedOperationException("Not implemented"); - } - }); + registerRpc(createRpc()); + } + + /** + * Initially we need to push all the data to the client. + * + * TODO: The same is true for unknown size changes. + */ + @Override + public void beforeClientResponse(boolean initial) { + super.beforeClientResponse(initial); + + if (initial) { + getRpcProxy(DataProviderClientRpc.class).resetSize(data.size()); + pushRows(0, data); + } + } + + /** + * Sends given row range to the client. + * + * @param firstIndex + * first index + * @param items + * items to send as an iterable + */ + protected void pushRows(long firstIndex, Iterable<T> items) { + JsonArray data = Json.createArray(); + + int i = 0; + for (T item : items) { + data.set(i++, getDataObject(item)); + } + + getRpcProxy(DataProviderClientRpc.class).setData(firstIndex, data); + } + + /** + * Creates the JsonObject for given item. This method calls all data + * generators for this item. + * + * @param item + * item to be made into a json object + * @return json object representing the item + */ + protected JsonObject getDataObject(T item) { + JsonObject dataObject = Json.createObject(); + + dataObject.put("k", item.toString()); + + // TODO: Add data generator stuff.. + + return dataObject; + } + + /** + * Creates an instance of DataRequestRpc. By default it is + * {@link DataRequestRpcImpl}. + * + * @return data request rpc implementation + */ + protected DataRequestRpc createRpc() { + return new DataRequestRpcImpl(); } } |