diff options
author | Pekka Hyvönen <pekka@vaadin.com> | 2016-12-09 09:14:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-09 09:14:56 +0200 |
commit | 22063cfe1a027057e523ccd85937162af43edfd8 (patch) | |
tree | 21fae6cecb4c32de02787b4b1ee5cff284d5754b | |
parent | a95dadcde5a223aacc3005b298d66e00419454e8 (diff) | |
download | vaadin-framework-22063cfe1a027057e523ccd85937162af43edfd8.tar.gz vaadin-framework-22063cfe1a027057e523ccd85937162af43edfd8.zip |
Add a generic selection listener to Grid & SelectionModels (#105)
* Add a generic selection listener to Grid & SelectionModels
Fixes some inconsistent event naming and invalid javadocs.
Fixes vaadin/framework8-issues#541
22 files changed, 338 insertions, 66 deletions
diff --git a/server/src/main/java/com/vaadin/data/SelectionModel.java b/server/src/main/java/com/vaadin/data/SelectionModel.java index 310f79f7d0..29c32c7698 100644 --- a/server/src/main/java/com/vaadin/data/SelectionModel.java +++ b/server/src/main/java/com/vaadin/data/SelectionModel.java @@ -24,6 +24,9 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Stream; +import com.vaadin.event.selection.SelectionListener; +import com.vaadin.shared.Registration; + /** * Models the selection logic of a {@code Listing} component. Determines how * items can be selected and deselected. @@ -257,4 +260,14 @@ public interface SelectionModel<T> extends Serializable { public default boolean isSelected(T item) { return getSelectedItems().contains(item); } + + /** + * Adds a generic listener to this selection model, accepting both single + * and multiselection events. + * + * @param listener + * the listener to add + * @return a registration handle for removing the listener + */ + public Registration addSelectionListener(SelectionListener<T> listener); } diff --git a/server/src/main/java/com/vaadin/event/selection/MultiSelectionEvent.java b/server/src/main/java/com/vaadin/event/selection/MultiSelectionEvent.java index e08ba491af..3c89a53f50 100644 --- a/server/src/main/java/com/vaadin/event/selection/MultiSelectionEvent.java +++ b/server/src/main/java/com/vaadin/event/selection/MultiSelectionEvent.java @@ -145,4 +145,14 @@ public class MultiSelectionEvent<T> extends ValueChangeEvent<Set<T>> public MultiSelect<T> getSource() { return (MultiSelect<T>) super.getSource(); } + + /** + * {@inheritDoc} + * <p> + * This is the same as {@link #getValue()}. + */ + @Override + public Set<T> getAllSelectedItems() { + return getValue(); + } } diff --git a/server/src/main/java/com/vaadin/event/selection/SelectionEvent.java b/server/src/main/java/com/vaadin/event/selection/SelectionEvent.java index 7240886f2e..5389014f93 100644 --- a/server/src/main/java/com/vaadin/event/selection/SelectionEvent.java +++ b/server/src/main/java/com/vaadin/event/selection/SelectionEvent.java @@ -17,6 +17,7 @@ package com.vaadin.event.selection; import java.io.Serializable; import java.util.Optional; +import java.util.Set; /** * A selection event that unifies the way to access to selection event for multi @@ -33,11 +34,30 @@ public interface SelectionEvent<T> extends Serializable { /** * Get first selected data item. * <p> - * This is the same as {@link SingleSelectionChange#getSelectedItem()} in + * This is the same as {@link SingleSelectionEvent#getSelectedItem()} in * case of single selection and the first selected item from - * {@link MultiSelectionChangeEvent#getNewSelection()} in case of multi selection. - * + * {@link MultiSelectionEvent#getNewSelection()} in case of multi selection. + * * @return the first selected item. */ Optional<T> getFirstSelected(); + + /** + * Gets all the currently selected items. + * <p> + * This method applies more to multiselection - for single select it returns + * either an empty set or a set containing the only selected item. + * + * @return return all the selected items, if any, never {@code null} + */ + Set<T> getAllSelectedItems(); + + /** + * Returns whether this selection event was triggered by user interaction, + * on the client side, or programmatically, on the server side. + * + * @return {@code true} if this event originates from the client, + * {@code false} otherwise. + */ + boolean isUserOriginated(); } diff --git a/server/src/main/java/com/vaadin/event/selection/SelectionListener.java b/server/src/main/java/com/vaadin/event/selection/SelectionListener.java new file mode 100644 index 0000000000..e26e02315f --- /dev/null +++ b/server/src/main/java/com/vaadin/event/selection/SelectionListener.java @@ -0,0 +1,39 @@ +/* + * 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.event.selection; + +import com.vaadin.server.SerializableConsumer; + +/** + * A listener for {@code SelectionEvent}. + * <p> + * This is a generic listener for both type of selections, single and + * multiselect. + * + * @author Vaadin Ltd. + * + * @param <T> + * the type of the selected item + * + * @see SelectionEvent + * + * @since 8.0 + */ +@FunctionalInterface +public interface SelectionListener<T> + extends SerializableConsumer<SelectionEvent<T>> { + +} diff --git a/server/src/main/java/com/vaadin/event/selection/SingleSelectionEvent.java b/server/src/main/java/com/vaadin/event/selection/SingleSelectionEvent.java index c6c9ce6b6c..b9f03891a5 100644 --- a/server/src/main/java/com/vaadin/event/selection/SingleSelectionEvent.java +++ b/server/src/main/java/com/vaadin/event/selection/SingleSelectionEvent.java @@ -15,7 +15,9 @@ */ package com.vaadin.event.selection; +import java.util.Collections; import java.util.Optional; +import java.util.Set; import com.vaadin.data.HasValue.ValueChangeEvent; import com.vaadin.ui.AbstractSingleSelect; @@ -96,4 +98,14 @@ public class SingleSelectionEvent<T> extends ValueChangeEvent<T> public Optional<T> getFirstSelected() { return getSelectedItem(); } + + @Override + public Set<T> getAllSelectedItems() { + Optional<T> selectedItem = getSelectedItem(); + if (selectedItem.isPresent()) { + return Collections.singleton(selectedItem.get()); + } else { + return Collections.emptySet(); + } + } } diff --git a/server/src/main/java/com/vaadin/event/selection/SingleSelectionListener.java b/server/src/main/java/com/vaadin/event/selection/SingleSelectionListener.java index eb054e250b..ef34435678 100644 --- a/server/src/main/java/com/vaadin/event/selection/SingleSelectionListener.java +++ b/server/src/main/java/com/vaadin/event/selection/SingleSelectionListener.java @@ -1,12 +1,12 @@ /* * 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 @@ -19,15 +19,15 @@ import java.io.Serializable; import java.util.function.Consumer; /** - * A listener for {@code SingleSelectionChange} events. - * + * A listener for {@code SingleSelectionEvent}. + * * @author Vaadin Ltd. - * + * * @param <T> * the type of the selected item - * + * * @see SingleSelectionEvent - * + * * @since 8.0 */ @FunctionalInterface diff --git a/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java b/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java index 81126f3189..1ff77b7f1a 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java +++ b/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java @@ -97,13 +97,13 @@ public abstract class AbstractSingleSelect<T> extends AbstractListing<T> /** * Adds a selection listener to this select. The listener is called when the - * value of this select is changed either by the user or programmatically. + * selection is changed either by the user or programmatically. * * @param listener - * the value change listener, not null + * the selection listener, not null * @return a registration for the listener */ - public Registration addSelectionChangeListener( + public Registration addSelectionListener( SingleSelectionListener<T> listener) { return addListener(SingleSelectionEvent.class, listener, SELECTION_CHANGE_METHOD); @@ -168,7 +168,7 @@ public abstract class AbstractSingleSelect<T> extends AbstractListing<T> @Override public Registration addValueChangeListener( HasValue.ValueChangeListener<T> listener) { - return addSelectionChangeListener(event -> listener.accept( + return addSelectionListener(event -> listener.accept( new ValueChangeEvent<>(this, event.isUserOriginated()))); } diff --git a/server/src/main/java/com/vaadin/ui/ComboBox.java b/server/src/main/java/com/vaadin/ui/ComboBox.java index 7e54e34b0b..b85a721c54 100644 --- a/server/src/main/java/com/vaadin/ui/ComboBox.java +++ b/server/src/main/java/com/vaadin/ui/ComboBox.java @@ -558,7 +558,7 @@ public class ComboBox<T> extends AbstractSingleSelect<T> @Override public Registration addValueChangeListener( HasValue.ValueChangeListener<T> listener) { - return addSelectionChangeListener(event -> { + return addSelectionListener(event -> { listener.accept(new ValueChangeEvent<>(event.getComponent(), this, event.isUserOriginated())); }); diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index acb93e9fe3..7d9913409e 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -46,6 +46,9 @@ import com.vaadin.data.SelectionModel; import com.vaadin.event.ConnectorEvent; import com.vaadin.event.ContextClickEvent; import com.vaadin.event.EventListener; +import com.vaadin.event.selection.MultiSelectionListener; +import com.vaadin.event.selection.SelectionListener; +import com.vaadin.event.selection.SingleSelectionListener; import com.vaadin.server.EncodeResult; import com.vaadin.server.Extension; import com.vaadin.server.JsonCodec; @@ -235,6 +238,31 @@ public class Grid<T> extends AbstractListing<T> * @return the single select wrapper */ SingleSelect<T> asSingleSelect(); + + /** + * {@inheritDoc} + * <p> + * Use {@link #addSingleSelectionListener(SingleSelectionListener)} for + * more specific single selection event. + * + * @see #addSingleSelectionListener(SingleSelectionListener) + */ + @Override + public default Registration addSelectionListener( + SelectionListener<T> listener) { + return addSingleSelectionListener(e -> listener.accept(e)); + } + + /** + * Adds a single selection listener that is called when the value of + * this select is changed either by the user or programmatically. + * + * @param listener + * the value change listener, not {@code null} + * @return a registration for the listener + */ + public Registration addSingleSelectionListener( + SingleSelectionListener<T> listener); } /** @@ -253,6 +281,31 @@ public class Grid<T> extends AbstractListing<T> * @return the multiselect wrapper */ MultiSelect<T> asMultiSelect(); + + /** + * {@inheritDoc} + * <p> + * Use {@link #addMultiSelectionListener(MultiSelectionListener)} for + * more specific event on multiselection. + * + * @see #addMultiSelectionListener(MultiSelectionListener) + */ + @Override + public default Registration addSelectionListener( + SelectionListener<T> listener) { + return addMultiSelectionListener(e -> listener.accept(e)); + } + + /** + * Adds a selection listener that will be called when the selection is + * changed either by the user or programmatically. + * + * @param listener + * the value change listener, not {@code null} + * @return a registration for the listener + */ + public Registration addMultiSelectionListener( + MultiSelectionListener<T> listener); } /** @@ -3136,6 +3189,7 @@ public class Grid<T> extends AbstractListing<T> if (selectionModel != null) { // null when called from constructor selectionModel.remove(); } + selectionModel = model; if (selectionModel instanceof AbstractListingExtension) { @@ -3143,6 +3197,7 @@ public class Grid<T> extends AbstractListing<T> } else { addExtension(selectionModel); } + } /** @@ -3178,6 +3233,33 @@ public class Grid<T> extends AbstractListing<T> return model; } + /** + * Adds a selection listener to the current selection model. + * <p> + * <em>NOTE:</em> If selection mode is switched with + * {@link #setSelectionMode(SelectionMode)}, then this listener is not + * triggered anymore when selection changes! + * <p> + * This is a shorthand for + * {@code grid.getSelectionModel().addSelectionListener()}. To get more + * detailed selection events, use {@link #getSelectionModel()} and either + * {@link SingleSelectionModel#addSingleSelectionListener(SingleSelectionListener)} + * or + * {@link MultiSelectionModel#addMultiSelectionListener(MultiSelectionListener)} + * depending on the used selection mode. + * + * @param listener + * the listener to add + * @return a registration handle to remove the listener + * @throws UnsupportedOperationException + * if selection has been disabled with + * {@link SelectionMode.NONE} + */ + public Registration addSelectionListener(SelectionListener<T> listener) + throws UnsupportedOperationException { + return getSelectionModel().addSelectionListener(listener); + } + @Override protected GridState getState() { return getState(true); 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 9c2b506107..ba17315b07 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 @@ -243,19 +243,11 @@ public class MultiSelectionModelImpl<T> extends AbstractSelectionModel<T> } } - /** - * Adds a selection listener that will be called when the selection is - * changed either by the user or programmatically. - * - * @param listener - * the value change listener, not {@code null} - * @return a registration for the listener - */ - public Registration addSelectionListener( + @Override + public Registration addMultiSelectionListener( MultiSelectionListener<T> listener) { - addListener(MultiSelectionEvent.class, listener, + return addListener(MultiSelectionEvent.class, listener, SELECTION_CHANGE_METHOD); - return () -> removeListener(MultiSelectionEvent.class, listener); } @Override @@ -348,7 +340,7 @@ public class MultiSelectionModelImpl<T> extends AbstractSelectionModel<T> public Registration addSelectionListener( MultiSelectionListener<T> listener) { return MultiSelectionModelImpl.this - .addSelectionListener(listener); + .addMultiSelectionListener(listener); } }; } 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 index 674a59d6f4..e3f40db2c3 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/NoSelectionModel.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/NoSelectionModel.java @@ -19,7 +19,9 @@ import java.util.Collections; import java.util.Optional; import java.util.Set; +import com.vaadin.event.selection.SelectionListener; import com.vaadin.server.AbstractExtension; +import com.vaadin.shared.Registration; import com.vaadin.ui.Grid.GridSelectionModel; /** @@ -57,4 +59,10 @@ public class NoSelectionModel<T> extends AbstractExtension public void deselectAll() { } + @Override + public Registration addSelectionListener(SelectionListener<T> listener) { + throw new UnsupportedOperationException( + "This selection model doesn't allow selection, cannot add selection listeners to it"); + } + } 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 6db6bfa61c..108ca6669e 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 @@ -79,15 +79,8 @@ public class SingleSelectionModelImpl<T> extends AbstractSelectionModel<T> return (SingleSelectionModelState) super.getState(markAsDirty); } - /** - * Adds a selection listener to this select. The listener is called when the - * value of this select is changed either by the user or programmatically. - * - * @param listener - * the value change listener, not null - * @return a registration for the listener - */ - public Registration addSelectionListener( + @Override + public Registration addSingleSelectionListener( SingleSelectionListener<T> listener) { return addListener(SingleSelectionEvent.class, listener, SELECTION_CHANGE_METHOD); @@ -255,8 +248,9 @@ public class SingleSelectionModelImpl<T> extends AbstractSelectionModel<T> @Override public Registration addValueChangeListener( com.vaadin.data.HasValue.ValueChangeListener<T> listener) { - return SingleSelectionModelImpl.this - .addSelectionListener(event -> listener.accept(event)); + return SingleSelectionModelImpl.this.addSingleSelectionListener( + (SingleSelectionListener<T>) event -> listener + .accept(event)); } @Override diff --git a/server/src/test/java/com/vaadin/data/GridAsMultiSelectInBinder.java b/server/src/test/java/com/vaadin/data/GridAsMultiSelectInBinderTest.java index 703ed9e170..c3921ff5fc 100644 --- a/server/src/test/java/com/vaadin/data/GridAsMultiSelectInBinder.java +++ b/server/src/test/java/com/vaadin/data/GridAsMultiSelectInBinderTest.java @@ -28,7 +28,7 @@ import com.vaadin.ui.Grid.SelectionMode; import com.vaadin.ui.MultiSelect; import com.vaadin.ui.components.grid.MultiSelectionModelImpl; -public class GridAsMultiSelectInBinder +public class GridAsMultiSelectInBinderTest extends BinderTestBase<Binder<BeanWithEnums>, BeanWithEnums> { public class TestEnumSetToStringConverter implements Converter<Set<TestEnum>, String> { diff --git a/server/src/test/java/com/vaadin/data/GridAsSingleSelectInBinder.java b/server/src/test/java/com/vaadin/data/GridAsSingleSelectInBinderTest.java index e1fc394fe6..964cf0bc98 100644 --- a/server/src/test/java/com/vaadin/data/GridAsSingleSelectInBinder.java +++ b/server/src/test/java/com/vaadin/data/GridAsSingleSelectInBinderTest.java @@ -19,7 +19,7 @@ import com.vaadin.ui.Grid.SelectionMode; import com.vaadin.ui.SingleSelect; import com.vaadin.ui.components.grid.SingleSelectionModelImpl; -public class GridAsSingleSelectInBinder +public class GridAsSingleSelectInBinderTest extends BinderTestBase<Binder<Person>, Person> { private class GridWithCustomSingleSelectionModel extends Grid<Sex> { 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 aa601c67be..c2896315f2 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 @@ -103,7 +103,7 @@ public class GridMultiSelectionModelTest { oldSelectionCapture = new Capture<>(); events = new AtomicInteger(); - selectionModel.addSelectionListener(event -> { + selectionModel.addMultiSelectionListener(event -> { currentSelectionCapture .setValue(new ArrayList<>(event.getNewSelection())); oldSelectionCapture @@ -135,7 +135,7 @@ public class GridMultiSelectionModelTest { List<String> selectionChanges = new ArrayList<>(); Capture<List<String>> oldSelectionCapture = new Capture<>(); ((MultiSelectionModelImpl<String>) customGrid.getSelectionModel()) - .addSelectionListener(e -> { + .addMultiSelectionListener(e -> { selectionChanges.addAll(e.getValue()); oldSelectionCapture .setValue(new ArrayList<>(e.getOldSelection())); @@ -578,7 +578,7 @@ public class GridMultiSelectionModelTest { Registration registration = Mockito.mock(Registration.class); MultiSelectionModelImpl<String> model = new MultiSelectionModelImpl<String>() { @Override - public Registration addSelectionListener( + public Registration addMultiSelectionListener( MultiSelectionListener<String> listener) { selectionListener.set(listener); return registration; @@ -595,10 +595,11 @@ public class GridMultiSelectionModelTest { grid.setItems("foo", "bar"); AtomicReference<MultiSelectionEvent<String>> event = new AtomicReference<>(); - Registration actualRegistration = model.addSelectionListener(evt -> { - Assert.assertNull(event.get()); - event.set(evt); - }); + Registration actualRegistration = model + .addMultiSelectionListener(evt -> { + Assert.assertNull(event.get()); + event.set(evt); + }); Assert.assertSame(registration, actualRegistration); selectionListener.get().accept(new MultiSelectionEvent<>(grid, 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 bae7c9c19b..9dd7d7e27a 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 @@ -73,8 +73,8 @@ public class GridSingleSelectionModelTest { .getSelectionModel(); selectionChanges = new ArrayList<>(); - selectionModel - .addSelectionListener(e -> selectionChanges.add(e.getValue())); + selectionModel.addSingleSelectionListener( + e -> selectionChanges.add(e.getValue())); } @Test(expected = IllegalStateException.class) @@ -97,7 +97,8 @@ public class GridSingleSelectionModelTest { List<String> selectionChanges = new ArrayList<>(); ((SingleSelectionModelImpl<String>) customGrid.getSelectionModel()) - .addSelectionListener(e -> selectionChanges.add(e.getValue())); + .addSingleSelectionListener( + e -> selectionChanges.add(e.getValue())); customGrid.getSelectionModel().select("Foo"); assertEquals("Foo", @@ -292,7 +293,7 @@ public class GridSingleSelectionModelTest { String value = "foo"; SingleSelectionModelImpl<String> select = new SingleSelectionModelImpl<String>() { @Override - public Registration addSelectionListener( + public Registration addSingleSelectionListener( SingleSelectionListener<String> listener) { selectionListener.set(listener); return registration; @@ -305,10 +306,11 @@ public class GridSingleSelectionModelTest { }; AtomicReference<ValueChangeEvent<?>> event = new AtomicReference<>(); - Registration actualRegistration = select.addSelectionListener(evt -> { - Assert.assertNull(event.get()); - event.set(evt); - }); + Registration actualRegistration = select + .addSingleSelectionListener(evt -> { + Assert.assertNull(event.get()); + event.set(evt); + }); Assert.assertSame(registration, actualRegistration); selectionListener.get().accept(new SingleSelectionEvent<>(grid, diff --git a/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java b/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java index 942228ed34..d477a731cf 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java @@ -1,13 +1,21 @@ package com.vaadin.tests.server.component.grid; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import java.util.Optional; + +import org.easymock.Capture; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import com.vaadin.event.selection.SelectionEvent; import com.vaadin.server.SerializableFunction; import com.vaadin.shared.ui.grid.HeightMode; import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.SelectionMode; import com.vaadin.ui.renderers.NumberRenderer; public class GridTest { @@ -78,4 +86,95 @@ public class GridTest { public void testGridMultipleColumnsWithSameIdentifier() { grid.addColumn("foo", t -> t); } + + @Test + public void testAddSelectionListener_singleSelectMode() { + grid.setItems("foo", "bar", "baz"); + + Capture<SelectionEvent<String>> eventCapture = new Capture<>(); + + grid.addSelectionListener(event -> eventCapture.setValue(event)); + + grid.getSelectionModel().select("foo"); + + SelectionEvent<String> event = eventCapture.getValue(); + assertNotNull(event); + assertFalse(event.isUserOriginated()); + assertEquals("foo", event.getFirstSelected().get()); + assertEquals("foo", + event.getAllSelectedItems().stream().findFirst().get()); + + grid.getSelectionModel().select("bar"); + + event = eventCapture.getValue(); + assertNotNull(event); + assertFalse(event.isUserOriginated()); + assertEquals("bar", event.getFirstSelected().get()); + assertEquals("bar", + event.getAllSelectedItems().stream().findFirst().get()); + + grid.getSelectionModel().deselect("bar"); + + event = eventCapture.getValue(); + assertNotNull(event); + assertFalse(event.isUserOriginated()); + assertEquals(Optional.empty(), event.getFirstSelected()); + assertEquals(0, event.getAllSelectedItems().size()); + } + + @Test + public void testAddSelectionListener_multiSelectMode() { + grid.setItems("foo", "bar", "baz"); + grid.setSelectionMode(SelectionMode.MULTI); + + Capture<SelectionEvent<String>> eventCapture = new Capture<>(); + + grid.addSelectionListener(event -> eventCapture.setValue(event)); + + grid.getSelectionModel().select("foo"); + + SelectionEvent<String> event = eventCapture.getValue(); + assertNotNull(event); + assertFalse(event.isUserOriginated()); + assertEquals("foo", event.getFirstSelected().get()); + assertEquals("foo", + event.getAllSelectedItems().stream().findFirst().get()); + + grid.getSelectionModel().select("bar"); + + event = eventCapture.getValue(); + assertNotNull(event); + assertFalse(event.isUserOriginated()); + assertEquals("foo", event.getFirstSelected().get()); + assertEquals("foo", + event.getAllSelectedItems().stream().findFirst().get()); + Assert.assertArrayEquals(new String[] { "foo", "bar" }, + event.getAllSelectedItems().toArray(new String[2])); + + grid.getSelectionModel().deselect("foo"); + + assertNotNull(event); + assertFalse(event.isUserOriginated()); + assertEquals("bar", event.getFirstSelected().get()); + assertEquals("bar", + event.getAllSelectedItems().stream().findFirst().get()); + Assert.assertArrayEquals(new String[] { "bar" }, + event.getAllSelectedItems().toArray(new String[1])); + + grid.getSelectionModel().deselectAll(); + + event = eventCapture.getValue(); + assertNotNull(event); + assertFalse(event.isUserOriginated()); + assertEquals(Optional.empty(), event.getFirstSelected()); + assertEquals(0, event.getAllSelectedItems().size()); + } + + @Test(expected = UnsupportedOperationException.class) + public void testAddSelectionListener_noSelectionMode() { + grid.setSelectionMode(SelectionMode.NONE); + + grid.addSelectionListener( + event -> Assert.fail("never ever happens (tm)")); + } } diff --git a/server/src/test/java/com/vaadin/ui/AbstractSingleSelectTest.java b/server/src/test/java/com/vaadin/ui/AbstractSingleSelectTest.java index ee129d0e0a..c930b8e217 100644 --- a/server/src/test/java/com/vaadin/ui/AbstractSingleSelectTest.java +++ b/server/src/test/java/com/vaadin/ui/AbstractSingleSelectTest.java @@ -81,7 +81,7 @@ public class AbstractSingleSelectTest { listing = new PersonListing(); listing.setItems(PERSON_A, PERSON_B, PERSON_C); selectionChanges = new ArrayList<>(); - listing.addSelectionChangeListener( + listing.addSelectionListener( e -> selectionChanges.add(e.getValue())); } @@ -237,7 +237,7 @@ public class AbstractSingleSelectTest { String value = "foo"; AbstractSingleSelect<String> select = new AbstractSingleSelect<String>() { @Override - public Registration addSelectionChangeListener( + public Registration addSelectionListener( SingleSelectionListener<String> listener) { selectionListener.set(listener); return registration; diff --git a/server/src/test/java/com/vaadin/ui/RadioButtonGroupTest.java b/server/src/test/java/com/vaadin/ui/RadioButtonGroupTest.java index 22b9206760..9219ec834e 100644 --- a/server/src/test/java/com/vaadin/ui/RadioButtonGroupTest.java +++ b/server/src/test/java/com/vaadin/ui/RadioButtonGroupTest.java @@ -42,7 +42,7 @@ public class RadioButtonGroupTest { public void apiSelectionChange_notUserOriginated() { AtomicInteger listenerCount = new AtomicInteger(0); - radioButtonGroup.addSelectionChangeListener(event -> { + radioButtonGroup.addSelectionListener(event -> { listenerCount.incrementAndGet(); Assert.assertFalse(event.isUserOriginated()); }); @@ -60,7 +60,7 @@ public class RadioButtonGroupTest { public void rpcSelectionChange_userOriginated() { AtomicInteger listenerCount = new AtomicInteger(0); - radioButtonGroup.addSelectionChangeListener(event -> { + radioButtonGroup.addSelectionListener(event -> { listenerCount.incrementAndGet(); Assert.assertTrue(event.isUserOriginated()); }); diff --git a/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractSingleSelectTestUI.java b/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractSingleSelectTestUI.java index 544cc73dc7..f2dc3c20ad 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractSingleSelectTestUI.java +++ b/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractSingleSelectTestUI.java @@ -33,7 +33,7 @@ public abstract class AbstractSingleSelectTestUI<T extends AbstractSingleSelect< protected void createListenerMenu() { createListenerAction("Selection listener", "Listeners", - c -> c.addSelectionChangeListener( + c -> c.addSelectionListener( e -> log("Selected: " + e.getValue()))); } 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 430692477d..4f9d733f44 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 @@ -230,7 +230,7 @@ public class GridBasics extends AbstractTestUIWithLog { selectionListenerRegistration = ((SingleSelectionModelImpl<DataObject>) grid .getSelectionModel()) - .addSelectionListener(this::onSingleSelect); + .addSingleSelectionListener(this::onSingleSelect); layout.addComponent(createMenu()); layout.addComponent(grid); @@ -512,7 +512,7 @@ public class GridBasics extends AbstractTestUIWithLog { grid.setSelectionMode(SelectionMode.SINGLE); selectionListenerRegistration = ((SingleSelectionModelImpl<DataObject>) grid .getSelectionModel()) - .addSelectionListener(this::onSingleSelect); + .addSingleSelectionListener(this::onSingleSelect); grid.asSingleSelect().setReadOnly(isUserSelectionAllowed); }); selectionModelItem.addItem("multi", menuItem -> { @@ -558,10 +558,10 @@ public class GridBasics extends AbstractTestUIWithLog { selectionListenerRegistration.remove(); MultiSelectionModelImpl<DataObject> model = (MultiSelectionModelImpl<DataObject>) grid .setSelectionMode(SelectionMode.MULTI); - model.addSelectionListener(this::onMultiSelect); + model.addMultiSelectionListener(this::onMultiSelect); grid.asMultiSelect().setReadOnly(isUserSelectionAllowed); selectionListenerRegistration = model - .addSelectionListener(this::onMultiSelect); + .addMultiSelectionListener(this::onMultiSelect); } } diff --git a/uitest/src/main/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTestUI.java b/uitest/src/main/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTestUI.java index 07e5b2c4d0..672ab1a49f 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTestUI.java +++ b/uitest/src/main/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTestUI.java @@ -102,7 +102,7 @@ public class RadioButtonGroupTestUI protected void createListenerMenu() { createListenerAction("Selection listener", "Listeners", - c -> c.addSelectionChangeListener( + c -> c.addSelectionListener( e -> log("Selected: " + e.getSelectedItem()))); } |