diff options
author | Pekka Hyvönen <pekka@vaadin.com> | 2016-11-29 16:29:39 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-11-30 12:58:00 +0000 |
commit | 429a1b99f3f91dd10aa951e38b90d87e0a4caa44 (patch) | |
tree | 077841f04bda1ce334adfdf36415bfd69fd316b8 /server | |
parent | 2de9aca9e5f9fe0d637f05c7198a93f93e0689e1 (diff) | |
download | vaadin-framework-429a1b99f3f91dd10aa951e38b90d87e0a4caa44.tar.gz vaadin-framework-429a1b99f3f91dd10aa951e38b90d87e0a4caa44.zip |
NoSelectionModel for Grid
Fixes vaadin/framework8-issues#518
Change-Id: Ice6bdef03c3b62dd2c74376f98cfb8004dfa0bbf
Diffstat (limited to 'server')
-rw-r--r-- | server/src/main/java/com/vaadin/ui/components/grid/NoSelectionModel.java | 71 | ||||
-rw-r--r-- | server/src/test/java/com/vaadin/tests/components/grid/GridNoSelectionModelTest.java | 69 |
2 files changed, 140 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/ui/components/grid/NoSelectionModel.java b/server/src/main/java/com/vaadin/ui/components/grid/NoSelectionModel.java new file mode 100644 index 0000000000..047ee54491 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/components/grid/NoSelectionModel.java @@ -0,0 +1,71 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.ui.components.grid; + +import java.util.Collections; +import java.util.Optional; +import java.util.Set; + +import com.vaadin.server.AbstractExtension; +import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.GridSelectionModel; + +/** + * Selection model that doesn't allow selecting anything from the grid. + * + * @author Vaadin Ltd + * + * @since 8.0 + * + * @param <T> + * the type of items in the grid + */ +public class NoSelectionModel<T> extends AbstractExtension + implements GridSelectionModel<T> { + + /** + * Creates a new no selection model and attaches it for the given grid. + * + * @param grid + * the grid to bind the selection model into + */ + public NoSelectionModel(Grid<T> grid) { + extend(grid); + } + + @Override + public Set<T> getSelectedItems() { + return Collections.emptySet(); + } + + @Override + public Optional<T> getFirstSelectedItem() { + return Optional.empty(); + } + + @Override + public void select(T item) { + } + + @Override + public void deselect(T item) { + } + + @Override + public void deselectAll() { + } + +}
\ No newline at end of file diff --git a/server/src/test/java/com/vaadin/tests/components/grid/GridNoSelectionModelTest.java b/server/src/test/java/com/vaadin/tests/components/grid/GridNoSelectionModelTest.java new file mode 100644 index 0000000000..15456bd950 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/components/grid/GridNoSelectionModelTest.java @@ -0,0 +1,69 @@ +package com.vaadin.tests.components.grid; + +import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.Optional; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.server.data.provider.bov.Person; +import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.GridSelectionModel; +import com.vaadin.ui.components.grid.MultiSelectionModelImpl; +import com.vaadin.ui.components.grid.NoSelectionModel; +import com.vaadin.ui.components.grid.SingleSelectionModelImpl; + +public class GridNoSelectionModelTest { + + public static final Person PERSON_C = new Person("c", 3); + public static final Person PERSON_B = new Person("b", 2); + public static final Person PERSON_A = new Person("a", 1); + + private Grid<Person> grid; + private GridSelectionModel<Person> model; + + @Before + public void setUp() { + grid = new Grid<>(); + grid.setItems(PERSON_A, PERSON_B, PERSON_C); + + model = new NoSelectionModel<>(grid); + grid.setSelectionModel(model); + } + + @Test + public void select() { + model.select(PERSON_A); + + Assert.assertFalse(model.isSelected(PERSON_A)); + Assert.assertEquals(0, model.getSelectedItems().size()); + Assert.assertEquals(Optional.empty(), model.getFirstSelectedItem()); + + model.select(PERSON_B); + + Assert.assertFalse(model.isSelected(PERSON_B)); + Assert.assertEquals(0, model.getSelectedItems().size()); + Assert.assertEquals(Optional.empty(), model.getFirstSelectedItem()); + } + + @Test + public void changingToSingleSelectionModel() { + grid.setSelectionModel(new SingleSelectionModelImpl<>(grid)); + + grid.getSelectionModel().select(PERSON_B); + Assert.assertEquals(PERSON_B, + grid.getSelectionModel().getFirstSelectedItem().get()); + } + + @Test + public void changingToMultiSelectionModel() { + grid.setSelectionModel(new MultiSelectionModelImpl<>(grid)); + + grid.getSelectionModel().select(PERSON_B); + Assert.assertEquals(new LinkedHashSet<>(Arrays.asList(PERSON_B)), + grid.getSelectionModel().getSelectedItems()); + } + +} |