From: Johannes Dahlström Date: Thu, 8 Sep 2016 18:26:35 +0000 (+0300) Subject: Reimplement Grid selection model in terms of AbstractSingleSelection X-Git-Tag: 8.0.0.alpha2~34 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=34dc467f08440e2b1a4e563ede205590002ac64e;p=vaadin-framework.git Reimplement Grid selection model in terms of AbstractSingleSelection Change-Id: Id21cb0df32e07bd01f90a29a814dcf0775067bfa --- 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 863cee0c88..e2d2172166 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 @@ -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(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() { + + @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 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 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"); } /** diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index c895ad08c3..705a9ca890 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -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 * the grid bean type */ -public class Grid extends AbstractListing> - implements HasComponents { +public class Grid extends AbstractSingleSelect implements HasComponents { @Deprecated private static final Method ITEM_CLICK_METHOD = ReflectTools @@ -197,7 +195,7 @@ public class Grid extends AbstractListing> * * @param */ - public static abstract class AbstractGridExtension + public abstract static class AbstractGridExtension extends AbstractListingExtension { @Override @@ -776,6 +774,44 @@ public class Grid extends AbstractListing> } } + 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 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> columnKeys = new KeyMapper<>(); private Set> columnSet = new LinkedHashSet<>(); private List>> sortOrder = new ArrayList<>(); @@ -788,7 +824,7 @@ public class Grid extends AbstractListing> * Constructor for the {@link Grid} component. */ public Grid() { - setSelectionModel(new SingleSelection<>(this)); + setSelectionModel(new SingleSelection()); registerRpc(new GridServerRpcImpl()); detailsManager = new DetailsManager<>(); addExtension(detailsManager); diff --git a/shared/src/main/java/com/vaadin/shared/ui/AbstractSingleSelectState.java b/shared/src/main/java/com/vaadin/shared/ui/AbstractSingleSelectState.java index 3889fdae2b..98aad0bf37 100644 --- a/shared/src/main/java/com/vaadin/shared/ui/AbstractSingleSelectState.java +++ b/shared/src/main/java/com/vaadin/shared/ui/AbstractSingleSelectState.java @@ -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 diff --git a/shared/src/main/java/com/vaadin/shared/ui/grid/GridState.java b/shared/src/main/java/com/vaadin/shared/ui/grid/GridState.java index ffdbc79a32..4f480139d5 100644 --- a/shared/src/main/java/com/vaadin/shared/ui/grid/GridState.java +++ b/shared/src/main/java/com/vaadin/shared/ui/grid/GridState.java @@ -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 diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java b/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java index c82a434e23..0278149be9 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java @@ -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) grid.getSelectionModel()) - .addSelectionListener(e -> log("Selected: " + e.getValue())); + grid.addSelectionListener(e -> log("Selected: " + e.getValue())); layout.addComponent(createMenu()); layout.addComponent(grid);