From 0a325d95a588010a6d5c511afddb8db8048d50ee Mon Sep 17 00:00:00 2001 From: Aleksi Hietanen Date: Fri, 3 Feb 2017 13:27:51 +0200 Subject: Add selection methods to grid that delegate to its selection model (#8315) * Add selection methods to grid that delegate to the selection model * Merge remote-tracking branch 'github/master' into 577-alt * Remove getFirstSelected, isSelected --- server/src/main/java/com/vaadin/ui/Grid.java | 46 +++++++++++++++++- .../grid/GridDelegatesToSelectionModelTest.java | 55 ++++++++++++++++++++++ 2 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 server/src/test/java/com/vaadin/tests/components/grid/GridDelegatesToSelectionModelTest.java (limited to 'server') diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index 8e12b4a20b..f3dc3d9389 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -3044,6 +3044,50 @@ public class Grid extends AbstractListing implements HasComponents, return model; } + /** + * This method is a shorthand that delegates to the currently set selection + * model. + * + * @see #getSelectionModel() + * @see GridSelectionModel + */ + public Set getSelectedItems() { + return getSelectionModel().getSelectedItems(); + } + + /** + * This method is a shorthand that delegates to the currently set selection + * model. + * + * @see #getSelectionModel() + * @see GridSelectionModel + */ + public void select(T item) { + getSelectionModel().select(item); + } + + /** + * This method is a shorthand that delegates to the currently set selection + * model. + * + * @see #getSelectionModel() + * @see GridSelectionModel + */ + public void deselect(T item) { + getSelectionModel().deselect(item); + } + + /** + * This method is a shorthand that delegates to the currently set selection + * model. + * + * @see #getSelectionModel() + * @see GridSelectionModel + */ + public void deselectAll() { + getSelectionModel().deselectAll(); + } + /** * Adds a selection listener to the current selection model. *

@@ -3621,7 +3665,6 @@ public class Grid extends AbstractListing implements HasComponents, * * @return the comparator based on column sorting information. */ - protected SerializableComparator createSortingComparator() { BinaryOperator> operator = (comparator1, comparator2) -> SerializableComparator @@ -3631,5 +3674,4 @@ public class Grid extends AbstractListing implements HasComponents, order -> order.getSorted().getComparator(order.getDirection())) .reduce((x, y) -> 0, operator); } - } diff --git a/server/src/test/java/com/vaadin/tests/components/grid/GridDelegatesToSelectionModelTest.java b/server/src/test/java/com/vaadin/tests/components/grid/GridDelegatesToSelectionModelTest.java new file mode 100644 index 0000000000..ae121d2e03 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/components/grid/GridDelegatesToSelectionModelTest.java @@ -0,0 +1,55 @@ +package com.vaadin.tests.components.grid; + +import static org.mockito.Mockito.verify; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +import com.vaadin.ui.Grid; +import com.vaadin.ui.components.grid.GridSelectionModel; + +public class GridDelegatesToSelectionModelTest { + + private GridSelectionModel selectionModelMock; + + private CustomGrid grid; + + private class CustomGrid extends Grid { + CustomGrid() { + super(); + setSelectionModel(selectionModelMock); + } + } + + @Before + @SuppressWarnings("unchecked") + public void init() { + selectionModelMock = Mockito.mock(GridSelectionModel.class); + grid = new CustomGrid(); + } + + @Test + public void grid_getSelectedItems_delegated_to_SelectionModel() { + grid.getSelectedItems(); + verify(selectionModelMock).getSelectedItems(); + } + + @Test + public void grid_select_delegated_to_SelectionModel() { + grid.select(""); + verify(selectionModelMock).select(""); + } + + @Test + public void grid_deselect_delegated_to_SelectionModel() { + grid.deselect(""); + verify(selectionModelMock).deselect(""); + } + + @Test + public void grid_deselectAll_delegated_to_SelectionModel() { + grid.deselectAll(); + verify(selectionModelMock).deselectAll(); + } +} -- cgit v1.2.3