From 22c372b7a5cc6540d45b9add88d447ff52eab62d Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Sat, 15 Oct 2016 22:44:27 +0300 Subject: Send selection change events when changing selection mode (#20391) Change-Id: Iaeb772981f3ac039bf0fe3cde595cfdb3691cddf --- .../src/main/java/com/vaadin/v7/ui/Grid.java | 13 ++++++++- .../server/component/grid/GridSelectionTest.java | 34 ++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) (limited to 'compatibility-server') diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/Grid.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/Grid.java index ce78fa33cf..ef35808f99 100644 --- a/compatibility-server/src/main/java/com/vaadin/v7/ui/Grid.java +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/Grid.java @@ -5621,7 +5621,8 @@ public class Grid extends AbstractComponent * Takes a new {@link SelectionModel} into use. *

* The SelectionModel that is previously in use will have all its items - * deselected. + * deselected. If any items were selected, this will fire a + * {@link SelectionEvent}. *

* If the given SelectionModel is already in use, this method does nothing. * @@ -5638,13 +5639,23 @@ public class Grid extends AbstractComponent } if (this.selectionModel != selectionModel) { + Collection oldSelection; // this.selectionModel is null on init if (this.selectionModel != null) { + oldSelection = this.selectionModel.getSelectedRows(); this.selectionModel.remove(); + } else { + oldSelection = Collections.emptyList(); } this.selectionModel = selectionModel; selectionModel.setGrid(this); + Collection newSelection = this.selectionModel + .getSelectedRows(); + + if (!SharedUtil.equals(oldSelection, newSelection)) { + fireSelectionEvent(oldSelection, newSelection); + } } } diff --git a/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/grid/GridSelectionTest.java b/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/grid/GridSelectionTest.java index afc38a4201..2421e8d07a 100644 --- a/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/grid/GridSelectionTest.java +++ b/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/grid/GridSelectionTest.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertTrue; import java.util.Collection; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -374,4 +375,37 @@ public class GridSelectionTest { assertEquals("selectedRows is correct", itemId2Present, grid.getSelectedRow()); } + + @Test + public void selectionChangeEventWhenChangingSelectionModeSingleToNone() { + grid.select(itemId1Present); + Assert.assertEquals(itemId1Present, grid.getSelectedRow()); + mockListener.clearEvent(); + grid.setSelectionMode(SelectionMode.NONE); + assertTrue(mockListener.eventHasHappened()); + assertTrue(mockListener.getRemoved().contains(itemId1Present)); + } + + @Test + public void selectionChangeEventWhenChangingSelectionModeMultiToNone() { + grid.setSelectionMode(SelectionMode.MULTI); + grid.select(itemId1Present); + grid.select(itemId2Present); + mockListener.clearEvent(); + grid.setSelectionMode(SelectionMode.NONE); + assertTrue(mockListener.eventHasHappened()); + assertTrue(mockListener.getRemoved().contains(itemId1Present)); + assertTrue(mockListener.getRemoved().contains(itemId2Present)); + } + + @Test + public void noSelectionChangeEventWhenChanginModeWithNoneSelected() { + mockListener.clearEvent(); + grid.setSelectionMode(SelectionMode.SINGLE); + assertFalse(mockListener.eventHasHappened()); + grid.setSelectionMode(SelectionMode.NONE); + assertFalse(mockListener.eventHasHappened()); + grid.setSelectionMode(SelectionMode.MULTI); + assertFalse(mockListener.eventHasHappened()); + } } -- cgit v1.2.3