]> source.dussan.org Git - vaadin-framework.git/commitdiff
Parameterize listing and selection connectors by selection model type
authorJohannes Dahlström <johannesd@vaadin.com>
Mon, 5 Sep 2016 16:34:19 +0000 (19:34 +0300)
committerVaadin Code Review <review@vaadin.com>
Wed, 7 Sep 2016 07:19:58 +0000 (07:19 +0000)
Change-Id: I9c31582242b0b37b8a732e41bc73c59881dcf68b

client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java
client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java
client/src/main/java/com/vaadin/client/connectors/selection/AbstractSelectionConnector.java
client/src/main/java/com/vaadin/client/connectors/selection/SingleSelectionConnector.java
uitest/src/main/java/com/vaadin/tests/widgetset/client/data/DummyComponentConnector.java

index 938df199da0d7d0202aba8a3d3ae26fa2f1c40d4..6593b5a0be68e878a2ac789820b5727acd64cdc3 100644 (file)
@@ -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 <SELECTIONMODEL>
+ *            the client-side selection model type
+ *
+ * @since 8.0
  */
-public abstract class AbstractListingConnector
+public abstract class AbstractListingConnector<SELECTIONMODEL extends SelectionModel<?>>
         extends AbstractComponentConnector implements HasDataSource {
 
     private DataSource<JsonObject> dataSource = null;
 
-    private SelectionModel<JsonObject> selectionModel = null;
+    private SELECTIONMODEL selectionModel = null;
 
     @Override
     public void setDataSource(DataSource<JsonObject> dataSource) {
@@ -51,11 +58,49 @@ public abstract class AbstractListingConnector
      * @param selectionModel
      *            the selection model or null to disable
      */
-    public void setSelectionModel(SelectionModel<JsonObject> selectionModel) {
+    public void setSelectionModel(SELECTIONMODEL selectionModel) {
         this.selectionModel = selectionModel;
     }
 
-    public SelectionModel<JsonObject> 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);
+    }
 }
index 31f6cc38ab89dcf5bf16ba110093c58862d9de5a..91adcc14ac42e9e7683c9326599aa956e05eb525 100644 (file)
@@ -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<SelectionModel<JsonObject>>
         implements HasComponentsConnector, SimpleManagedLayout, DeferredWorker {
 
     /* Map to keep track of all added columns */
@@ -63,6 +64,7 @@ public class GridConnector extends AbstractListingConnector
     private ClickSelectHandler<JsonObject> clickSelectHandler;
 
     @Override
+    @SuppressWarnings("unchecked")
     public Grid<JsonObject> getWidget() {
         return (Grid<JsonObject>) super.getWidget();
     }
index 7edc944a224de03a86c22d9fa8dad215c693b4d3..9d4604993dcb2b528b2da0c0df8e0424d99a9f0c 100644 (file)
@@ -27,15 +27,18 @@ import elemental.json.JsonObject;
  * The client-side connector for selection extensions.
  *
  * @author Vaadin Ltd.
- *
- * @since
+ * 
+ * @param <SELECTIONMODEL>
+ *            the supported client-side selection model
+ * @since 8.0
  */
-public abstract class AbstractSelectionConnector
+public abstract class AbstractSelectionConnector<SELECTIONMODEL extends SelectionModel<?>>
         extends AbstractExtensionConnector {
 
-    private SelectionModel<JsonObject> 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<SELECTIONMODEL>) target)
+                .setSelectionModel(model);
     }
 
     /**
@@ -51,11 +55,12 @@ public abstract class AbstractSelectionConnector
      *
      * @return created selection model
      */
-    protected abstract SelectionModel<JsonObject> createSelectionModel();
+    protected abstract SELECTIONMODEL createSelectionModel();
 
     @Override
-    public AbstractListingConnector getParent() {
-        return (AbstractListingConnector) super.getParent();
+    @SuppressWarnings("unchecked")
+    public AbstractListingConnector<SELECTIONMODEL> getParent() {
+        return (AbstractListingConnector<SELECTIONMODEL>) super.getParent();
     }
 
     /**
@@ -63,7 +68,7 @@ public abstract class AbstractSelectionConnector
      *
      * @return the selection model in use
      */
-    protected SelectionModel<JsonObject> getSelectionModel() {
+    protected SELECTIONMODEL getSelectionModel() {
         return model;
     }
 
index 912b20310e4c3d4a13ad33d7d5c4033cbff1f74e..875a70412c9ea4a1cb7ef76fad385abb3af4ea69 100644 (file)
@@ -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<SelectionModel.Single<JsonObject>> {
 
     private static class SingleSelection
             implements SelectionModel.Single<JsonObject> {
@@ -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<JsonObject> createSelectionModel() {
+    protected SingleSelection createSelectionModel() {
         return new SingleSelection(getRpcProxy(SelectionServerRpc.class));
     }
 }
index 061b52abf589cb9074c188f28af37b02ed0cb62c..228f5120e7cea174c33dcf16afa4bbe1b6d943cb 100644 (file)
@@ -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<SelectionModel<?>> {
 
     @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 = "<b>" + text + "</b>";
                     label.addStyleName("selected");
                 }