From: Denis Anisimov Date: Tue, 1 Nov 2016 13:27:28 +0000 (+0200) Subject: Make SelectionModel API only server side. X-Git-Tag: 8.0.0.alpha6~17 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=13747f7003625a012bfdc788a11482a346bcba4d;p=vaadin-framework.git Make SelectionModel API only server side. Client side doesn't use selection model anymore. Fixes vaadin/framework8-issues#421 Change-Id: If3ecb1c2f3a0024df9bfdfd182eaf8cf8625ac75 --- diff --git a/client/src/main/java/com/vaadin/client/connectors/AbstractFocusableListingConnector.java b/client/src/main/java/com/vaadin/client/connectors/AbstractFocusableListingConnector.java index 996086d36d..9ce7dc194c 100644 --- a/client/src/main/java/com/vaadin/client/connectors/AbstractFocusableListingConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/AbstractFocusableListingConnector.java @@ -18,7 +18,6 @@ package com.vaadin.client.connectors; import com.google.gwt.event.dom.client.HasAllFocusHandlers; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ui.ConnectorFocusAndBlurHandler; -import com.vaadin.shared.data.selection.SelectionModel; /** * Abstract class for listing widget connectors that contains focusable children @@ -28,11 +27,9 @@ import com.vaadin.shared.data.selection.SelectionModel; * * @param * widget type which has to allow to register focus/blur handlers - * @param - * the client-side selection model type */ -public abstract class AbstractFocusableListingConnector> - extends AbstractListingConnector { +public abstract class AbstractFocusableListingConnector + extends AbstractListingConnector { private ConnectorFocusAndBlurHandler handler; diff --git a/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java b/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java index b89106307e..88fccab32e 100644 --- a/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java @@ -19,7 +19,6 @@ import com.vaadin.client.connectors.data.HasDataSource; import com.vaadin.client.data.DataSource; import com.vaadin.client.ui.AbstractFieldConnector; import com.vaadin.shared.data.DataCommunicatorConstants; -import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.ui.AbstractListing; import elemental.json.JsonObject; @@ -30,18 +29,13 @@ import elemental.json.JsonValue; * * @author Vaadin Ltd. * - * @param - * the client-side selection model type - * * @since 8.0 */ -public abstract class AbstractListingConnector> - extends AbstractFieldConnector implements HasDataSource { +public abstract class AbstractListingConnector extends AbstractFieldConnector + implements HasDataSource { private DataSource dataSource = null; - private SELECTIONMODEL selectionModel = null; - @Override public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; @@ -52,25 +46,6 @@ public abstract class AbstractListingConnector> - implements HasRequiredIndicator { + extends AbstractListingConnector implements HasRequiredIndicator { /** * Abstraction layer to help populate different multiselect widgets based on diff --git a/client/src/main/java/com/vaadin/client/connectors/AbstractSingleSelectConnector.java b/client/src/main/java/com/vaadin/client/connectors/AbstractSingleSelectConnector.java index 5554b8421c..96ccf2f7c2 100644 --- a/client/src/main/java/com/vaadin/client/connectors/AbstractSingleSelectConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/AbstractSingleSelectConnector.java @@ -18,7 +18,6 @@ package com.vaadin.client.connectors; import com.google.gwt.event.dom.client.HasAllFocusHandlers; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ui.HasRequiredIndicator; -import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.shared.ui.AbstractSingleSelectState; /** @@ -28,8 +27,7 @@ import com.vaadin.shared.ui.AbstractSingleSelectState; * @since 8.0.0 */ public abstract class AbstractSingleSelectConnector - extends - AbstractFocusableListingConnector> + extends AbstractFocusableListingConnector implements HasRequiredIndicator { @Override diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java index ad5f8200ea..ef715acfad 100644 --- a/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java @@ -21,7 +21,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; import com.google.gwt.dom.client.Element; @@ -40,6 +40,7 @@ import com.vaadin.client.annotations.OnStateChange; import com.vaadin.client.connectors.AbstractListingConnector; import com.vaadin.client.connectors.grid.ColumnConnector.CustomColumn; import com.vaadin.client.data.DataSource; +import com.vaadin.client.data.SelectionModel; import com.vaadin.client.ui.SimpleManagedLayout; import com.vaadin.client.widget.grid.CellReference; import com.vaadin.client.widget.grid.EventCellReference; @@ -58,7 +59,6 @@ import com.vaadin.client.widgets.Grid.HeaderCell; import com.vaadin.client.widgets.Grid.HeaderRow; import com.vaadin.shared.MouseEventDetails; import com.vaadin.shared.data.DataCommunicatorConstants; -import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.shared.data.selection.SelectionServerRpc; import com.vaadin.shared.data.sort.SortDirection; import com.vaadin.shared.ui.Connect; @@ -79,8 +79,7 @@ import elemental.json.JsonObject; * @since 8.0 */ @Connect(com.vaadin.ui.Grid.class) -public class GridConnector - extends AbstractListingConnector> +public class GridConnector extends AbstractListingConnector implements HasComponentsConnector, SimpleManagedLayout, DeferredWorker { private class ItemClickHandler @@ -205,7 +204,7 @@ public class GridConnector /* Item click events */ getWidget().addBodyClickHandler(itemClickHandler); getWidget().addBodyDoubleClickHandler(itemClickHandler); - getWidget().setSelectionModel(new SelectionModel.Single() { + getWidget().setSelectionModel(new SelectionModel() { @Override public void select(JsonObject item) { @@ -220,7 +219,7 @@ public class GridConnector } @Override - public Optional getSelectedItem() { + public Set getSelectedItems() { throw new UnsupportedOperationException( "Selected item not known on the client side"); } @@ -230,6 +229,7 @@ public class GridConnector return item.hasKey(DataCommunicatorConstants.SELECTED) && item.getBoolean(DataCommunicatorConstants.SELECTED); } + }); layout(); @@ -321,12 +321,6 @@ public class GridConnector getWidget().setDataSource(dataSource); } - @Override - public void setSelectionModel(SelectionModel selectionModel) { - throw new UnsupportedOperationException( - "Cannot set a selection model for GridConnector"); - } - /** * Adds a column to the Grid widget. For each column a communication id * stored for client to server communication. diff --git a/client/src/main/java/com/vaadin/client/connectors/selection/AbstractSelectionConnector.java b/client/src/main/java/com/vaadin/client/connectors/selection/AbstractSelectionConnector.java index 9d4604993d..55ce2dee7b 100644 --- a/client/src/main/java/com/vaadin/client/connectors/selection/AbstractSelectionConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/selection/AbstractSelectionConnector.java @@ -19,7 +19,6 @@ import com.vaadin.client.ServerConnector; import com.vaadin.client.connectors.AbstractListingConnector; import com.vaadin.client.extensions.AbstractExtensionConnector; import com.vaadin.shared.data.DataCommunicatorConstants; -import com.vaadin.shared.data.selection.SelectionModel; import elemental.json.JsonObject; @@ -28,15 +27,11 @@ import elemental.json.JsonObject; * * @author Vaadin Ltd. * - * @param - * the supported client-side selection model * @since 8.0 */ -public abstract class AbstractSelectionConnector> +public abstract class AbstractSelectionConnector extends AbstractExtensionConnector { - private SELECTIONMODEL model = null; - @Override @SuppressWarnings("unchecked") protected void extend(ServerConnector target) { @@ -45,31 +40,12 @@ public abstract class AbstractSelectionConnector) target) - .setSelectionModel(model); } - /** - * Creates a selection model object to be used by the Connector. - * - * @return created selection model - */ - protected abstract SELECTIONMODEL createSelectionModel(); - @Override @SuppressWarnings("unchecked") - public AbstractListingConnector getParent() { - return (AbstractListingConnector) super.getParent(); - } - - /** - * Returns the client-side selection model associated with this connector. - * - * @return the selection model in use - */ - protected SELECTIONMODEL getSelectionModel() { - return model; + public AbstractListingConnector getParent() { + return (AbstractListingConnector) super.getParent(); } /** diff --git a/client/src/main/java/com/vaadin/client/data/SelectionModel.java b/client/src/main/java/com/vaadin/client/data/SelectionModel.java new file mode 100644 index 0000000000..80eb20fef1 --- /dev/null +++ b/client/src/main/java/com/vaadin/client/data/SelectionModel.java @@ -0,0 +1,73 @@ +/* + * 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.client.data; + +import java.util.Set; + +/** + * Models the selection logic of a {@code Grid} component. Determines how items + * can be selected and deselected. + * + * @author Vaadin Ltd. + * + * @param + * the type of the items to select + * @since 8.0 + */ +public interface SelectionModel { + + /** + * Selects the given item. If another item was already selected, that item + * is deselected. + * + * @param item + * the item to select, not null + */ + void select(T item); + + /** + * Deselects the given item. If the item is not currently selected, does + * nothing. + * + * @param item + * the item to deselect, not null + */ + void deselect(T item); + + /** + * Returns a set of the currently selected items. It is safe to invoke other + * {@code SelectionModel} methods while iterating over the set. + * + * @return the items in the current selection, not null + */ + Set getSelectedItems(); + + /** + * Returns whether the given item is currently selected. + * + * @param item + * the item to check, not null + * @return {@code true} if the item is selected, {@code false} otherwise + */ + boolean isSelected(T item); + + /** + * Deselects all currently selected items. + */ + default void deselectAll() { + getSelectedItems().forEach(this::deselect); + } +} diff --git a/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java b/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java index 97f382453f..8b56483e8f 100644 --- a/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java @@ -30,7 +30,6 @@ import com.vaadin.shared.EventId; import com.vaadin.shared.Registration; import com.vaadin.shared.communication.FieldRpc.FocusAndBlurServerRpc; import com.vaadin.shared.data.DataCommunicatorConstants; -import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.shared.data.selection.SelectionServerRpc; import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.combobox.ComboBoxConstants; @@ -41,8 +40,7 @@ import com.vaadin.ui.ComboBox; import elemental.json.JsonObject; @Connect(ComboBox.class) -public class ComboBoxConnector - extends AbstractListingConnector> +public class ComboBoxConnector extends AbstractListingConnector implements HasRequiredIndicator, HasDataSource, SimpleManagedLayout, HasErrorIndicator { diff --git a/client/src/main/java/com/vaadin/client/ui/optiongroup/CheckBoxGroupConnector.java b/client/src/main/java/com/vaadin/client/ui/optiongroup/CheckBoxGroupConnector.java index 940287d583..4262666943 100644 --- a/client/src/main/java/com/vaadin/client/ui/optiongroup/CheckBoxGroupConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/optiongroup/CheckBoxGroupConnector.java @@ -27,7 +27,6 @@ import com.vaadin.client.data.DataSource; import com.vaadin.client.ui.HasRequiredIndicator; import com.vaadin.client.ui.VCheckBoxGroup; import com.vaadin.shared.data.selection.MultiSelectServerRpc; -import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.optiongroup.CheckBoxGroupState; import com.vaadin.ui.CheckBoxGroup; @@ -38,8 +37,8 @@ import elemental.json.JsonObject; // We don't care about the framework-provided selection model at this point // TODO refactor to extend AbstractMultiSelectConnector, maybe when // SelectionModel is removed from client side framwork8-issues#421 -public class CheckBoxGroupConnector extends - AbstractFocusableListingConnector> +public class CheckBoxGroupConnector + extends AbstractFocusableListingConnector implements HasRequiredIndicator { @Override diff --git a/client/src/main/java/com/vaadin/client/widget/grid/events/SelectAllEvent.java b/client/src/main/java/com/vaadin/client/widget/grid/events/SelectAllEvent.java index cd86dbe993..2ac2f51e4b 100644 --- a/client/src/main/java/com/vaadin/client/widget/grid/events/SelectAllEvent.java +++ b/client/src/main/java/com/vaadin/client/widget/grid/events/SelectAllEvent.java @@ -16,7 +16,7 @@ package com.vaadin.client.widget.grid.events; import com.google.gwt.event.shared.GwtEvent; -import com.vaadin.shared.data.selection.SelectionModel; +import com.vaadin.client.data.SelectionModel; /** * A select all event, fired by the Grid when it needs all rows in data source @@ -32,9 +32,9 @@ public class SelectAllEvent extends GwtEvent> { */ private final static Type> TYPE = new Type<>();; - private SelectionModel.Multi selectionModel; + private SelectionModel selectionModel; - public SelectAllEvent(SelectionModel.Multi selectionModel) { + public SelectAllEvent(SelectionModel selectionModel) { this.selectionModel = selectionModel; } @@ -53,7 +53,7 @@ public class SelectAllEvent extends GwtEvent> { handler.onSelectAll(this); } - public SelectionModel.Multi getSelectionModel() { + public SelectionModel getSelectionModel() { return selectionModel; } } diff --git a/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModelWithSelectionColumn.java b/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModelWithSelectionColumn.java index 5c57607e70..f6e55bf596 100644 --- a/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModelWithSelectionColumn.java +++ b/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModelWithSelectionColumn.java @@ -16,7 +16,6 @@ package com.vaadin.client.widget.grid.selection; import com.vaadin.client.renderers.Renderer; -import com.vaadin.shared.data.selection.SelectionModel; /** * Interface for SelectionModels that wants Grid to display a selection column. @@ -24,13 +23,9 @@ import com.vaadin.shared.data.selection.SelectionModel; * @author Vaadin Ltd * @since 8.0 * - * @param - * selected item type - * * @see Renderer */ -public interface SelectionModelWithSelectionColumn - extends SelectionModel { +public interface SelectionModelWithSelectionColumn { /** * Returns a new instance of the Renderer for selection column. diff --git a/client/src/main/java/com/vaadin/client/widgets/Grid.java b/client/src/main/java/com/vaadin/client/widgets/Grid.java index 7d6c1369b8..276cde7666 100644 --- a/client/src/main/java/com/vaadin/client/widgets/Grid.java +++ b/client/src/main/java/com/vaadin/client/widgets/Grid.java @@ -83,6 +83,7 @@ import com.vaadin.client.WidgetUtil; import com.vaadin.client.data.DataChangeHandler; import com.vaadin.client.data.DataSource; import com.vaadin.client.data.DataSource.RowHandle; +import com.vaadin.client.data.SelectionModel; import com.vaadin.client.renderers.ComplexRenderer; import com.vaadin.client.renderers.Renderer; import com.vaadin.client.renderers.WidgetRenderer; @@ -171,8 +172,6 @@ import com.vaadin.client.widgets.Grid.StaticSection.StaticCell; import com.vaadin.client.widgets.Grid.StaticSection.StaticRow; import com.vaadin.shared.Range; import com.vaadin.shared.Registration; -import com.vaadin.shared.data.selection.SelectionModel; -import com.vaadin.shared.data.selection.SelectionModel.Multi; import com.vaadin.shared.data.sort.SortDirection; import com.vaadin.shared.ui.grid.GridConstants; import com.vaadin.shared.ui.grid.GridConstants.Section; @@ -2865,7 +2864,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, * Later on this could be fixed so that it check such handlers * exist. */ - final SelectionModel.Multi model = (Multi) getSelectionModel(); + final SelectionModel model = getSelectionModel(); if (selectAllCheckBox == null) { selectAllCheckBox = GWT.create(CheckBox.class); @@ -5886,6 +5885,11 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, public void deselect(T item) { } + @Override + public boolean isSelected(T item) { + return false; + } + }); escalator.getBody().setSpacerUpdater(gridSpacerUpdater); @@ -7580,7 +7584,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, this.selectionModel = selectionModel; if (selectionModel instanceof SelectionModelWithSelectionColumn) { setSelectColumnRenderer( - ((SelectionModelWithSelectionColumn) selectionModel) + ((SelectionModelWithSelectionColumn) selectionModel) .getRenderer()); } else { setSelectColumnRenderer(null); @@ -8227,8 +8231,8 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, columnHider.updateTogglesOrder(); - fireEvent(new ColumnReorderEvent(oldOrder, newOrder, - isUserOriginated)); + fireEvent( + new ColumnReorderEvent<>(oldOrder, newOrder, isUserOriginated)); } /** diff --git a/server/src/main/java/com/vaadin/data/Listing.java b/server/src/main/java/com/vaadin/data/Listing.java index 667d4f4aef..397272a677 100644 --- a/server/src/main/java/com/vaadin/data/Listing.java +++ b/server/src/main/java/com/vaadin/data/Listing.java @@ -20,7 +20,6 @@ import java.util.Collection; import java.util.Set; import com.vaadin.server.data.DataSource; -import com.vaadin.shared.data.selection.SelectionModel; /** * A generic interface for components that show a list of data. diff --git a/server/src/main/java/com/vaadin/data/SelectionModel.java b/server/src/main/java/com/vaadin/data/SelectionModel.java new file mode 100644 index 0000000000..eca43e6c8d --- /dev/null +++ b/server/src/main/java/com/vaadin/data/SelectionModel.java @@ -0,0 +1,225 @@ +/* + * 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.data; + +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 + * items can be selected and deselected. + * + * @author Vaadin Ltd. + * + * @param + * the type of the items to select + * @since 8.0 + */ +public interface SelectionModel extends Serializable { + + /** + * A selection model in which at most one item can be selected at a time. + * Selecting another item deselects the originally selected item. + * + * @param + * the type of the items to select + */ + public interface Single extends SelectionModel { + + /** + * Selects the given item. If another item was already selected, that + * item is deselected. + */ + @Override + public void select(T item); + + /** + * Returns the currently selected item, or an empty optional if no item + * is selected. + * + * @return an optional of the selected item if any, an empty optional + * otherwise + */ + public Optional getSelectedItem(); + + /** + * 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 + */ + public default void setSelectedItem(T item) { + if (item != null) { + select(item); + } else { + deselectAll(); + } + } + + /** + * Returns a singleton set of the currently selected item or an empty + * set if no item is selected. + * + * @return a singleton set of the selected item if any, an empty set + * otherwise + * + * @see #getSelectedItem() + */ + @Override + public default Set getSelectedItems() { + return getSelectedItem().map(Collections::singleton) + .orElse(Collections.emptySet()); + } + } + + /** + * A selection model in which multiple items can be selected at the same + * time. Selecting an item adds it to the selection. + * + * @param + * the type of the items to select + */ + public interface Multi extends SelectionModel { + + /** + * 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 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); + } + + /** + * Returns an immutable set of the currently selected items. It is safe to + * invoke other {@code SelectionModel} methods while iterating over the set. + *

+ * Implementation note: the iteration order of the items in the + * returned set should be well-defined and documented by the implementing + * class. + * + * @return the items in the current selection, not null + */ + public Set getSelectedItems(); + + /** + * Selects the given item. Depending on the implementation, may cause other + * items to be deselected. If the item is already selected, does nothing. + * + * @param item + * the item to select, not null + */ + public void select(T item); + + /** + * Deselects the given item. If the item is not currently selected, does + * nothing. + * + * @param item + * the item to deselect, not null + */ + public void deselect(T item); + + /** + * Deselects all currently selected items. + */ + public default void deselectAll() { + getSelectedItems().forEach(this::deselect); + } + + /** + * Returns whether the given item is currently selected. + * + * @param item + * the item to check, not null + * @return {@code true} if the item is selected, {@code false} otherwise + */ + public default boolean isSelected(T item) { + return getSelectedItems().contains(item); + } +} diff --git a/server/src/main/java/com/vaadin/data/selection/AbstractSelectionModel.java b/server/src/main/java/com/vaadin/data/selection/AbstractSelectionModel.java index aa2457f589..7e60847685 100644 --- a/server/src/main/java/com/vaadin/data/selection/AbstractSelectionModel.java +++ b/server/src/main/java/com/vaadin/data/selection/AbstractSelectionModel.java @@ -15,8 +15,8 @@ */ package com.vaadin.data.selection; +import com.vaadin.data.SelectionModel; import com.vaadin.shared.data.DataCommunicatorConstants; -import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.ui.AbstractListing.AbstractListingExtension; import elemental.json.JsonObject; 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 d63f26d680..5fb91bbcbe 100644 --- a/server/src/main/java/com/vaadin/event/selection/MultiSelectionEvent.java +++ b/server/src/main/java/com/vaadin/event/selection/MultiSelectionEvent.java @@ -24,7 +24,7 @@ import com.vaadin.ui.AbstractMultiSelect; /** * Event fired when the the selection changes in a - * {@link com.vaadin.shared.data.selection.SelectionModel.Multi}. + * {@link com.vaadin.data.SelectionModel.Multi}. * * @author Vaadin Ltd * diff --git a/server/src/main/java/com/vaadin/event/selection/MultiSelectionListener.java b/server/src/main/java/com/vaadin/event/selection/MultiSelectionListener.java index c8de132341..d8005a396c 100644 --- a/server/src/main/java/com/vaadin/event/selection/MultiSelectionListener.java +++ b/server/src/main/java/com/vaadin/event/selection/MultiSelectionListener.java @@ -20,7 +20,7 @@ import java.util.function.Consumer; /** * Listens to changes from a - * {@link com.vaadin.shared.data.selection.SelectionModel.Multi}. + * {@link com.vaadin.data.SelectionModel.Multi}. * * @author Vaadin Ltd * diff --git a/server/src/main/java/com/vaadin/ui/AbstractListing.java b/server/src/main/java/com/vaadin/ui/AbstractListing.java index 0840e3fc30..3892e55662 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractListing.java +++ b/server/src/main/java/com/vaadin/ui/AbstractListing.java @@ -18,11 +18,11 @@ package com.vaadin.ui; import java.util.Objects; import com.vaadin.data.Listing; +import com.vaadin.data.SelectionModel; import com.vaadin.server.AbstractExtension; import com.vaadin.server.data.DataCommunicator; import com.vaadin.server.data.DataGenerator; import com.vaadin.server.data.DataSource; -import com.vaadin.shared.data.selection.SelectionModel; /** * A base class for listing components. Provides common handling for fetching diff --git a/server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java b/server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java index bd1fc8937a..779cd959dd 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java +++ b/server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java @@ -26,6 +26,8 @@ import java.util.function.Predicate; import java.util.stream.Collectors; import com.vaadin.data.HasValue; +import com.vaadin.data.SelectionModel; +import com.vaadin.data.SelectionModel.Multi; import com.vaadin.event.selection.MultiSelectionEvent; import com.vaadin.event.selection.MultiSelectionListener; import com.vaadin.server.Resource; @@ -35,8 +37,6 @@ import com.vaadin.server.data.DataGenerator; import com.vaadin.shared.AbstractFieldState; import com.vaadin.shared.Registration; import com.vaadin.shared.data.selection.MultiSelectServerRpc; -import com.vaadin.shared.data.selection.SelectionModel; -import com.vaadin.shared.data.selection.SelectionModel.Multi; import com.vaadin.shared.ui.ListingJsonConstants; import com.vaadin.util.ReflectTools; diff --git a/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java b/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java index c529ad3f26..cb72d52444 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java +++ b/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java @@ -20,12 +20,12 @@ import java.util.Objects; import java.util.Optional; import com.vaadin.data.HasValue; +import com.vaadin.data.SelectionModel; +import com.vaadin.data.SelectionModel.Single; import com.vaadin.event.selection.SingleSelectionChangeEvent; import com.vaadin.event.selection.SingleSelectionListener; import com.vaadin.server.data.DataCommunicator; import com.vaadin.shared.Registration; -import com.vaadin.shared.data.selection.SelectionModel; -import com.vaadin.shared.data.selection.SelectionModel.Single; import com.vaadin.shared.data.selection.SelectionServerRpc; import com.vaadin.shared.ui.AbstractSingleSelectState; import com.vaadin.util.ReflectTools; @@ -39,7 +39,7 @@ import com.vaadin.util.ReflectTools; * @param * the item date type * - * @see com.vaadin.shared.data.selection.SelectionModel.Single + * @see com.vaadin.data.SelectionModel.Single * * @since 8.0 */ diff --git a/server/src/test/java/com/vaadin/ui/AbstractMultiSelectTest.java b/server/src/test/java/com/vaadin/ui/AbstractMultiSelectTest.java index e117d01b9e..6d748f076a 100644 --- a/server/src/test/java/com/vaadin/ui/AbstractMultiSelectTest.java +++ b/server/src/test/java/com/vaadin/ui/AbstractMultiSelectTest.java @@ -37,12 +37,12 @@ import org.junit.runners.Parameterized.Parameters; import org.mockito.Mockito; import com.vaadin.data.HasValue.ValueChangeEvent; +import com.vaadin.data.SelectionModel.Multi; import com.vaadin.event.selection.MultiSelectionEvent; import com.vaadin.event.selection.MultiSelectionListener; import com.vaadin.server.data.DataSource; import com.vaadin.shared.Registration; import com.vaadin.shared.data.selection.MultiSelectServerRpc; -import com.vaadin.shared.data.selection.SelectionModel.Multi; @RunWith(Parameterized.class) public class AbstractMultiSelectTest { diff --git a/server/src/test/java/com/vaadin/ui/AbstractSingleSelectTest.java b/server/src/test/java/com/vaadin/ui/AbstractSingleSelectTest.java index 258bf8d0bd..6ae5c91b96 100644 --- a/server/src/test/java/com/vaadin/ui/AbstractSingleSelectTest.java +++ b/server/src/test/java/com/vaadin/ui/AbstractSingleSelectTest.java @@ -34,12 +34,12 @@ import org.junit.Test; import org.mockito.Mockito; import com.vaadin.data.HasValue.ValueChangeEvent; +import com.vaadin.data.SelectionModel.Multi; import com.vaadin.event.selection.SingleSelectionChangeEvent; import com.vaadin.event.selection.SingleSelectionListener; import com.vaadin.server.data.datasource.bov.Person; import com.vaadin.shared.Registration; import com.vaadin.shared.data.DataCommunicatorClientRpc; -import com.vaadin.shared.data.selection.SelectionModel.Multi; import com.vaadin.ui.AbstractSingleSelect.AbstractSingleSelection; /** diff --git a/server/src/test/java/com/vaadin/ui/RadioButtonGroupTest.java b/server/src/test/java/com/vaadin/ui/RadioButtonGroupTest.java index 6bbd9a9438..31bc2de271 100644 --- a/server/src/test/java/com/vaadin/ui/RadioButtonGroupTest.java +++ b/server/src/test/java/com/vaadin/ui/RadioButtonGroupTest.java @@ -23,9 +23,9 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import com.vaadin.data.SelectionModel; +import com.vaadin.data.SelectionModel.Multi; import com.vaadin.server.data.DataSource; -import com.vaadin.shared.data.selection.SelectionModel; -import com.vaadin.shared.data.selection.SelectionModel.Multi; import com.vaadin.shared.data.selection.SelectionServerRpc; public class RadioButtonGroupTest { 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 deleted file mode 100644 index c56fe8c4d9..0000000000 --- a/shared/src/main/java/com/vaadin/shared/data/selection/SelectionModel.java +++ /dev/null @@ -1,225 +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.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 - * items can be selected and deselected. - * - * @author Vaadin Ltd. - * - * @param - * the type of the items to select - * @since 8.0 - */ -public interface SelectionModel extends Serializable { - - /** - * A selection model in which at most one item can be selected at a time. - * Selecting another item deselects the originally selected item. - * - * @param - * the type of the items to select - */ - public interface Single extends SelectionModel { - - /** - * Selects the given item. If another item was already selected, that - * item is deselected. - */ - @Override - public void select(T item); - - /** - * Returns the currently selected item, or an empty optional if no item - * is selected. - * - * @return an optional of the selected item if any, an empty optional - * otherwise - */ - public Optional getSelectedItem(); - - /** - * 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 - */ - public default void setSelectedItem(T item) { - if (item != null) { - select(item); - } else { - deselectAll(); - } - } - - /** - * Returns a singleton set of the currently selected item or an empty - * set if no item is selected. - * - * @return a singleton set of the selected item if any, an empty set - * otherwise - * - * @see #getSelectedItem() - */ - @Override - public default Set getSelectedItems() { - return getSelectedItem().map(Collections::singleton) - .orElse(Collections.emptySet()); - } - } - - /** - * A selection model in which multiple items can be selected at the same - * time. Selecting an item adds it to the selection. - * - * @param - * the type of the items to select - */ - public interface Multi extends SelectionModel { - - /** - * 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 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); - } - - /** - * Returns an immutable set of the currently selected items. It is safe to - * invoke other {@code SelectionModel} methods while iterating over the set. - *

- * Implementation note: the iteration order of the items in the - * returned set should be well-defined and documented by the implementing - * class. - * - * @return the items in the current selection, not null - */ - public Set getSelectedItems(); - - /** - * Selects the given item. Depending on the implementation, may cause other - * items to be deselected. If the item is already selected, does nothing. - * - * @param item - * the item to select, not null - */ - public void select(T item); - - /** - * Deselects the given item. If the item is not currently selected, does - * nothing. - * - * @param item - * the item to deselect, not null - */ - public void deselect(T item); - - /** - * Deselects all currently selected items. - */ - public default void deselectAll() { - getSelectedItems().forEach(this::deselect); - } - - /** - * Returns whether the given item is currently selected. - * - * @param item - * the item to check, not null - * @return {@code true} if the item is selected, {@code false} otherwise - */ - public default boolean isSelected(T item) { - return getSelectedItems().contains(item); - } -} diff --git a/uitest/src/main/java/com/vaadin/tests/components/AbstractListingFocusBlurTest.java b/uitest/src/main/java/com/vaadin/tests/components/AbstractListingFocusBlurTest.java index 0b94b8fecb..f953c3acac 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/AbstractListingFocusBlurTest.java +++ b/uitest/src/main/java/com/vaadin/tests/components/AbstractListingFocusBlurTest.java @@ -21,10 +21,10 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; import com.googlecode.gentyref.GenericTypeReflector; +import com.vaadin.data.SelectionModel; import com.vaadin.event.FieldEvents.BlurNotifier; import com.vaadin.event.FieldEvents.FocusNotifier; import com.vaadin.server.VaadinRequest; -import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.ui.AbstractListing; /** diff --git a/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractMultiSelectTestUI.java b/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractMultiSelectTestUI.java index 71fd0fd37d..ae69cbb6ff 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractMultiSelectTestUI.java +++ b/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractMultiSelectTestUI.java @@ -5,7 +5,7 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; -import com.vaadin.shared.data.selection.SelectionModel.Multi; +import com.vaadin.data.SelectionModel.Multi; import com.vaadin.ui.AbstractMultiSelect; import com.vaadin.ui.ItemCaptionGenerator; diff --git a/uitest/src/main/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupFocusBlur.java b/uitest/src/main/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupFocusBlur.java index 92efed09e7..b92132d9f7 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupFocusBlur.java +++ b/uitest/src/main/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupFocusBlur.java @@ -15,7 +15,7 @@ */ package com.vaadin.tests.components.checkboxgroup; -import com.vaadin.shared.data.selection.SelectionModel.Multi; +import com.vaadin.data.SelectionModel.Multi; import com.vaadin.tests.components.AbstractListingFocusBlurTest; import com.vaadin.ui.CheckBoxGroup; 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 036512a514..3d8802c5ed 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 @@ -15,8 +15,8 @@ */ package com.vaadin.tests.components.radiobutton; +import com.vaadin.data.SelectionModel; import com.vaadin.server.FontAwesome; -import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.tests.components.abstractlisting.AbstractListingTestUI; import com.vaadin.ui.ItemCaptionGenerator; import com.vaadin.ui.RadioButtonGroup; diff --git a/uitest/src/main/java/com/vaadin/tests/data/DummyData.java b/uitest/src/main/java/com/vaadin/tests/data/DummyData.java index 917eb39bbd..d0ba74a942 100644 --- a/uitest/src/main/java/com/vaadin/tests/data/DummyData.java +++ b/uitest/src/main/java/com/vaadin/tests/data/DummyData.java @@ -8,12 +8,12 @@ import java.util.Set; import java.util.stream.Stream; import com.vaadin.annotations.Widgetset; +import com.vaadin.data.SelectionModel; import com.vaadin.server.VaadinRequest; import com.vaadin.server.data.DataCommunicator; import com.vaadin.server.data.ListDataSource; import com.vaadin.server.data.Query; import com.vaadin.shared.data.DataCommunicatorConstants; -import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.tests.components.AbstractTestUIWithLog; import com.vaadin.tests.widgetset.TestingWidgetSet; import com.vaadin.ui.AbstractListing; diff --git a/uitest/src/main/java/com/vaadin/tests/widgetset/client/data/DummyComponentConnector.java b/uitest/src/main/java/com/vaadin/tests/widgetset/client/data/DummyComponentConnector.java index 228f5120e7..80ae0d972a 100644 --- a/uitest/src/main/java/com/vaadin/tests/widgetset/client/data/DummyComponentConnector.java +++ b/uitest/src/main/java/com/vaadin/tests/widgetset/client/data/DummyComponentConnector.java @@ -4,15 +4,13 @@ import com.google.gwt.user.client.ui.FlowPanel; import com.vaadin.client.connectors.AbstractListingConnector; import com.vaadin.client.data.DataSource; import com.vaadin.client.ui.VLabel; -import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.shared.ui.Connect; import com.vaadin.tests.data.DummyData.DummyComponent; import elemental.json.JsonObject; @Connect(DummyComponent.class) -public class DummyComponentConnector extends - AbstractListingConnector> { +public class DummyComponentConnector extends AbstractListingConnector { @Override public FlowPanel getWidget() {