diff options
-rw-r--r-- | client/src/com/vaadin/client/widgets/Grid.java | 26 | ||||
-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 |
3 files changed, 140 insertions, 0 deletions
diff --git a/client/src/com/vaadin/client/widgets/Grid.java b/client/src/com/vaadin/client/widgets/Grid.java index e01edcddd7..db7b25720e 100644 --- a/client/src/com/vaadin/client/widgets/Grid.java +++ b/client/src/com/vaadin/client/widgets/Grid.java @@ -151,6 +151,7 @@ import com.vaadin.client.widget.grid.selection.SelectionEvent; import com.vaadin.client.widget.grid.selection.SelectionHandler; import com.vaadin.client.widget.grid.selection.SelectionModel; import com.vaadin.client.widget.grid.selection.SelectionModel.Multi; +import com.vaadin.client.widget.grid.selection.SelectionModel.Single; import com.vaadin.client.widget.grid.selection.SelectionModelMulti; import com.vaadin.client.widget.grid.selection.SelectionModelNone; import com.vaadin.client.widget.grid.selection.SelectionModelSingle; @@ -7514,6 +7515,31 @@ public class Grid<T> extends ResizeComposite implements } /** + * Deselect all rows using the current selection model. + * + * @param row + * a row object + * @return <code>true</code> iff the current selection changed + * @throws IllegalStateException + * if the current selection model is not an instance of + * {@link SelectionModel.Single} or {@link SelectionModel.Multi} + */ + public boolean deselectAll() { + if (selectionModel instanceof SelectionModel.Single<?>) { + Single<T> single = ((SelectionModel.Single<T>) selectionModel); + if (single.getSelectedRow() != null) { + return single.deselect(single.getSelectedRow()); + } else { + return false; + } + } else if (selectionModel instanceof SelectionModel.Multi<?>) { + return ((SelectionModel.Multi<T>) selectionModel).deselectAll(); + } else { + throw new IllegalStateException("Unsupported selection model"); + } + } + + /** * Gets last selected row from the current SelectionModel. * <p> * Only selection models implementing {@link SelectionModel.Single} are 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); |