diff options
author | Denis Anisimov <denis@vaadin.com> | 2016-10-20 13:33:43 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-10-24 10:29:06 +0000 |
commit | 6d75c88fcb8c37e4f588bdc09dc183f9d51e4a62 (patch) | |
tree | 0120d3580a0c6d36432d0cdf17bad0e919411ec8 /server | |
parent | 09485d529d085a380b347a118b54df9ae30fefd0 (diff) | |
download | vaadin-framework-6d75c88fcb8c37e4f588bdc09dc183f9d51e4a62.tar.gz vaadin-framework-6d75c88fcb8c37e4f588bdc09dc183f9d51e4a62.zip |
Move old SelectionEvent to V7 and create a new selection event supertype
Change-Id: I17496c803f81fdaa98e47cbfcc9bdc91625c134f
Diffstat (limited to 'server')
5 files changed, 128 insertions, 136 deletions
diff --git a/server/src/main/java/com/vaadin/event/SelectionEvent.java b/server/src/main/java/com/vaadin/event/SelectionEvent.java deleted file mode 100644 index b5a4d587e6..0000000000 --- a/server/src/main/java/com/vaadin/event/SelectionEvent.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * 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; - -import java.io.Serializable; -import java.util.Collection; -import java.util.Collections; -import java.util.EventObject; -import java.util.LinkedHashSet; -import java.util.Set; - -/** - * An event that specifies what in a selection has changed, and where the - * selection took place. - * - * @since 7.4 - * @author Vaadin Ltd - */ -public class SelectionEvent extends EventObject { - - private LinkedHashSet<Object> oldSelection; - private LinkedHashSet<Object> newSelection; - - public SelectionEvent(Object source, Collection<Object> oldSelection, - Collection<Object> newSelection) { - super(source); - this.oldSelection = new LinkedHashSet<>(oldSelection); - this.newSelection = new LinkedHashSet<>(newSelection); - } - - /** - * A {@link Collection} of all the itemIds that became selected. - * <p> - * <em>Note:</em> this excludes all itemIds that might have been previously - * selected. - * - * @return a Collection of the itemIds that became selected - */ - public Set<Object> getAdded() { - return setDifference(newSelection, oldSelection); - } - - /** - * A {@link Collection} of all the itemIds that became deselected. - * <p> - * <em>Note:</em> this excludes all itemIds that might have been previously - * deselected. - * - * @return a Collection of the itemIds that became deselected - */ - public Set<Object> getRemoved() { - return setDifference(oldSelection, newSelection); - } - - /** - * Slightly optimized set difference that can return the original set or a - * modified one. - * - * @param set1 - * original set - * @param set2 - * the set to subtract - * @return the difference set - */ - private static <T> Set<T> setDifference(Set<T> set1, Set<T> set2) { - if (set2.isEmpty()) { - return set1; - } else { - LinkedHashSet<T> set = new LinkedHashSet<>(set1); - set.removeAll(set2); - return set; - } - } - - /** - * A {@link Collection} of all the itemIds that are currently selected. - * - * @return a Collection of the itemIds that are currently selected - */ - public Set<Object> getSelected() { - return Collections.unmodifiableSet(newSelection); - } - - /** - * The listener interface for receiving {@link SelectionEvent - * SelectionEvents}. - */ - public interface SelectionListener extends Serializable { - /** - * Notifies the listener that the selection state has changed. - * - * @param event - * the selection change event - */ - void select(SelectionEvent event); - } - - /** - * The interface for adding and removing listeners for {@link SelectionEvent - * SelectionEvents}. - */ - public interface SelectionNotifier extends Serializable { - /** - * Registers a new selection listener - * - * @param listener - * the listener to register - */ - void addSelectionListener(SelectionListener listener); - - /** - * Removes a previously registered selection change listener - * - * @param listener - * the listener to remove - */ - void removeSelectionListener(SelectionListener 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 1de911f353..c79b405523 100644 --- a/server/src/main/java/com/vaadin/event/selection/MultiSelectionEvent.java +++ b/server/src/main/java/com/vaadin/event/selection/MultiSelectionEvent.java @@ -16,6 +16,7 @@ package com.vaadin.event.selection; import java.util.Collections; +import java.util.Optional; import java.util.Set; import com.vaadin.data.HasValue.ValueChange; @@ -33,7 +34,8 @@ import com.vaadin.ui.AbstractListing; * @param <T> * the data type of the selection model */ -public class MultiSelectionEvent<T> extends ValueChange<Set<T>> { +public class MultiSelectionEvent<T> extends ValueChange<Set<T>> + implements SelectionEvent<T> { private final Set<T> oldSelection; @@ -75,4 +77,9 @@ public class MultiSelectionEvent<T> extends ValueChange<Set<T>> { public Set<T> getOldSelection() { return Collections.unmodifiableSet(oldSelection); } + + @Override + public Optional<T> getFirstSelected() { + return getValue().stream().findFirst(); + } } diff --git a/server/src/main/java/com/vaadin/event/selection/SelectionEvent.java b/server/src/main/java/com/vaadin/event/selection/SelectionEvent.java new file mode 100644 index 0000000000..28b3fd4461 --- /dev/null +++ b/server/src/main/java/com/vaadin/event/selection/SelectionEvent.java @@ -0,0 +1,43 @@ +/* + * 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 java.io.Serializable; +import java.util.Optional; + +/** + * A selection event that unifies the way to access to selection event for multi + * selection and single selection components (in case when only one selected + * item is required). + * + * @since 8.0 + * @author Vaadin Ltd + * @param <T> + * the data type of the selection model + */ +public interface SelectionEvent<T> extends Serializable { + + /** + * Get first selected data item. + * <p> + * This is the same as {@link SingleSelectionChange#getSelectedItem()} in + * case of single selection and the first selected item from + * {@link MultiSelectionEvent#getNewSelection()} in case of multi selection. + * + * @return the first selected item. + */ + Optional<T> getFirstSelected(); +} diff --git a/server/src/main/java/com/vaadin/event/selection/SingleSelectionChange.java b/server/src/main/java/com/vaadin/event/selection/SingleSelectionChange.java index fe120c1692..3f4323b9ac 100644 --- a/server/src/main/java/com/vaadin/event/selection/SingleSelectionChange.java +++ b/server/src/main/java/com/vaadin/event/selection/SingleSelectionChange.java @@ -29,7 +29,8 @@ import com.vaadin.ui.AbstractListing; * the type of the selected item * @since 8.0 */ -public class SingleSelectionChange<T> extends ValueChange<T> { +public class SingleSelectionChange<T> extends ValueChange<T> + implements SelectionEvent<T> { /** * Creates a new selection change event. @@ -42,8 +43,8 @@ public class SingleSelectionChange<T> extends ValueChange<T> { * {@code true} if this event originates from the client, * {@code false} otherwise. */ - public SingleSelectionChange(AbstractListing<T, ?> source, - T selectedItem, boolean userOriginated) { + public SingleSelectionChange(AbstractListing<T, ?> source, T selectedItem, + boolean userOriginated) { super(source, selectedItem, userOriginated); } @@ -64,4 +65,9 @@ public class SingleSelectionChange<T> extends ValueChange<T> { public AbstractListing<T, ?> getSource() { return (AbstractListing<T, ?>) super.getSource(); } + + @Override + public Optional<T> getFirstSelected() { + return getSelectedItem(); + } } diff --git a/server/src/test/java/com/vaadin/event/selection/SelectionEventTest.java b/server/src/test/java/com/vaadin/event/selection/SelectionEventTest.java new file mode 100644 index 0000000000..8ae15f5ad2 --- /dev/null +++ b/server/src/test/java/com/vaadin/event/selection/SelectionEventTest.java @@ -0,0 +1,68 @@ +/* + * 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 java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Optional; + +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +/** + * @author Vaadin Ltd + * + */ +public class SelectionEventTest { + + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Test + public void getFirstSelected_mutliSelectEvent() { + MultiSelectionEvent<?> event = Mockito.mock(MultiSelectionEvent.class); + Mockito.doCallRealMethod().when(event).getFirstSelected(); + + Mockito.when(event.getValue()) + .thenReturn(new LinkedHashSet(Arrays.asList("foo", "bar"))); + + Optional<?> selected = event.getFirstSelected(); + + Mockito.verify(event).getValue(); + Assert.assertEquals("foo", selected.get()); + + Mockito.when(event.getValue()).thenReturn(Collections.emptySet()); + Assert.assertFalse(event.getFirstSelected().isPresent()); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Test + public void getFirstSelected_singleSelectEvent() { + SingleSelectionChange event = Mockito.mock(SingleSelectionChange.class); + Mockito.doCallRealMethod().when(event).getFirstSelected(); + + Mockito.when(event.getSelectedItem()).thenReturn(Optional.of("foo")); + + Optional<?> selected = event.getSelectedItem(); + + Mockito.verify(event).getSelectedItem(); + Assert.assertEquals("foo", selected.get()); + + Mockito.when(event.getSelectedItem()).thenReturn(Optional.empty()); + Assert.assertFalse(event.getFirstSelected().isPresent()); + } + +} |