Change-Id: I5d5237fcc06ae184a884cb17fa9b6eee5c37179atags/7.6.0.alpha7
@@ -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; | |||
@@ -7513,6 +7514,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> |
@@ -5279,6 +5279,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> |
@@ -282,6 +282,79 @@ public class GridSelection { | |||
assertTrue("selectedRows is empty", grid.getSelectedRows().isEmpty()); | |||
} | |||
@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); |