diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-09-01 10:31:19 +0300 |
---|---|---|
committer | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-09-06 10:38:26 +0300 |
commit | fbb55ac4fdf09103cf7e999b321812bea033a981 (patch) | |
tree | 396e92361207f579c6d71b56db6ebc0eae9ae8fc /server | |
parent | 2cafb5f128f718399b112a0c87e9a3d303018371 (diff) | |
download | vaadin-framework-fbb55ac4fdf09103cf7e999b321812bea033a981.tar.gz vaadin-framework-fbb55ac4fdf09103cf7e999b321812bea033a981.zip |
Clean up old Grid selection models
This patch removes old Grid selection model APIs
in favor of the new common SelectionModel API.
Change-Id: Iab8f2921930a575012c7da6226811d14a7145271
Diffstat (limited to 'server')
5 files changed, 66 insertions, 13 deletions
diff --git a/server/src/main/java/com/vaadin/data/selection/SingleSelection.java b/server/src/main/java/com/vaadin/data/selection/SingleSelection.java index fb6d3b1cac..54f0240b37 100644 --- a/server/src/main/java/com/vaadin/data/selection/SingleSelection.java +++ b/server/src/main/java/com/vaadin/data/selection/SingleSelection.java @@ -88,8 +88,8 @@ public class SingleSelection<T> extends AbstractSelectionModel<T> * @see SingleSelectionChange */ @FunctionalInterface - public interface SingleSelectionListener<T> extends - EventListener<SingleSelectionChange<T>> { + public interface SingleSelectionListener<T> + extends EventListener<SingleSelectionChange<T>> { @Override public void accept(SingleSelectionChange<T> event); @@ -112,12 +112,14 @@ public class SingleSelection<T> extends AbstractSelectionModel<T> @Override public void select(String key) { - doSelect(getData(key), true); + if (!Objects.equals(selectedItem, getData(key))) { + doSelect(getData(key), true); + } } @Override public void deselect(String key) { - if (getData(key).equals(selectedItem)) { + if (Objects.equals(selectedItem, getData(key))) { doSelect(null, true); } } @@ -133,14 +135,15 @@ public class SingleSelection<T> extends AbstractSelectionModel<T> } @Override - public void select(T value) { - doSelect(value, false); + public void select(T item) { + doSelect(item, false); } @Override public void deselect(T value) { - if(Objects.equals(selectedItem,value)) + if (Objects.equals(selectedItem, value)) { doSelect(null, false); + } } @Override @@ -172,16 +175,22 @@ public class SingleSelection<T> extends AbstractSelectionModel<T> * Selects the given item or deselects the current one if given * {@code null}. * - * @param value + * @param item * the item to select or {@code null} to deselect * @param userOriginated * {@code true} if this event originates from the client, * {@code false} otherwise. */ - protected void doSelect(T value, boolean userOriginated) { - if (!Objects.equals(value, this.selectedItem)) { - this.selectedItem = value; - fireEvent(new SingleSelectionChange<>(getParent(), value, + protected void doSelect(T item, boolean userOriginated) { + if (!Objects.equals(item, selectedItem)) { + if (selectedItem != null) { + refresh(selectedItem); + } + selectedItem = item; + if (selectedItem != null) { + refresh(selectedItem); + } + fireEvent(new SingleSelectionChange<>(getParent(), selectedItem, userOriginated)); } } diff --git a/server/src/main/java/com/vaadin/server/data/DataCommunicator.java b/server/src/main/java/com/vaadin/server/data/DataCommunicator.java index 8c92ad50e3..5aceb9b305 100644 --- a/server/src/main/java/com/vaadin/server/data/DataCommunicator.java +++ b/server/src/main/java/com/vaadin/server/data/DataCommunicator.java @@ -373,6 +373,11 @@ public class DataCommunicator<T> extends AbstractExtension { * updated data object */ public void refresh(T data) { + if (!handler.getActiveData().contains(data)) { + // Item is not currently available at the client-side + return; + } + if (updatedData.isEmpty()) { markAsDirty(); } diff --git a/server/src/main/java/com/vaadin/ui/AbstractListing.java b/server/src/main/java/com/vaadin/ui/AbstractListing.java index 2854903463..5996b52c9b 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractListing.java +++ b/server/src/main/java/com/vaadin/ui/AbstractListing.java @@ -160,6 +160,11 @@ public abstract class AbstractListing<T, SELECTIONMODEL extends SelectionModel<T * the selection model to use, not null */ protected void setSelectionModel(SELECTIONMODEL model) { + if (selectionModel != null) { + throw new IllegalStateException( + "A selection model can't be changed."); + } + Objects.requireNonNull(model, "selection model cannot be null"); selectionModel = model; } diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index ae7c550172..387cd22275 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -34,9 +34,9 @@ import java.util.stream.Stream; import com.vaadin.data.selection.SingleSelection; import com.vaadin.server.AbstractExtension; import com.vaadin.server.KeyMapper; +import com.vaadin.server.data.DataGenerator; import com.vaadin.server.data.DataSource; import com.vaadin.server.data.SortOrder; -import com.vaadin.server.data.DataGenerator; import com.vaadin.shared.MouseEventDetails; import com.vaadin.shared.data.DataCommunicatorConstants; import com.vaadin.shared.data.selection.SelectionModel; diff --git a/server/src/test/java/com/vaadin/tests/components/grid/GridSelectionTest.java b/server/src/test/java/com/vaadin/tests/components/grid/GridSelectionTest.java new file mode 100644 index 0000000000..0522e26ac9 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/components/grid/GridSelectionTest.java @@ -0,0 +1,34 @@ +package com.vaadin.tests.components.grid; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.ui.Grid; + +public class GridSelectionTest { + + Grid<String> grid; + + @Before + public void setUp() { + grid = new Grid<>(); + grid.setItems("Foo", "Bar"); + } + + @Test + public void testGridWithSingleSelection() { + Assert.assertFalse(grid.isSelected("Foo")); + grid.select("Foo"); + Assert.assertTrue(grid.isSelected("Foo")); + Assert.assertEquals(1, grid.getSelectedItems().size()); + Assert.assertEquals("Foo", grid.getSelectedItems().iterator().next()); + grid.select("Bar"); + Assert.assertFalse(grid.isSelected("Foo")); + Assert.assertTrue(grid.isSelected("Bar")); + grid.deselect("Bar"); + Assert.assertFalse(grid.isSelected("Bar")); + Assert.assertEquals(0, grid.getSelectedItems().size()); + } + +} |