summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <teemusa@vaadin.com>2016-08-22 16:39:50 +0300
committerVaadin Code Review <review@vaadin.com>2016-08-24 12:11:31 +0000
commit87e0a08b6059add166b46b8b4379791a40ff0ede (patch)
tree7dbe56876a7befbbe9985343b165ee91f65ed4c4 /client
parentfa2a40b38e8f06632f9b89c9acea1ed0d40c3410 (diff)
downloadvaadin-framework-87e0a08b6059add166b46b8b4379791a40ff0ede.tar.gz
vaadin-framework-87e0a08b6059add166b46b8b4379791a40ff0ede.zip
Add a typed version of the Grid component
Change-Id: I00cbe80ac72787de0c4cc88e1c223badc2c4ae89
Diffstat (limited to 'client')
-rw-r--r--client/src/main/java/com/vaadin/client/connectors/grid/ColumnConnector.java85
-rw-r--r--client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java136
2 files changed, 221 insertions, 0 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/ColumnConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/ColumnConnector.java
new file mode 100644
index 0000000000..4b61b62dc7
--- /dev/null
+++ b/client/src/main/java/com/vaadin/client/connectors/grid/ColumnConnector.java
@@ -0,0 +1,85 @@
+/*
+ * 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.grid;
+
+import com.vaadin.client.ServerConnector;
+import com.vaadin.client.annotations.OnStateChange;
+import com.vaadin.client.extensions.AbstractExtensionConnector;
+import com.vaadin.client.widgets.Grid.Column;
+import com.vaadin.shared.data.DataCommunicatorConstants;
+import com.vaadin.shared.ui.Connect;
+import com.vaadin.shared.ui.grid.ColumnState;
+
+import elemental.json.JsonObject;
+import elemental.json.JsonValue;
+
+/**
+ * A connector class for columns of the Grid component.
+ *
+ * @author Vaadin Ltd
+ * @since
+ */
+@Connect(com.vaadin.ui.Grid.Column.class)
+public class ColumnConnector extends AbstractExtensionConnector {
+
+ private Column<JsonValue, JsonObject> column;
+
+ /* This parent is needed because it's no longer available in onUnregister */
+ private GridConnector parent;
+
+ @Override
+ protected void extend(ServerConnector target) {
+ parent = getParent();
+ column = new Column<JsonValue, JsonObject>() {
+
+ @Override
+ public JsonValue getValue(JsonObject row) {
+ return row.getObject(DataCommunicatorConstants.DATA)
+ .get(getState().id);
+ }
+ };
+ getParent().addColumn(column, getState().id);
+ }
+
+ @OnStateChange("caption")
+ void updateCaption() {
+ column.setHeaderCaption(getState().caption);
+ }
+
+ @OnStateChange("sortable")
+ void updateSortable() {
+ column.setSortable(getState().sortable);
+ }
+
+ @Override
+ public void onUnregister() {
+ super.onUnregister();
+
+ parent.removeColumn(column);
+ column = null;
+ }
+
+ @Override
+ public GridConnector getParent() {
+ return (GridConnector) super.getParent();
+ }
+
+ @Override
+ public ColumnState getState() {
+ return (ColumnState) super.getState();
+ }
+
+}
diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java
new file mode 100644
index 0000000000..6c809f731c
--- /dev/null
+++ b/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.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.grid;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.vaadin.client.DeferredWorker;
+import com.vaadin.client.connectors.AbstractListingConnector;
+import com.vaadin.client.data.DataSource;
+import com.vaadin.client.ui.SimpleManagedLayout;
+import com.vaadin.client.widget.grid.selection.ClickSelectHandler;
+import com.vaadin.client.widget.grid.sort.SortEvent;
+import com.vaadin.client.widget.grid.sort.SortOrder;
+import com.vaadin.client.widgets.Grid;
+import com.vaadin.client.widgets.Grid.Column;
+import com.vaadin.shared.data.sort.SortDirection;
+import com.vaadin.shared.ui.Connect;
+import com.vaadin.shared.ui.grid.GridServerRpc;
+
+import elemental.json.JsonObject;
+
+/**
+ * A connector class for the typed Grid component.
+ *
+ * @author Vaadin Ltd
+ * @since
+ */
+@Connect(com.vaadin.ui.Grid.class)
+public class GridConnector extends AbstractListingConnector
+ implements SimpleManagedLayout, DeferredWorker {
+ /* Map to keep track of all added columns */
+ private Map<Column<?, JsonObject>, String> columnToIdMap = new HashMap<>();
+
+ @Override
+ public Grid<JsonObject> getWidget() {
+ return (Grid<JsonObject>) super.getWidget();
+ }
+
+ @Override
+ protected void init() {
+ super.init();
+
+ new ClickSelectHandler<JsonObject>(getWidget());
+ getWidget().addSortHandler(this::handleSortEvent);
+
+ layout();
+ }
+
+ @Override
+ public void setDataSource(DataSource<JsonObject> dataSource) {
+ getWidget().setDataSource(dataSource);
+ }
+
+ /**
+ * Adds a column to the Grid widget. For each column a communication id
+ * stored for client to server communication.
+ *
+ * @param column
+ * column to add
+ * @param id
+ * communication id
+ */
+ public void addColumn(Column<?, JsonObject> column, String id) {
+ assert !columnToIdMap.containsKey(column) && !columnToIdMap
+ .containsValue(id) : "Column with given id already exists.";
+ getWidget().addColumn(column);
+ columnToIdMap.put(column, id);
+ }
+
+ /**
+ * Removes a column from Grid widget. This method also removes communication
+ * id mapping for the column.
+ *
+ * @param column
+ * column to remove
+ */
+ public void removeColumn(Column<?, JsonObject> column) {
+ assert columnToIdMap
+ .containsKey(column) : "Given Column does not exist.";
+ getWidget().removeColumn(column);
+ columnToIdMap.remove(column);
+ }
+
+ @Override
+ public void onUnregister() {
+ super.onUnregister();
+
+ columnToIdMap.clear();
+ }
+
+ @Override
+ public boolean isWorkPending() {
+ return getWidget().isWorkPending();
+ }
+
+ @Override
+ public void layout() {
+ getWidget().onResize();
+ }
+
+ /**
+ * Sends sort information from an event to the server-side of the Grid.
+ *
+ * @param event
+ * the sort event
+ */
+ private void handleSortEvent(SortEvent<JsonObject> event) {
+ List<String> columnIds = new ArrayList<>();
+ List<SortDirection> sortDirections = new ArrayList<>();
+ for (SortOrder so : event.getOrder()) {
+ if (columnToIdMap.containsKey(so.getColumn())) {
+ columnIds.add(columnToIdMap.get(so.getColumn()));
+ sortDirections.add(so.getDirection());
+ }
+ }
+ getRpcProxy(GridServerRpc.class).sort(columnIds.toArray(new String[0]),
+ sortDirections.toArray(new SortDirection[0]),
+ event.isUserOriginated());
+ }
+}