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 | |
parent | 2de9aca9e5f9fe0d637f05c7198a93f93e0689e1 (diff) | |
download | vaadin-framework-429a1b99f3f91dd10aa951e38b90d87e0a4caa44.tar.gz vaadin-framework-429a1b99f3f91dd10aa951e38b90d87e0a4caa44.zip |
NoSelectionModel for Grid
Fixes vaadin/framework8-issues#518
Change-Id: Ice6bdef03c3b62dd2c74376f98cfb8004dfa0bbf
6 files changed, 346 insertions, 6 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/NoSelectionModelConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/NoSelectionModelConnector.java new file mode 100644 index 0000000000..383fae2882 --- /dev/null +++ b/client/src/main/java/com/vaadin/client/connectors/grid/NoSelectionModelConnector.java @@ -0,0 +1,61 @@ +/* + * 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.client.connectors.grid; + +import com.vaadin.client.ServerConnector; +import com.vaadin.client.extensions.AbstractExtensionConnector; +import com.vaadin.client.widget.grid.selection.SelectionModel; +import com.vaadin.shared.ui.Connect; + +import elemental.json.JsonObject; + +/** + * Connector for grids selection model that doesn't allow selecting anything. + * + * @author Vaadin Ltd + * + * @since 8.0 + */ +@Connect(com.vaadin.ui.components.grid.NoSelectionModel.class) +public class NoSelectionModelConnector extends AbstractExtensionConnector { + + @Override + protected void extend(ServerConnector target) { + assert target instanceof GridConnector : "NoSelectionModelConnector cannot extend anything else than Grid."; + + ((GridConnector) target).getWidget() + .setSelectionModel(new SelectionModel<JsonObject>() { + + @Override + public void select(JsonObject item) { + } + + @Override + public void deselect(JsonObject item) { + } + + @Override + public boolean isSelected(JsonObject item) { + return false; + } + + @Override + public void deselectAll() { + } + }); + } + +} 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()); + } + +} diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java b/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java index c2628598c5..6d5a678ca7 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java @@ -42,6 +42,7 @@ import com.vaadin.ui.TextField; import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.components.grid.MultiSelectionModelImpl; import com.vaadin.ui.components.grid.MultiSelectionModelImpl.SelectAllCheckBoxVisible; +import com.vaadin.ui.components.grid.NoSelectionModel; import com.vaadin.ui.components.grid.SingleSelectionModelImpl; import com.vaadin.ui.renderers.DateRenderer; import com.vaadin.ui.renderers.HtmlRenderer; @@ -495,6 +496,10 @@ public class GridBasics extends AbstractTestUIWithLog { selectionModelItem.addItem("multi", menuItem -> { switchToMultiSelect(); }); + selectionModelItem.addItem("none", menuItem -> { + selectionListenerRegistration.remove(); + grid.setSelectionModel(new NoSelectionModel<>(grid)); + }); selectionModelItem.addItem("Select All", menuItem -> { switchToMultiSelect(); diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridNoSelectionTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridNoSelectionTest.java new file mode 100644 index 0000000000..d0e36dec6d --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridNoSelectionTest.java @@ -0,0 +1,136 @@ +package com.vaadin.tests.components.grid; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.Keys; +import org.openqa.selenium.interactions.Actions; + +import com.vaadin.testbench.customelements.GridElement; +import com.vaadin.tests.components.grid.basics.GridBasicsTest; + +public class GridNoSelectionTest extends GridBasicsTest { + + @Test + public void clickToSelectDoesNothing() { + setSelectionModelNone(); + + verifyClickSelectDoesNothing(); + } + + @Test + public void spaceBarSelectDoesNothing() { + setSelectionModelNone(); + + verifyKeyboardSelectionNotAllowed(); + } + + @Test + public void serverSideSelectDoesNothing() { + toggleFirstRowSelection(); + + assertTrue(getGridElement().getRow(0).isSelected()); + + setSelectionModelNone(); + + toggleFirstRowSelection(); + + assertFalse(getGridElement().getRow(0).isSelected()); + } + + @Test + public void changingSelectionModels_fromMulti() { + setSelectionModelMulti(); + + getGridElement().getCell(0, 0).click(); + assertTrue(getGridElement().getRow(0).isSelected()); + + getGridElement().scrollToRow(50); + getGridElement().getCell(49, 0).click(); + assertTrue(getGridElement().getRow(49).isSelected()); + + setSelectionModelNone(); + + assertFalse(getGridElement().getRow(0).isSelected()); + + verifyClickSelectDoesNothing(); + verifyKeyboardSelectionNotAllowed(); + + getGridElement().scrollToRow(50); + assertFalse(getGridElement().getRow(49).isSelected()); + } + + @Test + public void changingSelectionModels_fromMultiAllSelected() { + setSelectionModelMulti(); + + getGridHeaderRowCells().get(0).click(); // select all click + + assertTrue(getDefaultColumnHeader(0).findElement(By.tagName("input")) + .isSelected()); + assertTrue(getGridElement().getRow(0).isSelected()); + assertTrue(getGridElement().getRow(1).isSelected()); + assertTrue(getGridElement().getRow(10).isSelected()); + + setSelectionModelNone(); + + assertEquals(0, getDefaultColumnHeader(0) + .findElements(By.tagName("input")).size()); + assertFalse(getGridElement().getRow(0).isSelected()); + assertFalse(getGridElement().getRow(1).isSelected()); + assertFalse(getGridElement().getRow(10).isSelected()); + } + + @Test + public void changingSelectionModels_fromSingle() { + // this is the same as default + getGridElement().getCell(3, 0).click(); + assertTrue(getGridElement().getRow(3).isSelected()); + + setSelectionModelNone(); + + assertFalse(getGridElement().getRow(3).isSelected()); + + verifyClickSelectDoesNothing(); + verifyKeyboardSelectionNotAllowed(); + } + + protected void verifyClickSelectDoesNothing() { + getGridElement().getCell(0, 0).click(); + + assertFalse("Grid row should not be selected", + getGridElement().getRow(0).isSelected()); + + getGridElement().getCell(2, 2).click(); + + assertFalse("Grid row should not be selected", + getGridElement().getRow(2).isSelected()); + } + + protected void verifyKeyboardSelectionNotAllowed() { + GridElement grid = getGridElement(); + grid.getCell(3, 1).click(); + + assertFalse("Grid should not allow selecting", + grid.getRow(3).isSelected()); + + new Actions(getDriver()).sendKeys(Keys.SPACE).perform(); + + assertFalse("Grid should not allow selecting with space key", + grid.getRow(3).isSelected()); + + new Actions(getDriver()).sendKeys(Keys.SPACE).perform(); + + assertFalse("Grid should not allow selecting", + grid.getRow(3).isSelected()); + + new Actions(getDriver()).sendKeys(Keys.ARROW_DOWN).perform(); + new Actions(getDriver()).sendKeys(Keys.SPACE).perform(); + assertFalse("Grid should not allow selecting", + grid.getRow(4).isSelected()); + } + +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridBasicsTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridBasicsTest.java index 20e94cb7a6..5a88be9690 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridBasicsTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridBasicsTest.java @@ -235,6 +235,10 @@ public abstract class GridBasicsTest extends MultiBrowserTest { selectMenuPath("Component", "State", "Selection model", "single"); } + protected void setSelectionModelNone() { + selectMenuPath("Component", "State", "Selection model", "none"); + } + protected void setSelectAllCheckBoxVisible() { selectMenuPath("Component", "State", "Selection model", "SelectAllCheckbox: Visible"); @@ -255,10 +259,4 @@ public abstract class GridBasicsTest extends MultiBrowserTest { return header.findElement(By.tagName("input")); } - - // TODO enable once select model none is added - // protected void setSelectionModelNone() { - // selectMenuPath("Component", "State", "Selection model", "none"); - // } - } |