]> source.dussan.org Git - vaadin-framework.git/commitdiff
Reimplement Grid selection model in terms of AbstractSingleSelection
authorJohannes Dahlström <johannesd@vaadin.com>
Thu, 8 Sep 2016 18:26:35 +0000 (21:26 +0300)
committerVaadin Code Review <review@vaadin.com>
Mon, 12 Sep 2016 08:19:48 +0000 (08:19 +0000)
Change-Id: Id21cb0df32e07bd01f90a29a814dcf0775067bfa

client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java
server/src/main/java/com/vaadin/ui/Grid.java
shared/src/main/java/com/vaadin/shared/ui/AbstractSingleSelectState.java
shared/src/main/java/com/vaadin/shared/ui/grid/GridState.java
uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java

index 863cee0c883e48e7711fc63438452c8592f0a271..e2d21721669d5bf36a96140e4b7566c9f144b907 100644 (file)
@@ -20,6 +20,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.dom.client.NativeEvent;
@@ -45,8 +46,9 @@ 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.DataCommunicatorConstants;
 import com.vaadin.shared.data.selection.SelectionModel;
-import com.vaadin.shared.data.selection.SelectionModel.Single;
+import com.vaadin.shared.data.selection.SelectionServerRpc;
 import com.vaadin.shared.data.sort.SortDirection;
 import com.vaadin.shared.ui.Connect;
 import com.vaadin.shared.ui.grid.GridConstants;
@@ -123,7 +125,8 @@ public class GridConnector
         super.init();
 
         // Default selection style is space key.
-        spaceSelectHandler = new SpaceSelectHandler<JsonObject>(getWidget());
+        spaceSelectHandler = new SpaceSelectHandler<>(getWidget());
+        clickSelectHandler = new ClickSelectHandler<>(getWidget());
         getWidget().addSortHandler(this::handleSortEvent);
         getWidget().setRowStyleGenerator(rowRef -> {
             JsonObject json = rowRef.getRow();
@@ -152,6 +155,32 @@ public class GridConnector
         /* Item click events */
         getWidget().addBodyClickHandler(itemClickHandler);
         getWidget().addBodyDoubleClickHandler(itemClickHandler);
+        getWidget().setSelectionModel(new SelectionModel.Single<JsonObject>() {
+
+            @Override
+            public void select(JsonObject item) {
+                getRpcProxy(SelectionServerRpc.class)
+                        .select(item.getString(DataCommunicatorConstants.KEY));
+            }
+
+            @Override
+            public void deselect(JsonObject item) {
+                getRpcProxy(SelectionServerRpc.class).deselect(
+                        item.getString(DataCommunicatorConstants.KEY));
+            }
+
+            @Override
+            public Optional<JsonObject> getSelectedItem() {
+                throw new UnsupportedOperationException(
+                        "Selected item not known on the client side");
+            }
+
+            @Override
+            public boolean isSelected(JsonObject item) {
+                return item.hasKey(DataCommunicatorConstants.SELECTED)
+                        && item.getBoolean(DataCommunicatorConstants.SELECTED);
+            }
+        });
 
         layout();
     }
@@ -164,15 +193,8 @@ public class GridConnector
 
     @Override
     public void setSelectionModel(SelectionModel<JsonObject> selectionModel) {
-        removeClickHandler();
-
-        super.setSelectionModel(selectionModel);
-        getWidget().setSelectionModel(selectionModel);
-
-        if (selectionModel instanceof Single) {
-            // Single selection should be moved by a click.
-            clickSelectHandler = new ClickSelectHandler<>(getWidget());
-        }
+        throw new UnsupportedOperationException(
+                "Cannot set a selection model for GridConnector");
     }
 
     /**
index c895ad08c3a937877635176d5919d8fb1e4a6930..705a9ca89018828a2125560b32445f5e5d672c28 100644 (file)
@@ -29,11 +29,11 @@ import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.Set;
 import java.util.function.Function;
 import java.util.stream.Stream;
 
-import com.vaadin.data.selection.SingleSelection;
 import com.vaadin.event.ConnectorEvent;
 import com.vaadin.event.EventListener;
 import com.vaadin.server.KeyMapper;
@@ -41,7 +41,6 @@ import com.vaadin.server.data.SortOrder;
 import com.vaadin.shared.MouseEventDetails;
 import com.vaadin.shared.Registration;
 import com.vaadin.shared.data.DataCommunicatorConstants;
-import com.vaadin.shared.data.selection.SelectionModel;
 import com.vaadin.shared.data.sort.SortDirection;
 import com.vaadin.shared.ui.grid.ColumnState;
 import com.vaadin.shared.ui.grid.GridConstants;
@@ -67,8 +66,7 @@ import elemental.json.JsonValue;
  * @param <T>
  *            the grid bean type
  */
-public class Grid<T> extends AbstractListing<T, SelectionModel<T>>
-        implements HasComponents {
+public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents {
 
     @Deprecated
     private static final Method ITEM_CLICK_METHOD = ReflectTools
@@ -197,7 +195,7 @@ public class Grid<T> extends AbstractListing<T, SelectionModel<T>>
      *
      * @param <T>
      */
-    public static abstract class AbstractGridExtension<T>
+    public abstract static class AbstractGridExtension<T>
             extends AbstractListingExtension<T> {
 
         @Override
@@ -776,6 +774,44 @@ public class Grid<T> extends AbstractListing<T, SelectionModel<T>>
         }
     }
 
+    private final class SingleSelection extends AbstractSingleSelection {
+        private T selectedItem = null;
+
+        SingleSelection() {
+            addDataGenerator((item, json) -> {
+                if (isSelected(item)) {
+                    json.put(DataCommunicatorConstants.SELECTED, true);
+                }
+            });
+        }
+
+        @Override
+        public Optional<T> getSelectedItem() {
+            return Optional.ofNullable(selectedItem);
+        }
+
+        @Override
+        protected boolean isKeySelected(String key) {
+            return Objects.equals(key, getSelectedKey());
+        }
+
+        @Override
+        protected String getSelectedKey() {
+            return itemToKey(selectedItem);
+        }
+
+        @Override
+        protected void doSetSelectedKey(String key) {
+            if (selectedItem != null) {
+                getDataCommunicator().refresh(selectedItem);
+            }
+            selectedItem = keyToItem(key);
+            if (selectedItem != null) {
+                getDataCommunicator().refresh(selectedItem);
+            }
+        }
+    }
+
     private KeyMapper<Column<T, ?>> columnKeys = new KeyMapper<>();
     private Set<Column<T, ?>> columnSet = new LinkedHashSet<>();
     private List<SortOrder<Column<T, ?>>> sortOrder = new ArrayList<>();
@@ -788,7 +824,7 @@ public class Grid<T> extends AbstractListing<T, SelectionModel<T>>
      * Constructor for the {@link Grid} component.
      */
     public Grid() {
-        setSelectionModel(new SingleSelection<>(this));
+        setSelectionModel(new SingleSelection());
         registerRpc(new GridServerRpcImpl());
         detailsManager = new DetailsManager<>();
         addExtension(detailsManager);
index 3889fdae2b60728ae25bcaaf379b69e80f2e7c7a..98aad0bf37f2509edacf37fb2eb50b1dcd02d305 100644 (file)
@@ -15,8 +15,6 @@
  */
 package com.vaadin.shared.ui;
 
-import com.vaadin.shared.AbstractComponentState;
-
 /**
  * Shared state for {@code AbstractSingleSelect}.
  *
@@ -24,7 +22,7 @@ import com.vaadin.shared.AbstractComponentState;
  *
  * @since 8.0
  */
-public class AbstractSingleSelectState extends AbstractComponentState {
+public class AbstractSingleSelectState extends TabIndexState {
 
     /**
      * The key of the currently selected item or {@code null} if no item is
index ffdbc79a323c3f0f3b3fdf81cc0583770d92cfae..4f480139d51958762b7ba84e5f3ec9664d9f5890 100644 (file)
@@ -21,7 +21,7 @@ import java.util.List;
 
 import com.vaadin.shared.annotations.DelegateToWidget;
 import com.vaadin.shared.data.sort.SortDirection;
-import com.vaadin.shared.ui.TabIndexState;
+import com.vaadin.shared.ui.AbstractSingleSelectState;
 
 /**
  * The shared state for the {@link com.vaadin.ui.Grid} component.
@@ -29,7 +29,7 @@ import com.vaadin.shared.ui.TabIndexState;
  * @since 8.0
  * @author Vaadin Ltd
  */
-public class GridState extends TabIndexState {
+public class GridState extends AbstractSingleSelectState {
 
     /**
      * The default value for height-by-rows for both GWT widgets
index c82a434e2382ff0071ea8b49c0ef3470061c9a9f..0278149be939e9d612b59616ab0396fbb2e0e56c 100644 (file)
@@ -9,7 +9,6 @@ import java.util.function.Consumer;
 import java.util.stream.Stream;
 
 import com.vaadin.annotations.Widgetset;
-import com.vaadin.data.selection.SingleSelection;
 import com.vaadin.server.VaadinRequest;
 import com.vaadin.shared.Registration;
 import com.vaadin.shared.ui.grid.HeightMode;
@@ -146,8 +145,7 @@ public class GridBasics extends AbstractTestUIWithLog {
         grid.addColumn("Small Random", data -> data.getSmallRandom() / 5d,
                 new ProgressBarRenderer());
 
-        ((SingleSelection<DataObject>) grid.getSelectionModel())
-                .addSelectionListener(e -> log("Selected: " + e.getValue()));
+        grid.addSelectionListener(e -> log("Selected: " + e.getValue()));
 
         layout.addComponent(createMenu());
         layout.addComponent(grid);