diff options
Diffstat (limited to 'server/src/main/java/com/vaadin/ui')
-rw-r--r-- | server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java | 66 | ||||
-rw-r--r-- | server/src/main/java/com/vaadin/ui/CheckBoxGroup.java | 62 |
2 files changed, 92 insertions, 36 deletions
diff --git a/server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java b/server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java new file mode 100644 index 0000000000..1d17841812 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java @@ -0,0 +1,66 @@ +/* + * 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; + +import java.lang.reflect.Method; +import java.util.Objects; + +import com.vaadin.event.selection.MultiSelectionEvent; +import com.vaadin.event.selection.MultiSelectionListener; +import com.vaadin.shared.Registration; +import com.vaadin.shared.data.selection.SelectionModel.Multi; +import com.vaadin.util.ReflectTools; + +/** + * Base class for listing components that allow selecting multiple items. + * + * @param <T> + * item type + * @author Vaadin Ltd + * @since 8.0 + */ +public abstract class AbstractMultiSelect<T> + extends AbstractListing<T, Multi<T>> { + + @Deprecated + private static final Method SELECTION_CHANGE_METHOD = ReflectTools + .findMethod(MultiSelectionListener.class, "accept", + MultiSelectionEvent.class); + + /** + * Creates a new multi select with an empty data source. + */ + protected AbstractMultiSelect() { + super(); + } + + /** + * 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</code> + * @return a registration for the listener + */ + public Registration addSelectionListener( + MultiSelectionListener<T> listener) { + Objects.requireNonNull(listener, "listener cannot be null"); + addListener(MultiSelectionEvent.class, listener, + SELECTION_CHANGE_METHOD); + return () -> removeListener(MultiSelectionEvent.class, listener); + } + +}
\ No newline at end of file diff --git a/server/src/main/java/com/vaadin/ui/CheckBoxGroup.java b/server/src/main/java/com/vaadin/ui/CheckBoxGroup.java index 8687aa2b92..8525dc6cbb 100644 --- a/server/src/main/java/com/vaadin/ui/CheckBoxGroup.java +++ b/server/src/main/java/com/vaadin/ui/CheckBoxGroup.java @@ -16,7 +16,6 @@ package com.vaadin.ui; -import java.lang.reflect.Method; import java.util.Collection; import java.util.Collections; import java.util.LinkedHashSet; @@ -29,17 +28,14 @@ import java.util.function.Predicate; import com.vaadin.data.Listing; import com.vaadin.event.selection.MultiSelectionEvent; -import com.vaadin.event.selection.MultiSelectionListener; import com.vaadin.server.Resource; import com.vaadin.server.ResourceReference; import com.vaadin.server.data.DataGenerator; import com.vaadin.server.data.DataSource; -import com.vaadin.shared.Registration; import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.shared.data.selection.SelectionServerRpc; import com.vaadin.shared.ui.optiongroup.CheckBoxGroupConstants; import com.vaadin.shared.ui.optiongroup.CheckBoxGroupState; -import com.vaadin.util.ReflectTools; import elemental.json.JsonObject; @@ -52,8 +48,7 @@ import elemental.json.JsonObject; * @author Vaadin Ltd * @since 8.0 */ -public class CheckBoxGroup<T> - extends AbstractListing<T, SelectionModel.Multi<T>> { +public class CheckBoxGroup<T> extends AbstractMultiSelect<T> { private final class SimpleMultiSelectModel implements SelectionModel.Multi<T> { @@ -62,11 +57,16 @@ public class CheckBoxGroup<T> @Override public void select(T item) { + // Not user originated + select(item, false); + } + + private void select(T item, boolean userOriginated) { if (selection.contains(item)) { return; } - updateSelection(set -> set.add(item)); + updateSelection(set -> set.add(item), userOriginated); } @Override @@ -76,11 +76,16 @@ public class CheckBoxGroup<T> @Override public void deselect(T item) { + // Not user originated + deselect(item, false); + } + + private void deselect(T item, boolean userOriginated) { if (!selection.contains(item)) { return; } - updateSelection(set -> set.remove(item)); + updateSelection(set -> set.remove(item), userOriginated); } @Override @@ -89,16 +94,17 @@ public class CheckBoxGroup<T> return; } - updateSelection(Set::clear); + updateSelection(Set::clear, false); } - private void updateSelection(Consumer<Set<T>> handler) { + private void updateSelection(Consumer<Set<T>> handler, + boolean userOriginated) { LinkedHashSet<T> oldSelection = new LinkedHashSet<>(selection); handler.accept(selection); LinkedHashSet<T> newSelection = new LinkedHashSet<>(selection); fireEvent(new MultiSelectionEvent<>(CheckBoxGroup.this, - oldSelection, newSelection)); + oldSelection, newSelection, userOriginated)); getDataCommunicator().reset(); } @@ -109,11 +115,6 @@ public class CheckBoxGroup<T> } } - @Deprecated - private static final Method SELECTION_CHANGE_METHOD = ReflectTools - .findMethod(MultiSelectionListener.class, "accept", - MultiSelectionEvent.class); - private Function<T, Resource> itemIconProvider = item -> null; private Function<T, String> itemCaptionProvider = String::valueOf; @@ -172,14 +173,14 @@ public class CheckBoxGroup<T> @Override public void select(String key) { - getItemForSelectionChange(key) - .ifPresent(getSelectionModel()::select); + getItemForSelectionChange(key).ifPresent( + item -> getSelectionModel().select(item, true)); } @Override public void deselect(String key) { - getItemForSelectionChange(key) - .ifPresent(getSelectionModel()::deselect); + getItemForSelectionChange(key).ifPresent( + item -> getSelectionModel().deselect(item, true)); } private Optional<T> getItemForSelectionChange(String key) { @@ -190,6 +191,11 @@ public class CheckBoxGroup<T> return Optional.of(item); } + + private SimpleMultiSelectModel getSelectionModel() { + return (SimpleMultiSelectModel) CheckBoxGroup.this + .getSelectionModel(); + } }); addDataGenerator(new DataGenerator<T>() { @@ -330,20 +336,4 @@ public class CheckBoxGroup<T> Objects.nonNull(itemEnabledProvider); this.itemEnabledProvider = itemEnabledProvider; } - - /** - * 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</code> - * @return a registration for the listener - */ - public Registration addSelectionListener( - MultiSelectionListener<T> listener) { - Objects.requireNonNull(listener, "listener cannot be null"); - addListener(MultiSelectionEvent.class, listener, - SELECTION_CHANGE_METHOD); - return () -> removeListener(MultiSelectionEvent.class, listener); - } } |