aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <teemusa@vaadin.com>2016-01-29 15:06:48 +0200
committerTeemu Suo-Anttila <teemusa@vaadin.com>2016-02-02 16:33:15 +0200
commitd366c3d35160d7c7b8f9cbdd374b0f760beac908 (patch)
tree9f19c124d8fb2dfa5d58c4067a209a3711365f82
parent3b420bdeab5575765345dfdd3994a74a8cc999b2 (diff)
downloadvaadin-framework-d366c3d35160d7c7b8f9cbdd374b0f760beac908.tar.gz
vaadin-framework-d366c3d35160d7c7b8f9cbdd374b0f760beac908.zip
Implement TypedDataGenerator support and clean up client-side code
Change-Id: I96080e2344ca0626940635dde77f2715c9d62c7b
-rw-r--r--client/src/com/vaadin/client/data/AbstractDataChangeHandler.java45
-rw-r--r--server/src/com/vaadin/server/communication/data/typed/DataProvider.java59
-rw-r--r--server/src/com/vaadin/server/communication/data/typed/TypedDataGenerator.java39
-rw-r--r--shared/src/com/vaadin/shared/data/DataProviderClientRpc.java5
-rw-r--r--uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderUI.java24
-rw-r--r--uitest/src/com/vaadin/tests/widgetset/client/dataprovider/DummyDataConnector.java20
6 files changed, 160 insertions, 32 deletions
diff --git a/client/src/com/vaadin/client/data/AbstractDataChangeHandler.java b/client/src/com/vaadin/client/data/AbstractDataChangeHandler.java
new file mode 100644
index 0000000000..b5a92e1307
--- /dev/null
+++ b/client/src/com/vaadin/client/data/AbstractDataChangeHandler.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.data;
+
+/**
+ * Empty {@link DataChangeHandler}. Use this when only a few of the methods are
+ * needed.
+ *
+ * @since
+ */
+public abstract class AbstractDataChangeHandler implements DataChangeHandler {
+
+ @Override
+ public void dataUpdated(int firstRowIndex, int numberOfRows) {
+ }
+
+ @Override
+ public void dataRemoved(int firstRowIndex, int numberOfRows) {
+ }
+
+ @Override
+ public void dataAdded(int firstRowIndex, int numberOfRows) {
+ }
+
+ @Override
+ public void dataAvailable(int firstRowIndex, int numberOfRows) {
+ }
+
+ @Override
+ public void resetDataAndSize(int estimatedNewDataSize) {
+ }
+}
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 62d39d50ec..60cf17ab57 100644
--- a/server/src/com/vaadin/server/communication/data/typed/DataProvider.java
+++ b/server/src/com/vaadin/server/communication/data/typed/DataProvider.java
@@ -16,6 +16,7 @@
package com.vaadin.server.communication.data.typed;
import java.util.Collection;
+import java.util.LinkedHashSet;
import com.vaadin.server.AbstractExtension;
import com.vaadin.shared.data.DataProviderClientRpc;
@@ -34,6 +35,28 @@ import elemental.json.JsonObject;
public class DataProvider<T> extends AbstractExtension {
/**
+ * Creates the appropriate type of DataProvider based on the type of
+ * Collection provided to the method.
+ * <p>
+ * <strong>Note:</strong> this method will also extend the given component
+ * with the newly created DataProvider. The user should <strong>not</strong>
+ * call the {@link #extend(com.vaadin.server.AbstractClientConnector)}
+ * method explicitly.
+ *
+ * @param data
+ * collection of data objects
+ * @param component
+ * component to extend with the data provider
+ * @return created data provider
+ */
+ public static <V> DataProvider<V> create(Collection<V> data,
+ AbstractComponent component) {
+ DataProvider<V> dataProvider = new DataProvider<V>(data);
+ dataProvider.extend(component);
+ return dataProvider;
+ }
+
+ /**
* Simple implementation of collection data provider communication. All data
* is sent by server automatically and no data is requested by client.
*/
@@ -55,20 +78,16 @@ public class DataProvider<T> extends AbstractExtension {
}
private Collection<T> data;
+ private Collection<TypedDataGenerator<T>> generators = new LinkedHashSet<TypedDataGenerator<T>>();
/**
- * Creates a new DataProvider, connecting it to given Collection and
- * Component
+ * Creates a new DataProvider with the given Collection.
*
* @param data
* collection of data to use
- * @param component
- * component to extend
*/
- public DataProvider(Collection<T> data, AbstractComponent component) {
+ protected DataProvider(Collection<T> data) {
this.data = data;
- extend(component);
-
registerRpc(createRpc());
}
@@ -88,6 +107,26 @@ public class DataProvider<T> extends AbstractExtension {
}
/**
+ * Adds a TypedDataGenerator to this DataProvider.
+ *
+ * @param generator
+ * typed data generator
+ */
+ public void addDataGenerator(TypedDataGenerator<T> generator) {
+ generators.add(generator);
+ }
+
+ /**
+ * Removes a TypedDataGenerator from this DataProvider.
+ *
+ * @param generator
+ * typed data generator
+ */
+ public void removeDataGenerator(TypedDataGenerator<T> generator) {
+ generators.add(generator);
+ }
+
+ /**
* Sends given row range to the client.
*
* @param firstIndex
@@ -117,9 +156,9 @@ public class DataProvider<T> extends AbstractExtension {
protected JsonObject getDataObject(T item) {
JsonObject dataObject = Json.createObject();
- dataObject.put("k", item.toString());
-
- // TODO: Add data generator stuff..
+ for (TypedDataGenerator<T> generator : generators) {
+ generator.generateData(item, dataObject);
+ }
return dataObject;
}
diff --git a/server/src/com/vaadin/server/communication/data/typed/TypedDataGenerator.java b/server/src/com/vaadin/server/communication/data/typed/TypedDataGenerator.java
new file mode 100644
index 0000000000..ed2e321df8
--- /dev/null
+++ b/server/src/com/vaadin/server/communication/data/typed/TypedDataGenerator.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.server.communication.data.typed;
+
+import java.io.Serializable;
+
+import elemental.json.JsonObject;
+
+/**
+ * Simple typed data generator for {@link DataProvider}.
+ *
+ * @since
+ */
+public interface TypedDataGenerator<T> extends Serializable {
+
+ /**
+ * Adds data for given object to JsonObject. This JsonObject will be sent to
+ * client-side DataSource.
+ *
+ * @param data
+ * data object
+ * @param jsonObject
+ * json object being sent to the client
+ */
+ void generateData(T data, JsonObject jsonObject);
+}
diff --git a/shared/src/com/vaadin/shared/data/DataProviderClientRpc.java b/shared/src/com/vaadin/shared/data/DataProviderClientRpc.java
index e734d4e818..94010e5aca 100644
--- a/shared/src/com/vaadin/shared/data/DataProviderClientRpc.java
+++ b/shared/src/com/vaadin/shared/data/DataProviderClientRpc.java
@@ -19,6 +19,11 @@ import com.vaadin.shared.communication.ClientRpc;
import elemental.json.JsonArray;
+/**
+ * RPC interface used by DataProvider to send data to the client-side.
+ *
+ * @since
+ */
public interface DataProviderClientRpc extends ClientRpc {
/**
diff --git a/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderUI.java b/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderUI.java
index 943c2b3b96..85ff2e7398 100644
--- a/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderUI.java
+++ b/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderUI.java
@@ -22,25 +22,41 @@ import java.util.Random;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.communication.data.typed.DataProvider;
+import com.vaadin.server.communication.data.typed.TypedDataGenerator;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.tests.fieldgroup.ComplexPerson;
import com.vaadin.tests.widgetset.TestingWidgetSet;
import com.vaadin.ui.AbstractComponent;
+import elemental.json.JsonObject;
+
@Widgetset(TestingWidgetSet.NAME)
public class DummyDataProviderUI extends AbstractTestUI {
- public static class DummyDataComponent<T> extends AbstractComponent {
+ public static class DummyDataComponent extends AbstractComponent {
+
+ private DataProvider<ComplexPerson> dataProvider;
+
+ public DummyDataComponent(Collection<ComplexPerson> data) {
+ dataProvider = DataProvider.create(data, this);
+ dataProvider
+ .addDataGenerator(new TypedDataGenerator<ComplexPerson>() {
- public DummyDataComponent(Collection<T> data) {
- new DataProvider<T>(data, this);
+ @Override
+ public void generateData(ComplexPerson data,
+ JsonObject dataObject) {
+ String name = data.getLastName() + ", "
+ + data.getFirstName();
+ dataObject.put("name", name);
+ }
+ });
}
}
@Override
protected void setup(VaadinRequest request) {
- addComponent(new DummyDataComponent<ComplexPerson>(getPersons(20)));
+ addComponent(new DummyDataComponent(getPersons(20)));
}
private Collection<ComplexPerson> getPersons(int count) {
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 eae615a962..4733026bd7 100644
--- a/uitest/src/com/vaadin/tests/widgetset/client/dataprovider/DummyDataConnector.java
+++ b/uitest/src/com/vaadin/tests/widgetset/client/dataprovider/DummyDataConnector.java
@@ -16,7 +16,7 @@
package com.vaadin.tests.widgetset.client.dataprovider;
import com.google.gwt.user.client.ui.FlowPanel;
-import com.vaadin.client.data.DataChangeHandler;
+import com.vaadin.client.data.AbstractDataChangeHandler;
import com.vaadin.client.data.DataSource;
import com.vaadin.client.data.HasDataSource;
import com.vaadin.client.ui.AbstractComponentConnector;
@@ -45,23 +45,7 @@ public class DummyDataConnector extends AbstractComponentConnector implements
@Override
public void setDataSource(DataSource<JsonObject> ds) {
dataSource = ds;
- dataSource.setDataChangeHandler(new DataChangeHandler() {
-
- @Override
- public void resetDataAndSize(int estimatedNewDataSize) {
- }
-
- @Override
- public void dataUpdated(int firstRowIndex, int numberOfRows) {
- }
-
- @Override
- public void dataRemoved(int firstRowIndex, int numberOfRows) {
- }
-
- @Override
- public void dataAvailable(int firstRowIndex, int numberOfRows) {
- }
+ dataSource.setDataChangeHandler(new AbstractDataChangeHandler() {
@Override
public void dataAdded(int firstRowIndex, int numberOfRows) {