summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <teemusa@vaadin.com>2016-08-18 10:54:46 +0300
committerVaadin Code Review <review@vaadin.com>2016-08-18 11:59:25 +0000
commit52dc5e4f1c1e11d2181385eb97b78641b8d5b32c (patch)
tree4fa93427724e70fbb91a5d81e18e2c21a437a6a1 /client
parent0081286c8d05c3751803181230092bd4b1e769f7 (diff)
downloadvaadin-framework-52dc5e4f1c1e11d2181385eb97b78641b8d5b32c.tar.gz
vaadin-framework-52dc5e4f1c1e11d2181385eb97b78641b8d5b32c.zip
Add DataCommunicator for data communication of Listings
Change-Id: I1f50823fdef105c3ba0463011574908a0cec7ad9
Diffstat (limited to 'client')
-rw-r--r--client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java18
-rw-r--r--client/src/main/java/com/vaadin/client/connectors/data/DataCommunicatorConnector.java136
-rw-r--r--client/src/main/java/com/vaadin/client/connectors/data/HasDataSource.java41
3 files changed, 194 insertions, 1 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java b/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java
index 92fb0f6720..06d3ecf97d 100644
--- a/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java
+++ b/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java
@@ -15,14 +15,30 @@
*/
package com.vaadin.client.connectors;
+import com.vaadin.client.connectors.data.HasDataSource;
+import com.vaadin.client.data.DataSource;
import com.vaadin.client.ui.AbstractComponentConnector;
import com.vaadin.ui.AbstractListing;
+import elemental.json.JsonObject;
+
/**
* Base connector class for {@link AbstractListing}.
*
* @since
*/
public abstract class AbstractListingConnector
- extends AbstractComponentConnector {
+ extends AbstractComponentConnector implements HasDataSource {
+
+ private DataSource<JsonObject> dataSource = null;
+
+ @Override
+ public void setDataSource(DataSource<JsonObject> dataSource) {
+ this.dataSource = dataSource;
+ }
+
+ @Override
+ public DataSource<JsonObject> getDataSource() {
+ return dataSource;
+ }
}
diff --git a/client/src/main/java/com/vaadin/client/connectors/data/DataCommunicatorConnector.java b/client/src/main/java/com/vaadin/client/connectors/data/DataCommunicatorConnector.java
new file mode 100644
index 0000000000..b2b3372764
--- /dev/null
+++ b/client/src/main/java/com/vaadin/client/connectors/data/DataCommunicatorConnector.java
@@ -0,0 +1,136 @@
+/*
+ * Copyright 2000-2016 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.connectors.data;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import com.vaadin.client.ServerConnector;
+import com.vaadin.client.data.AbstractRemoteDataSource;
+import com.vaadin.client.data.DataSource;
+import com.vaadin.client.extensions.AbstractExtensionConnector;
+import com.vaadin.server.data.DataCommunicator;
+import com.vaadin.shared.data.DataCommunicatorClientRpc;
+import com.vaadin.shared.data.DataCommunicatorConstants;
+import com.vaadin.shared.data.DataRequestRpc;
+import com.vaadin.shared.ui.Connect;
+
+import elemental.json.Json;
+import elemental.json.JsonArray;
+import elemental.json.JsonObject;
+
+/**
+ * A connector for DataCommunicator class.
+ *
+ * @since
+ */
+@Connect(DataCommunicator.class)
+public class DataCommunicatorConnector extends AbstractExtensionConnector {
+
+ /**
+ * Client-side {@link DataSource} implementation to be used with
+ * {@link DataCommunicator}.
+ */
+ public class VaadinDataSource extends AbstractRemoteDataSource<JsonObject> {
+
+ private Set<String> droppedKeys = new HashSet<String>();
+
+ protected VaadinDataSource() {
+ registerRpc(DataCommunicatorClientRpc.class,
+ new DataCommunicatorClientRpc() {
+
+ @Override
+ public void reset(int size) {
+ resetDataAndSize(size);
+ }
+
+ @Override
+ public void setData(int firstIndex, JsonArray data) {
+ ArrayList<JsonObject> rows = new ArrayList<JsonObject>(
+ data.length());
+ for (int i = 0; i < data.length(); i++) {
+ JsonObject rowObject = data.getObject(i);
+ rows.add(rowObject);
+ }
+
+ setRowData(firstIndex, rows);
+ }
+
+ @Override
+ public void updateData(JsonArray data) {
+ for (int i = 0; i < data.length(); ++i) {
+ updateRowData(data.getObject(i));
+ }
+ }
+ });
+ }
+
+ @Override
+ protected void requestRows(int firstRowIndex, int numberOfRows,
+ RequestRowsCallback<JsonObject> callback) {
+ getRpcProxy(DataRequestRpc.class).requestRows(firstRowIndex,
+ numberOfRows, 0, 0);
+
+ JsonArray dropped = Json.createArray();
+ int i = 0;
+ for (String key : droppedKeys) {
+ dropped.set(i++, key);
+ }
+ droppedKeys.clear();
+
+ getRpcProxy(DataRequestRpc.class).dropRows(dropped);
+ }
+
+ @Override
+ public String getRowKey(JsonObject row) {
+ return row.getString(DataCommunicatorConstants.KEY);
+ }
+
+ @Override
+ protected void onDropFromCache(int rowIndex, JsonObject removed) {
+ droppedKeys.add(getRowKey(removed));
+
+ super.onDropFromCache(rowIndex, removed);
+ }
+
+ /**
+ * Updates row data based on row key.
+ *
+ * @param row
+ * new row object
+ */
+ protected void updateRowData(JsonObject row) {
+ int index = indexOfKey(getRowKey(row));
+ if (index >= 0) {
+ setRowData(index, Collections.singletonList(row));
+ }
+ }
+ }
+
+ private DataSource<JsonObject> ds = new VaadinDataSource();
+
+ @Override
+ protected void extend(ServerConnector target) {
+ ServerConnector parent = getParent();
+ if (parent instanceof HasDataSource) {
+ ((HasDataSource) parent).setDataSource(ds);
+ } else {
+ assert false : "Parent not implementing HasDataSource";
+ }
+ }
+} \ No newline at end of file
diff --git a/client/src/main/java/com/vaadin/client/connectors/data/HasDataSource.java b/client/src/main/java/com/vaadin/client/connectors/data/HasDataSource.java
new file mode 100644
index 0000000000..79a70a1fe9
--- /dev/null
+++ b/client/src/main/java/com/vaadin/client/connectors/data/HasDataSource.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2000-2016 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.connectors.data;
+
+import com.vaadin.client.data.DataSource;
+
+import elemental.json.JsonObject;
+
+/**
+ * Marker interface for Connectors that have a {@link DataSource}.
+ */
+public interface HasDataSource {
+
+ /**
+ * Sets the data source for this Connector.
+ *
+ * @param dataSource
+ * new data source
+ */
+ void setDataSource(DataSource<JsonObject> dataSource);
+
+ /**
+ * Gets the current data source for this Connector.
+ *
+ * @return data source
+ */
+ DataSource<JsonObject> getDataSource();
+}