summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorLeif Åstrand <leif@vaadin.com>2013-11-22 15:40:34 +0200
committerLeif Åstrand <leif@vaadin.com>2013-11-22 15:40:34 +0200
commitd54b02e31dad3e0b0a60e02750efb6bc268e3974 (patch)
tree09b3710c8d6d2296808807e25c2abf87d4b0e830 /server
parent4caa2f5b6e26ade52a4fba66a0a020b79f9008ea (diff)
downloadvaadin-framework-d54b02e31dad3e0b0a60e02750efb6bc268e3974.tar.gz
vaadin-framework-d54b02e31dad3e0b0a60e02750efb6bc268e3974.zip
Introduce initial data source support for Grid (#12878)
Change-Id: I2d1b2e4a797b2dac9ee97c832fcd40fb472edc08
Diffstat (limited to 'server')
-rw-r--r--server/src/com/vaadin/data/RpcDataProviderExtension.java101
-rw-r--r--server/src/com/vaadin/ui/components/grid/Grid.java9
2 files changed, 110 insertions, 0 deletions
diff --git a/server/src/com/vaadin/data/RpcDataProviderExtension.java b/server/src/com/vaadin/data/RpcDataProviderExtension.java
new file mode 100644
index 0000000000..48f03b98c0
--- /dev/null
+++ b/server/src/com/vaadin/data/RpcDataProviderExtension.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2000-2013 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.data;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import com.vaadin.data.Container.Indexed;
+import com.vaadin.server.AbstractExtension;
+import com.vaadin.shared.data.DataProviderRpc;
+import com.vaadin.shared.data.DataProviderState;
+import com.vaadin.shared.data.DataRequestRpc;
+import com.vaadin.ui.components.grid.Grid;
+
+/**
+ * Provides Vaadin server-side container data source to a
+ * {@link com.vaadin.client.ui.grid.GridConnector}. This is currently
+ * implemented as an Extension hardcoded to support a specific connector type.
+ * This will be changed once framework support for something more flexible has
+ * been implemented.
+ *
+ * @since 7.2
+ * @author Vaadin Ltd
+ */
+public class RpcDataProviderExtension extends AbstractExtension {
+
+ private final Indexed container;
+
+ /**
+ * Creates a new data provider using the given container.
+ *
+ * @param container
+ * the container to make available
+ */
+ public RpcDataProviderExtension(Indexed container) {
+ this.container = container;
+
+ // TODO support for reacting to events from the container added later
+
+ registerRpc(new DataRequestRpc() {
+ @Override
+ public void requestRows(int firstRow, int numberOfRows) {
+ pushRows(firstRow, numberOfRows);
+ }
+ });
+
+ getState().containerSize = container.size();
+ }
+
+ private void pushRows(int firstRow, int numberOfRows) {
+ List<?> itemIds = container.getItemIds(firstRow, numberOfRows);
+ Collection<?> propertyIds = container.getContainerPropertyIds();
+ List<String[]> rows = new ArrayList<String[]>(itemIds.size());
+ for (Object itemId : itemIds) {
+ Item item = container.getItem(itemId);
+ String[] row = new String[propertyIds.size()];
+
+ int i = 0;
+ for (Object propertyId : propertyIds) {
+ Object value = item.getItemProperty(propertyId).getValue();
+ String stringValue = String.valueOf(value);
+ row[i++] = stringValue;
+ }
+
+ rows.add(row);
+ }
+
+ getRpcProxy(DataProviderRpc.class).setRowData(firstRow, rows);
+ }
+
+ @Override
+ protected DataProviderState getState() {
+ return (DataProviderState) super.getState();
+ }
+
+ /**
+ * Makes the data source available to the given {@link Grid} component.
+ *
+ * @param component
+ * the remote data grid component to extend
+ */
+ public void extend(Grid component) {
+ super.extend(component);
+ }
+
+}
diff --git a/server/src/com/vaadin/ui/components/grid/Grid.java b/server/src/com/vaadin/ui/components/grid/Grid.java
index 2b19043d93..79cc05e1a0 100644
--- a/server/src/com/vaadin/ui/components/grid/Grid.java
+++ b/server/src/com/vaadin/ui/components/grid/Grid.java
@@ -29,6 +29,7 @@ import com.vaadin.data.Container;
import com.vaadin.data.Container.PropertySetChangeEvent;
import com.vaadin.data.Container.PropertySetChangeListener;
import com.vaadin.data.Container.PropertySetChangeNotifier;
+import com.vaadin.data.RpcDataProviderExtension;
import com.vaadin.server.KeyMapper;
import com.vaadin.shared.ui.grid.ColumnGroupRowState;
import com.vaadin.shared.ui.grid.GridColumnState;
@@ -107,6 +108,8 @@ public class Grid extends AbstractComponent {
}
};
+ private RpcDataProviderExtension datasourceExtension;
+
/**
* Creates a new Grid using the given datasource.
*
@@ -140,7 +143,13 @@ public class Grid extends AbstractComponent {
.removePropertySetChangeListener(propertyListener);
}
+ if (datasourceExtension != null) {
+ removeExtension(datasourceExtension);
+ }
+
datasource = container;
+ datasourceExtension = new RpcDataProviderExtension(container);
+ datasourceExtension.extend(this);
// Listen to changes in properties and remove columns if needed
if (datasource instanceof PropertySetChangeNotifier) {