* 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.
*
}
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);
+ }
}
}
import java.util.Collection;
+import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
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());
+ }
}