Browse Source

Add sanity checking to server side selection models (#13334)

Change-Id: I9686cdbf82c017f834502cf56eafe23ca9829d5f
tags/7.4.0.beta1
Teemu Suo-Anttila 9 years ago
parent
commit
60221e9625

+ 34
- 0
server/src/com/vaadin/ui/components/grid/selection/AbstractSelectionModel.java View File

@@ -47,6 +47,40 @@ public abstract class AbstractSelectionModel implements SelectionModel {
this.grid = grid;
}

/**
* Sanity check for existence of item id.
*
* @param itemId
* item id to be selected / deselected
*
* @throws IllegalArgumentException
* if item Id doesn't exist in the container of Grid
*/
protected void checkItemIdExists(Object itemId)
throws IllegalArgumentException {
if (!grid.getContainerDataSource().containsId(itemId)) {
throw new IllegalArgumentException("Given item id (" + itemId
+ ") does not exist in the container");
}
}

/**
* Sanity check for existence of item ids in given collection.
*
* @param itemIds
* item id collection to be selected / deselected
*
* @throws IllegalArgumentException
* if at least one item id doesn't exist in the container of
* Grid
*/
protected void checkItemIdsExist(Collection<?> itemIds)
throws IllegalArgumentException {
for (Object itemId : itemIds) {
checkItemIdExists(itemId);
}
}

/**
* Fires a {@link SelectionChangeEvent} to all the
* {@link SelectionChangeListener SelectionChangeListeners} currently added

+ 3
- 0
server/src/com/vaadin/ui/components/grid/selection/MultiSelectionModel.java View File

@@ -66,6 +66,9 @@ public class MultiSelectionModel extends AbstractSelectionModel implements
throw new IllegalArgumentException("itemIds may not be null");
}

// Sanity check
checkItemIdsExist(itemIds);

final boolean selectionWillChange = !selection.containsAll(itemIds)
&& selection.size() < selectionLimit;
if (selectionWillChange) {

+ 8
- 3
server/src/com/vaadin/ui/components/grid/selection/SelectionModel.java View File

@@ -87,7 +87,8 @@ public interface SelectionModel extends Serializable {
* selected
* @throws IllegalArgumentException
* if the <code>itemIds</code> varargs array is
* <code>null</code>
* <code>null</code> or given itemIds don't exist in the
* container of Grid
* @see #deselect(Object...)
*/
boolean select(Object... itemIds) throws IllegalArgumentException;
@@ -104,7 +105,8 @@ public interface SelectionModel extends Serializable {
* <code>false</code> if all the given itemIds already were
* selected
* @throws IllegalArgumentException
* if <code>itemIds</code> is <code>null</code>
* if <code>itemIds</code> is <code>null</code> or given
* itemIds don't exist in the container of Grid
* @see #deselect(Collection)
*/
boolean select(Collection<?> itemIds) throws IllegalArgumentException;
@@ -177,9 +179,12 @@ public interface SelectionModel extends Serializable {
* that the implementation already had an item selected, and
* that needs to be explicitly deselected before
* re-selecting something
* @throws IllegalArgumentException
* if given itemId does not exist in the container of Grid
* @see #deselect(Object)
*/
boolean select(Object itemId) throws IllegalStateException;
boolean select(Object itemId) throws IllegalStateException,
IllegalArgumentException;

/**
* Marks an item as deselected.

+ 2
- 0
server/src/com/vaadin/ui/components/grid/selection/SingleSelectionModel.java View File

@@ -28,6 +28,8 @@ public class SingleSelectionModel extends AbstractSelectionModel implements
SelectionModel.Single {
@Override
public boolean select(final Object itemId) {
checkItemIdExists(itemId);

final Object selectedRow = getSelectedRow();
final boolean modified = selection.add(itemId);
if (modified) {

+ 4
- 4
server/tests/src/com/vaadin/tests/server/component/grid/GridSelection.java View File

@@ -243,14 +243,14 @@ public class GridSelection {
grid.deselect(itemId1NotPresent);
}

@Test
public void selectNotPresentItemIdShouldNotThrowExceptionMulti() {
@Test(expected = IllegalArgumentException.class)
public void selectNotPresentItemIdShouldThrowExceptionMulti() {
grid.setSelectionMode(SelectionMode.MULTI);
grid.select(itemId1NotPresent);
}

@Test
public void selectNotPresentItemIdShouldNotThrowExceptionSingle() {
@Test(expected = IllegalArgumentException.class)
public void selectNotPresentItemIdShouldThrowExceptionSingle() {
grid.setSelectionMode(SelectionMode.SINGLE);
grid.select(itemId1NotPresent);
}

Loading…
Cancel
Save