From d8562821deedbbce7985a8b02cf14ee604934ac3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Johannes=20Dahlstr=C3=B6m?= Date: Mon, 5 Sep 2016 19:34:19 +0300 Subject: [PATCH] Parameterize listing and selection connectors by selection model type Change-Id: I9c31582242b0b37b8a732e41bc73c59881dcf68b --- .../connectors/AbstractListingConnector.java | 57 +++++++++++++++++-- .../client/connectors/grid/GridConnector.java | 4 +- .../selection/AbstractSelectionConnector.java | 23 +++++--- .../selection/SingleSelectionConnector.java | 7 ++- .../client/data/DummyComponentConnector.java | 10 ++-- 5 files changed, 77 insertions(+), 24 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 938df199da..6593b5a0be 100644 --- a/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java @@ -18,22 +18,29 @@ 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.shared.data.DataCommunicatorConstants; import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.ui.AbstractListing; import elemental.json.JsonObject; +import elemental.json.JsonValue; /** - * Base connector class for {@link AbstractListing}. + * A base connector class for {@link AbstractListing}. * - * @since + * @author Vaadin Ltd. + * + * @param + * the client-side selection model type + * + * @since 8.0 */ -public abstract class AbstractListingConnector +public abstract class AbstractListingConnector> extends AbstractComponentConnector implements HasDataSource { private DataSource dataSource = null; - private SelectionModel selectionModel = null; + private SELECTIONMODEL selectionModel = null; @Override public void setDataSource(DataSource dataSource) { @@ -51,11 +58,49 @@ public abstract class AbstractListingConnector * @param selectionModel * the selection model or null to disable */ - public void setSelectionModel(SelectionModel selectionModel) { + public void setSelectionModel(SELECTIONMODEL selectionModel) { this.selectionModel = selectionModel; } - public SelectionModel getSelectionModel() { + /** + * Returns the selection model instance used. + * + * @return the selection model + */ + public SELECTIONMODEL getSelectionModel() { return selectionModel; } + + /** + * Returns the key of the given data row. + * + * @param row + * the row + * @return the row key + */ + protected static String getRowKey(JsonObject row) { + return row.getString(DataCommunicatorConstants.KEY); + } + + /** + * Returns the data of the given data row. + * + * @param row + * the row + * @return the row data + */ + protected static JsonValue getRowData(JsonObject row) { + return row.get(DataCommunicatorConstants.DATA); + } + + /** + * Returns whether the given row is selected. + * + * @param row + * the row + * @return {@code true} if the row is selected, {@code false} otherwise + */ + protected boolean isRowSelected(JsonObject row) { + return row.hasKey(DataCommunicatorConstants.SELECTED); + } } 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 index 31f6cc38ab..91adcc14ac 100644 --- a/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java @@ -52,7 +52,8 @@ import elemental.json.JsonObject; * @since */ @Connect(com.vaadin.ui.Grid.class) -public class GridConnector extends AbstractListingConnector +public class GridConnector + extends AbstractListingConnector> implements HasComponentsConnector, SimpleManagedLayout, DeferredWorker { /* Map to keep track of all added columns */ @@ -63,6 +64,7 @@ public class GridConnector extends AbstractListingConnector private ClickSelectHandler clickSelectHandler; @Override + @SuppressWarnings("unchecked") public Grid getWidget() { return (Grid) super.getWidget(); } diff --git a/client/src/main/java/com/vaadin/client/connectors/selection/AbstractSelectionConnector.java b/client/src/main/java/com/vaadin/client/connectors/selection/AbstractSelectionConnector.java index 7edc944a22..9d4604993d 100644 --- a/client/src/main/java/com/vaadin/client/connectors/selection/AbstractSelectionConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/selection/AbstractSelectionConnector.java @@ -27,15 +27,18 @@ import elemental.json.JsonObject; * The client-side connector for selection extensions. * * @author Vaadin Ltd. - * - * @since + * + * @param + * the supported client-side selection model + * @since 8.0 */ -public abstract class AbstractSelectionConnector +public abstract class AbstractSelectionConnector> extends AbstractExtensionConnector { - private SelectionModel model = null; + private SELECTIONMODEL model = null; @Override + @SuppressWarnings("unchecked") protected void extend(ServerConnector target) { if (!(target instanceof AbstractListingConnector)) { throw new IllegalArgumentException( @@ -43,7 +46,8 @@ public abstract class AbstractSelectionConnector + AbstractListingConnector.class.getSimpleName()); } model = createSelectionModel(); - ((AbstractListingConnector) target).setSelectionModel(model); + ((AbstractListingConnector) target) + .setSelectionModel(model); } /** @@ -51,11 +55,12 @@ public abstract class AbstractSelectionConnector * * @return created selection model */ - protected abstract SelectionModel createSelectionModel(); + protected abstract SELECTIONMODEL createSelectionModel(); @Override - public AbstractListingConnector getParent() { - return (AbstractListingConnector) super.getParent(); + @SuppressWarnings("unchecked") + public AbstractListingConnector getParent() { + return (AbstractListingConnector) super.getParent(); } /** @@ -63,7 +68,7 @@ public abstract class AbstractSelectionConnector * * @return the selection model in use */ - protected SelectionModel getSelectionModel() { + protected SELECTIONMODEL getSelectionModel() { return model; } diff --git a/client/src/main/java/com/vaadin/client/connectors/selection/SingleSelectionConnector.java b/client/src/main/java/com/vaadin/client/connectors/selection/SingleSelectionConnector.java index 912b20310e..875a70412c 100644 --- a/client/src/main/java/com/vaadin/client/connectors/selection/SingleSelectionConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/selection/SingleSelectionConnector.java @@ -31,7 +31,8 @@ import elemental.json.JsonObject; * @author Vaadin Ltd. */ @Connect(com.vaadin.data.selection.SingleSelection.class) -public class SingleSelectionConnector extends AbstractSelectionConnector { +public class SingleSelectionConnector extends + AbstractSelectionConnector> { private static class SingleSelection implements SelectionModel.Single { @@ -68,7 +69,7 @@ public class SingleSelectionConnector extends AbstractSelectionConnector { } } - private AbstractListingConnector parent; + private AbstractListingConnector parent; @Override public void onUnregister() { @@ -85,7 +86,7 @@ public class SingleSelectionConnector extends AbstractSelectionConnector { } @Override - protected SelectionModel createSelectionModel() { + protected SingleSelection createSelectionModel() { return new SingleSelection(getRpcProxy(SelectionServerRpc.class)); } } diff --git a/uitest/src/main/java/com/vaadin/tests/widgetset/client/data/DummyComponentConnector.java b/uitest/src/main/java/com/vaadin/tests/widgetset/client/data/DummyComponentConnector.java index 061b52abf5..228f5120e7 100644 --- a/uitest/src/main/java/com/vaadin/tests/widgetset/client/data/DummyComponentConnector.java +++ b/uitest/src/main/java/com/vaadin/tests/widgetset/client/data/DummyComponentConnector.java @@ -4,14 +4,15 @@ import com.google.gwt.user.client.ui.FlowPanel; import com.vaadin.client.connectors.AbstractListingConnector; import com.vaadin.client.data.DataSource; import com.vaadin.client.ui.VLabel; -import com.vaadin.shared.data.DataCommunicatorConstants; +import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.shared.ui.Connect; import com.vaadin.tests.data.DummyData.DummyComponent; import elemental.json.JsonObject; @Connect(DummyComponent.class) -public class DummyComponentConnector extends AbstractListingConnector { +public class DummyComponentConnector extends + AbstractListingConnector> { @Override public FlowPanel getWidget() { @@ -30,9 +31,8 @@ public class DummyComponentConnector extends AbstractListingConnector { VLabel label = new VLabel(); getWidget().add(label); JsonObject row = dataSource.getRow(i); - String text = row.getString(DataCommunicatorConstants.DATA); - if (row.hasKey(DataCommunicatorConstants.SELECTED) - && row.getBoolean(DataCommunicatorConstants.SELECTED)) { + String text = getRowData(row).asString(); + if (isRowSelected(row)) { text = "" + text + ""; label.addStyleName("selected"); } -- 2.39.5