From 7e78d52dfe72678cf275585bc552b1612844da44 Mon Sep 17 00:00:00 2001 From: Pekka Hyvönen Date: Thu, 22 Sep 2016 14:19:34 +0300 Subject: TwinColSelect with new databinding API Removes feature for adding new items. Introduces a AbstractMultiSelect-abstraction layer, which is used in server side by TwinColSelect & CheckBoxGroup and on client side only TwinColSelect for now. Plan is to use it for ListSelect too. Further improvement would be to make AbstractMultiSelect use SelectionModel that extends AbstractSelectionModel and is thus used as an extension both as client & server side. Updates to JUnit 4.12 for easier use of @Parameterized test.. Change-Id: I64258c2229b9514d382693748e2ca562a1e448d4 --- .../data/selection/MultiSelectServerRpc.java | 41 +++++++++++ .../shared/data/selection/SelectionModel.java | 81 ++++++++++++++++++++-- .../com/vaadin/shared/ui/ListingJsonConstants.java | 28 ++++++++ .../ui/optiongroup/CheckBoxGroupConstants.java | 30 -------- .../ui/optiongroup/RadioButtonGroupConstants.java | 30 -------- .../ui/twincolselect/TwinColSelectState.java | 35 ++++++++++ 6 files changed, 180 insertions(+), 65 deletions(-) create mode 100644 shared/src/main/java/com/vaadin/shared/data/selection/MultiSelectServerRpc.java create mode 100644 shared/src/main/java/com/vaadin/shared/ui/ListingJsonConstants.java delete mode 100644 shared/src/main/java/com/vaadin/shared/ui/optiongroup/CheckBoxGroupConstants.java delete mode 100644 shared/src/main/java/com/vaadin/shared/ui/optiongroup/RadioButtonGroupConstants.java create mode 100644 shared/src/main/java/com/vaadin/shared/ui/twincolselect/TwinColSelectState.java (limited to 'shared') diff --git a/shared/src/main/java/com/vaadin/shared/data/selection/MultiSelectServerRpc.java b/shared/src/main/java/com/vaadin/shared/data/selection/MultiSelectServerRpc.java new file mode 100644 index 0000000000..a924f00d11 --- /dev/null +++ b/shared/src/main/java/com/vaadin/shared/data/selection/MultiSelectServerRpc.java @@ -0,0 +1,41 @@ +/* + * 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.shared.data.selection; + +import java.util.Set; + +import com.vaadin.shared.communication.ServerRpc; + +/** + * Transmits SelectionModel selection changes from the client to the server. + * + * @author Vaadin Ltd + * + * @since 8.0 + */ +public interface MultiSelectServerRpc extends ServerRpc { + + /** + * Updates the selected items based on their keys. + * + * @param addedItemKeys + * the item keys added to selection + * @param removedItemKeys + * the item keys removed from selection + */ + void updateSelection(Set addedItemKeys, + Set removedItemKeys); +} diff --git a/shared/src/main/java/com/vaadin/shared/data/selection/SelectionModel.java b/shared/src/main/java/com/vaadin/shared/data/selection/SelectionModel.java index 8711d6a9c8..c56fe8c4d9 100644 --- a/shared/src/main/java/com/vaadin/shared/data/selection/SelectionModel.java +++ b/shared/src/main/java/com/vaadin/shared/data/selection/SelectionModel.java @@ -16,9 +16,13 @@ package com.vaadin.shared.data.selection; import java.io.Serializable; +import java.util.Arrays; import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.stream.Stream; /** * Models the selection logic of a {@code Listing} component. Determines how @@ -60,7 +64,7 @@ public interface SelectionModel extends Serializable { /** * Sets the current selection to the given item, or clears selection if * given {@code null}. - * + * * @param item * the item to select or {@code null} to clear selection */ @@ -78,11 +82,11 @@ public interface SelectionModel extends Serializable { * * @return a singleton set of the selected item if any, an empty set * otherwise - * + * * @see #getSelectedItem() */ @Override - default Set getSelectedItems() { + public default Set getSelectedItems() { return getSelectedItem().map(Collections::singleton) .orElse(Collections.emptySet()); } @@ -98,10 +102,77 @@ public interface SelectionModel extends Serializable { public interface Multi extends SelectionModel { /** - * Adds the given items to the set of currently selected items. + * Adds the given item to the set of currently selected items. + *

+ * By default this does not clear any previous selection. To do that, + * use {@link #deselectAll()}. + *

+ * If the the item was already selected, this is a NO-OP. + * + * @param item + * the item to add to selection, not {@code null} */ @Override - public void select(T item); + public default void select(T item) { + Objects.requireNonNull(item); + selectItems(item); + }; + + /** + * Adds the given items to the set of currently selected items. + *

+ * By default this does not clear any previous selection. To do that, + * use {@link #deselectAll()}. + *

+ * If the all the items were already selected, this is a NO-OP. + *

+ * This is a short-hand for {@link #updateSelection(Set, Set)} with + * nothing to deselect. + * + * @param items + * to add to selection, not {@code null} + */ + public default void selectItems(T... items) { + Objects.requireNonNull(items); + Stream.of(items).forEach(Objects::requireNonNull); + + updateSelection(new LinkedHashSet<>(Arrays.asList(items)), + Collections.emptySet()); + } + + /** + * Removes the given items from the set of currently selected items. + *

+ * If the none of the items were selected, this is a NO-OP. + *

+ * This is a short-hand for {@link #updateSelection(Set, Set)} with + * nothing to select. + * + * @param items + * to remove from selection, not {@code null} + */ + public default void deselectItems(T... items) { + Objects.requireNonNull(items); + Stream.of(items).forEach(Objects::requireNonNull); + + updateSelection(Collections.emptySet(), + new LinkedHashSet<>(Arrays.asList(items))); + } + + /** + * Updates the selection by adding and removing the given items from it. + *

+ * If all the added items were already selected and the removed items + * were not selected, this is a NO-OP. + *

+ * Duplicate items (in both add & remove sets) are ignored. + * + * @param addedItems + * the items to add, not {@code null} + * @param removedItems + * the items to remove, not {@code null} + */ + public void updateSelection(Set addedItems, Set removedItems); } /** diff --git a/shared/src/main/java/com/vaadin/shared/ui/ListingJsonConstants.java b/shared/src/main/java/com/vaadin/shared/ui/ListingJsonConstants.java new file mode 100644 index 0000000000..82431bf7f1 --- /dev/null +++ b/shared/src/main/java/com/vaadin/shared/ui/ListingJsonConstants.java @@ -0,0 +1,28 @@ +/* + * 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.shared.ui; + +import java.io.Serializable; + +public class ListingJsonConstants implements Serializable { + public static final String JSONKEY_ITEM_DISABLED = "d"; + + public static final String JSONKEY_ITEM_ICON = "i"; + + public static final String JSONKEY_ITEM_VALUE = "v"; + + public static final String JSONKEY_ITEM_SELECTED = "s"; +} diff --git a/shared/src/main/java/com/vaadin/shared/ui/optiongroup/CheckBoxGroupConstants.java b/shared/src/main/java/com/vaadin/shared/ui/optiongroup/CheckBoxGroupConstants.java deleted file mode 100644 index 6bca43852a..0000000000 --- a/shared/src/main/java/com/vaadin/shared/ui/optiongroup/CheckBoxGroupConstants.java +++ /dev/null @@ -1,30 +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.shared.ui.optiongroup; - -import java.io.Serializable; - -public class CheckBoxGroupConstants implements Serializable { - public static final String JSONKEY_ITEM_DISABLED = "d"; - - public static final String JSONKEY_ITEM_ICON = "i"; - - public static final String JSONKEY_ITEM_VALUE = "v"; - - public static final String JSONKEY_ITEM_KEY = "k"; - - public static final String JSONKEY_ITEM_SELECTED = "s"; -} diff --git a/shared/src/main/java/com/vaadin/shared/ui/optiongroup/RadioButtonGroupConstants.java b/shared/src/main/java/com/vaadin/shared/ui/optiongroup/RadioButtonGroupConstants.java deleted file mode 100644 index 5278d211de..0000000000 --- a/shared/src/main/java/com/vaadin/shared/ui/optiongroup/RadioButtonGroupConstants.java +++ /dev/null @@ -1,30 +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.shared.ui.optiongroup; - -import java.io.Serializable; - -public class RadioButtonGroupConstants implements Serializable { - public static final String JSONKEY_ITEM_DISABLED = "d"; - - public static final String JSONKEY_ITEM_ICON = "i"; - - public static final String JSONKEY_ITEM_VALUE = "v"; - - public static final String JSONKEY_ITEM_KEY = "k"; - - public static final String JSONKEY_ITEM_SELECTED = "s"; -} diff --git a/shared/src/main/java/com/vaadin/shared/ui/twincolselect/TwinColSelectState.java b/shared/src/main/java/com/vaadin/shared/ui/twincolselect/TwinColSelectState.java new file mode 100644 index 0000000000..a6b09c0e91 --- /dev/null +++ b/shared/src/main/java/com/vaadin/shared/ui/twincolselect/TwinColSelectState.java @@ -0,0 +1,35 @@ +/* + * 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.shared.ui.twincolselect; + +import com.vaadin.shared.annotations.DelegateToWidget; +import com.vaadin.shared.ui.TabIndexState; + +/** + * Shared state for the TwinColSelect component. + * + * @since 7.0 + */ +public class TwinColSelectState extends TabIndexState { + { + primaryStyleName = "v-select-twincol"; + } + @DelegateToWidget + public int rows; + + public String leftColumnCaption; + public String rightColumnCaption; +} -- cgit v1.2.3