diff options
Diffstat (limited to 'server/src')
4 files changed, 62 insertions, 11 deletions
diff --git a/server/src/main/java/com/vaadin/ui/components/grid/MultiSelectionModelImpl.java b/server/src/main/java/com/vaadin/ui/components/grid/MultiSelectionModelImpl.java index f85c781345..9c2b506107 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/MultiSelectionModelImpl.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/MultiSelectionModelImpl.java @@ -324,16 +324,12 @@ public class MultiSelectionModelImpl<T> extends AbstractSelectionModel<T> @Override public void setReadOnly(boolean readOnly) { - // TODO support read only in grid ? - throw new UnsupportedOperationException( - "Read only mode is not supported for grid."); + getState().selectionAllowed = readOnly; } @Override public boolean isReadOnly() { - // TODO support read only in grid ? - throw new UnsupportedOperationException( - "Read only mode is not supported for grid."); + return isUserSelectionAllowed(); } @Override @@ -429,6 +425,11 @@ public class MultiSelectionModelImpl<T> extends AbstractSelectionModel<T> Objects.requireNonNull(addedItems); Objects.requireNonNull(removedItems); + if (userOriginated && !isUserSelectionAllowed()) { + throw new IllegalStateException("Client tried to update selection" + + " although user selection is disallowed"); + } + // if there are duplicates, some item is both added & removed, just // discard that and leave things as was before addedItems.removeIf(item -> removedItems.remove(item)); @@ -457,6 +458,10 @@ public class MultiSelectionModelImpl<T> extends AbstractSelectionModel<T> }, userOriginated); } + private boolean isUserSelectionAllowed() { + return getState(false).selectionAllowed; + } + private void doUpdateSelection(Consumer<Set<T>> handler, boolean userOriginated) { if (getParent() == null) { diff --git a/server/src/main/java/com/vaadin/ui/components/grid/SingleSelectionModelImpl.java b/server/src/main/java/com/vaadin/ui/components/grid/SingleSelectionModelImpl.java index ac1edaf576..6db6bfa61c 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/SingleSelectionModelImpl.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/SingleSelectionModelImpl.java @@ -169,6 +169,10 @@ public class SingleSelectionModelImpl<T> extends AbstractSelectionModel<T> * selection */ protected void setSelectedFromClient(String key) { + if (!isUserSelectionAllowed()) { + throw new IllegalStateException("Client tried to update selection" + + " although user selection is disallowed"); + } if (isKeySelected(key)) { return; } @@ -224,6 +228,10 @@ public class SingleSelectionModelImpl<T> extends AbstractSelectionModel<T> } } + private boolean isUserSelectionAllowed() { + return getState(false).selectionAllowed; + } + /** * Gets a wrapper for using this grid as a single select in a binder. * @@ -267,15 +275,12 @@ public class SingleSelectionModelImpl<T> extends AbstractSelectionModel<T> @Override public void setReadOnly(boolean readOnly) { - // TODO support read only when grid is used in binder ? - throw new UnsupportedOperationException( - "Read only is not supported for Grid."); + getState().selectionAllowed = readOnly; } @Override public boolean isReadOnly() { - throw new UnsupportedOperationException( - "Read only is not supported for Grid."); + return isUserSelectionAllowed(); } }; } diff --git a/server/src/test/java/com/vaadin/tests/components/grid/GridMultiSelectionModelTest.java b/server/src/test/java/com/vaadin/tests/components/grid/GridMultiSelectionModelTest.java index 3a57311bd7..aa601c67be 100644 --- a/server/src/test/java/com/vaadin/tests/components/grid/GridMultiSelectionModelTest.java +++ b/server/src/test/java/com/vaadin/tests/components/grid/GridMultiSelectionModelTest.java @@ -76,6 +76,21 @@ public class GridMultiSelectionModelTest { } } + private static class TestMultiSelectionModel + extends MultiSelectionModelImpl<Object> { + + public TestMultiSelectionModel() { + getState(false).selectionAllowed = false; + } + + @Override + protected void updateSelection(Set<Object> addedItems, + Set<Object> removedItems, boolean userOriginated) { + super.updateSelection(addedItems, removedItems, userOriginated); + } + + } + @Before public void setUp() { grid = new Grid<>(); @@ -98,6 +113,13 @@ public class GridMultiSelectionModelTest { } @Test(expected = IllegalStateException.class) + public void throwExcpetionWhenSelectionIsDisallowed() { + TestMultiSelectionModel model = new TestMultiSelectionModel(); + model.updateSelection(Collections.emptySet(), Collections.emptySet(), + true); + } + + @Test(expected = IllegalStateException.class) public void selectionModelChanged_usingPreviousSelectionModel_throws() { grid.setSelectionMode(SelectionMode.SINGLE); diff --git a/server/src/test/java/com/vaadin/tests/components/grid/GridSingleSelectionModelTest.java b/server/src/test/java/com/vaadin/tests/components/grid/GridSingleSelectionModelTest.java index 06ceb8bfc3..bae7c9c19b 100644 --- a/server/src/test/java/com/vaadin/tests/components/grid/GridSingleSelectionModelTest.java +++ b/server/src/test/java/com/vaadin/tests/components/grid/GridSingleSelectionModelTest.java @@ -48,6 +48,19 @@ public class GridSingleSelectionModelTest { } + private static class TestSingleSelectionModel + extends SingleSelectionModelImpl<Object> { + + public TestSingleSelectionModel() { + getState(false).selectionAllowed = false; + } + + @Override + protected void setSelectedFromClient(String key) { + super.setSelectedFromClient(key); + } + } + private List<Person> selectionChanges; private Grid<Person> grid; private SingleSelectionModelImpl<Person> selectionModel; @@ -65,6 +78,12 @@ public class GridSingleSelectionModelTest { } @Test(expected = IllegalStateException.class) + public void throwExceptionWhenSelectionIsDisallowed() { + TestSingleSelectionModel model = new TestSingleSelectionModel(); + model.setSelectedFromClient("foo"); + } + + @Test(expected = IllegalStateException.class) public void selectionModelChanged_usingPreviousSelectionModel_throws() { grid.setSelectionMode(SelectionMode.MULTI); |