diff options
author | Artur Signell <artur@vaadin.com> | 2015-08-03 23:42:27 +0300 |
---|---|---|
committer | Teemu Suo-Anttila <teemusa@vaadin.com> | 2015-10-09 09:53:52 +0000 |
commit | 98f9c438d8d03c4dbc5d896ef245d252a166b9ed (patch) | |
tree | d0e5fbe545f6fc3fe5547888fcbf73294041fc61 /server | |
parent | a0637c8feb47dc4d83ce976a7c1db769238a6c95 (diff) | |
download | vaadin-framework-98f9c438d8d03c4dbc5d896ef245d252a166b9ed.tar.gz vaadin-framework-98f9c438d8d03c4dbc5d896ef245d252a166b9ed.zip |
Add Grid.deselectAll() (#18339)
Change-Id: I5d5237fcc06ae184a884cb17fa9b6eee5c37179a
Diffstat (limited to 'server')
-rw-r--r-- | server/src/com/vaadin/ui/Grid.java | 41 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/tests/server/component/grid/GridSelection.java | 73 |
2 files changed, 114 insertions, 0 deletions
diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index 8d8b0fe8e3..d44cb31cb0 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -5280,6 +5280,47 @@ public class Grid extends AbstractFocusable implements SelectionNotifier, } /** + * Marks all items as unselected. + * <p> + * This method is a shorthand that delegates to the + * {@link #getSelectionModel() selection model}. Only + * {@link SelectionModel.Single} and {@link SelectionModel.Multi} are + * supported. + * + * @return <code>true</code> if the selection state changed, + * <code>false</code> if the itemId was already selected + * @throws IllegalStateException + * if the deselection was illegal. One such reason might be that + * the implementation requires one or more items to be selected + * at all times. + * @throws IllegalStateException + * if the selection model does not implement + * {@code SelectionModel.Single} or {code SelectionModel.Multi} + */ + public boolean deselectAll() throws IllegalStateException { + if (selectionModel instanceof SelectionModel.Single) { + if (getSelectedRow() != null) { + return deselect(getSelectedRow()); + } + return false; + } else if (selectionModel instanceof SelectionModel.Multi) { + return ((SelectionModel.Multi) selectionModel).deselectAll(); + } else if (selectionModel instanceof SelectionModel.None) { + throw new IllegalStateException("Cannot deselect all rows" + + ": Grid selection is disabled " + + "(the current selection model is " + + selectionModel.getClass().getName() + ")."); + } else { + throw new IllegalStateException("Cannot deselect all rows:" + + " Grid selection model does not implement " + + SelectionModel.Single.class.getName() + " or " + + SelectionModel.Multi.class.getName() + + "(the current model is " + + selectionModel.getClass().getName() + ")."); + } + } + + /** * Fires a selection change event. * <p> * <strong>Note:</strong> This is not a method that should be called by diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/GridSelection.java b/server/tests/src/com/vaadin/tests/server/component/grid/GridSelection.java index 7f09677b50..dc641965ba 100644 --- a/server/tests/src/com/vaadin/tests/server/component/grid/GridSelection.java +++ b/server/tests/src/com/vaadin/tests/server/component/grid/GridSelection.java @@ -283,6 +283,79 @@ public class GridSelection { } @Test + public void gridDeselectAllMultiAllSelected() { + grid.setSelectionMode(SelectionMode.MULTI); + final SelectionModel.Multi select = (SelectionModel.Multi) grid + .getSelectionModel(); + select.selectAll(); + mockListener.clearEvent(); + + assertTrue(grid.deselectAll()); + assertEquals("removed size", 10, mockListener.getRemoved().size()); + assertEquals("added size", 0, mockListener.getAdded().size()); + assertTrue("itemId1Present", + mockListener.getRemoved().contains(itemId1Present)); + assertTrue("itemId2Present", + mockListener.getRemoved().contains(itemId2Present)); + assertTrue("selectedRows is empty", grid.getSelectedRows().isEmpty()); + + } + + @Test + public void gridDeselectAllMultiOneSelected() { + grid.setSelectionMode(SelectionMode.MULTI); + final SelectionModel.Multi select = (SelectionModel.Multi) grid + .getSelectionModel(); + select.select(itemId2Present); + mockListener.clearEvent(); + + assertTrue(grid.deselectAll()); + assertEquals("removed size", 1, mockListener.getRemoved().size()); + assertEquals("added size", 0, mockListener.getAdded().size()); + assertFalse("itemId1Present", + mockListener.getRemoved().contains(itemId1Present)); + assertTrue("itemId2Present", + mockListener.getRemoved().contains(itemId2Present)); + assertTrue("selectedRows is empty", grid.getSelectedRows().isEmpty()); + + } + + @Test + public void gridDeselectAllSingleNoneSelected() { + grid.setSelectionMode(SelectionMode.SINGLE); + assertFalse(grid.deselectAll()); + assertTrue("selectedRows is empty", grid.getSelectedRows().isEmpty()); + } + + @Test + public void gridDeselectAllSingleOneSelected() { + grid.setSelectionMode(SelectionMode.SINGLE); + final SelectionModel.Single select = (SelectionModel.Single) grid + .getSelectionModel(); + select.select(itemId2Present); + mockListener.clearEvent(); + + assertTrue(grid.deselectAll()); + assertEquals("removed size", 1, mockListener.getRemoved().size()); + assertEquals("added size", 0, mockListener.getAdded().size()); + assertFalse("itemId1Present", + mockListener.getRemoved().contains(itemId1Present)); + assertTrue("itemId2Present", + mockListener.getRemoved().contains(itemId2Present)); + assertTrue("selectedRows is empty", grid.getSelectedRows().isEmpty()); + + } + + @Test + public void gridDeselectAllMultiNoneSelected() { + grid.setSelectionMode(SelectionMode.MULTI); + + assertFalse(grid.deselectAll()); + assertTrue("selectedRows is empty", grid.getSelectedRows().isEmpty()); + + } + + @Test public void reselectionDeselectsPreviousSingle() { grid.setSelectionMode(SelectionMode.SINGLE); grid.select(itemId1Present); |