summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2016-10-15 22:44:27 +0300
committerArtur Signell <artur@vaadin.com>2016-10-18 08:15:02 +0300
commit643365176aeae6edd28fcdd9f98602dec7a80e77 (patch)
tree43a485afc4585ccbe5a0589532709629ecbc8869 /server
parente4d824a0607f212abfd3fb36fef5224cc0034903 (diff)
downloadvaadin-framework-643365176aeae6edd28fcdd9f98602dec7a80e77.tar.gz
vaadin-framework-643365176aeae6edd28fcdd9f98602dec7a80e77.zip
Send selection change events when changing selection mode (#20391)
Change-Id: Iaeb772981f3ac039bf0fe3cde595cfdb3691cddf
Diffstat (limited to 'server')
-rw-r--r--server/src/main/java/com/vaadin/ui/Grid.java13
-rw-r--r--server/src/test/java/com/vaadin/tests/server/component/grid/GridSelectionTest.java34
2 files changed, 46 insertions, 1 deletions
diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java
index 34826bf6ee..3f61dcd88f 100644
--- a/server/src/main/java/com/vaadin/ui/Grid.java
+++ b/server/src/main/java/com/vaadin/ui/Grid.java
@@ -5560,7 +5560,8 @@ public class Grid extends AbstractFocusable implements SelectionNotifier,
* Takes a new {@link SelectionModel} into use.
* <p>
* 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}.
* <p>
* If the given SelectionModel is already in use, this method does nothing.
*
@@ -5577,13 +5578,23 @@ public class Grid extends AbstractFocusable implements SelectionNotifier,
}
if (this.selectionModel != selectionModel) {
+ Collection<Object> 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<Object> newSelection = this.selectionModel
+ .getSelectedRows();
+
+ if (!SharedUtil.equals(oldSelection, newSelection)) {
+ fireSelectionEvent(oldSelection, newSelection);
+ }
}
}
diff --git a/server/src/test/java/com/vaadin/tests/server/component/grid/GridSelectionTest.java b/server/src/test/java/com/vaadin/tests/server/component/grid/GridSelectionTest.java
index 8828d941ee..90644412fb 100644
--- a/server/src/test/java/com/vaadin/tests/server/component/grid/GridSelectionTest.java
+++ b/server/src/test/java/com/vaadin/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());
+ }
}