]> source.dussan.org Git - vaadin-framework.git/commitdiff
Send selection change events when changing selection mode (#20391)
authorArtur Signell <artur@vaadin.com>
Sat, 15 Oct 2016 19:44:27 +0000 (22:44 +0300)
committerArtur Signell <artur@vaadin.com>
Tue, 18 Oct 2016 05:15:02 +0000 (08:15 +0300)
Change-Id: Iaeb772981f3ac039bf0fe3cde595cfdb3691cddf

server/src/main/java/com/vaadin/ui/Grid.java
server/src/test/java/com/vaadin/tests/server/component/grid/GridSelectionTest.java

index 34826bf6eec836e54c9ffdc9e882666c3ee27ccf..3f61dcd88fd60397c1397706bbaba149c7ff48ce 100644 (file)
@@ -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);
+            }
         }
     }
 
index 8828d941ee5fb3c925545e1d747c93d12f027452..90644412fb2e214ad51e20f1f773960263a480dc 100644 (file)
@@ -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());
+    }
 }