From ddfafb7388c4f1f37034620c34e75e5d995257f7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Johannes=20Dahlstr=C3=B6m?= Date: Thu, 8 Sep 2016 20:53:43 +0300 Subject: [PATCH] Add a simple non-connector single selection model To eventually replace SingleSelection. Change-Id: I623dfa962bce62067a5b35dc14be26b3de333e9b --- .../data/selection/SingleSelection.java | 57 +----- .../selection/SingleSelectionChange.java | 67 +++++++ .../selection/SingleSelectionListener.java | 38 ++++ .../com/vaadin/ui/AbstractSingleSelect.java | 166 ++++++++++++++++++ .../shared/ui/AbstractSingleSelectState.java | 34 ++++ 5 files changed, 307 insertions(+), 55 deletions(-) create mode 100644 server/src/main/java/com/vaadin/event/selection/SingleSelectionChange.java create mode 100644 server/src/main/java/com/vaadin/event/selection/SingleSelectionListener.java create mode 100644 server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java create mode 100644 shared/src/main/java/com/vaadin/shared/ui/AbstractSingleSelectState.java diff --git a/server/src/main/java/com/vaadin/data/selection/SingleSelection.java b/server/src/main/java/com/vaadin/data/selection/SingleSelection.java index ef637d02ba..81260e0cc2 100644 --- a/server/src/main/java/com/vaadin/data/selection/SingleSelection.java +++ b/server/src/main/java/com/vaadin/data/selection/SingleSelection.java @@ -19,8 +19,8 @@ import java.lang.reflect.Method; import java.util.Objects; import java.util.Optional; -import com.vaadin.data.HasValue.ValueChange; -import com.vaadin.event.EventListener; +import com.vaadin.event.selection.SingleSelectionChange; +import com.vaadin.event.selection.SingleSelectionListener; import com.vaadin.shared.Registration; import com.vaadin.shared.data.selection.SelectionModel.Single; import com.vaadin.shared.data.selection.SelectionServerRpc; @@ -42,59 +42,6 @@ import com.vaadin.util.ReflectTools; public class SingleSelection extends AbstractSelectionModel implements Single { - /** - * Fired when the selection changes. - * - * @param - * the type of the selected item - */ - public static class SingleSelectionChange extends ValueChange { - - /** - * Creates a new selection change event. - * - * @param source - * the listing that fired the event - * @param selectedItem - * the selected item or {@code null} if deselected - * @param userOriginated - * {@code true} if this event originates from the client, - * {@code false} otherwise. - */ - public SingleSelectionChange(AbstractListing source, - T selectedItem, boolean userOriginated) { - super(source, selectedItem, userOriginated); - } - - /** - * Returns an optional of the item that was selected, or an empty - * optional if a previously selected item was deselected. - * - * @return the selected item or an empty optional if deselected - * - * @see SelectionModel.Single#getSelectedItem() - */ - public Optional getSelectedItem() { - return Optional.ofNullable(getValue()); - } - } - - /** - * A listener for selection events. - * - * @param - * the type of the selected item - * - * @see SingleSelectionChange - */ - @FunctionalInterface - public interface SingleSelectionListener - extends EventListener> { - - @Override - public void accept(SingleSelectionChange event); - } - @Deprecated private static final Method SELECTION_CHANGE_METHOD = ReflectTools .findMethod(SingleSelectionListener.class, "accept", diff --git a/server/src/main/java/com/vaadin/event/selection/SingleSelectionChange.java b/server/src/main/java/com/vaadin/event/selection/SingleSelectionChange.java new file mode 100644 index 0000000000..fe120c1692 --- /dev/null +++ b/server/src/main/java/com/vaadin/event/selection/SingleSelectionChange.java @@ -0,0 +1,67 @@ +/* + * 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.Optional; + +import com.vaadin.data.HasValue.ValueChange; +import com.vaadin.ui.AbstractListing; + +/** + * Fired when the selection changes in a listing component. + * + * @author Vaadin Ltd. + * + * @param + * the type of the selected item + * @since 8.0 + */ +public class SingleSelectionChange extends ValueChange { + + /** + * Creates a new selection change event. + * + * @param source + * the listing that fired the event + * @param selectedItem + * the selected item or {@code null} if deselected + * @param userOriginated + * {@code true} if this event originates from the client, + * {@code false} otherwise. + */ + public SingleSelectionChange(AbstractListing source, + T selectedItem, boolean userOriginated) { + super(source, selectedItem, userOriginated); + } + + /** + * Returns an optional of the item that was selected, or an empty optional + * if a previously selected item was deselected. + * + * @return the selected item or an empty optional if deselected + * + * @see SelectionModel.Single#getSelectedItem() + */ + public Optional getSelectedItem() { + return Optional.ofNullable(getValue()); + } + + @Override + @SuppressWarnings("unchecked") + public AbstractListing getSource() { + return (AbstractListing) super.getSource(); + } +} diff --git a/server/src/main/java/com/vaadin/event/selection/SingleSelectionListener.java b/server/src/main/java/com/vaadin/event/selection/SingleSelectionListener.java new file mode 100644 index 0000000000..04959fddd9 --- /dev/null +++ b/server/src/main/java/com/vaadin/event/selection/SingleSelectionListener.java @@ -0,0 +1,38 @@ +/* + * 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.event.EventListener; + +/** + * A listener for {@code SingleSelectionChange} events. + * + * @author Vaadin Ltd. + * + * @param + * the type of the selected item + * + * @see SingleSelectionChange + * + * @since 8.0 + */ +@FunctionalInterface +public interface SingleSelectionListener extends + EventListener> { + + @Override + public void accept(SingleSelectionChange event); +} diff --git a/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java b/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java new file mode 100644 index 0000000000..2169222c53 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java @@ -0,0 +1,166 @@ +/* + * 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 java.util.Optional; + +import com.vaadin.event.selection.SingleSelectionChange; +import com.vaadin.event.selection.SingleSelectionListener; +import com.vaadin.shared.Registration; +import com.vaadin.shared.data.selection.SelectionModel; +import com.vaadin.shared.data.selection.SelectionServerRpc; +import com.vaadin.shared.ui.AbstractSingleSelectState; +import com.vaadin.util.ReflectTools; + +/** + * An abstract base class for listing components that only support single + * selection and no lazy loading of data items. + * + * @author Vaadin Ltd. + * + * @param + * the item date type + * + * @see com.vaadin.shared.data.selection.SelectionModel.Single + * + * @since + */ +public abstract class AbstractSingleSelect extends + AbstractListing.SimpleSingleSelection> { + + /** + * A simple single selection model using the {@code AbstractSingleSelect} + * RPC and state to communicate with the client. Has no client-side + * counterpart; the listing connector is expected to handle selection. + * Client-to-server selection is passed via {@link SelectionServerRpc} and + * server-to-client via {@link AbstractSingleSelectState#selectedItemKey}. + */ + public class SimpleSingleSelection implements SelectionModel.Single { + + /** + * Creates a new {@code SimpleSingleSelection} instance. + */ + public SimpleSingleSelection() { + registerRpc(new SelectionServerRpc() { + + @Override + public void select(String key) { + setSelectedKey(key); + } + + @Override + public void deselect(String key) { + if (Objects.equals(key, getState(false).selectedItemKey)) { + setSelectedKey(null); + } + } + }); + } + + @Override + public void deselect(T item) { + Objects.requireNonNull(item, "deselected item cannot be null"); + // TODO creates a key if item not in data source + String key = getDataCommunicator().getKeyMapper().key(item); + if (Objects.equals(key, getState(false).selectedItemKey)) { + setSelectedItem(null); + } + } + + @Override + public void select(T item) { + Objects.requireNonNull(item, "selected item cannot be null"); + setSelectedItem(item); + } + + @Override + public Optional getSelectedItem() { + return Optional.ofNullable(getState(false).selectedItemKey).map( + getDataCommunicator().getKeyMapper()::get); + } + + private void setSelectedKey(String key) { + if (isReadOnly()) { + return; + } + if (Objects.equals(key, getState(false).selectedItemKey)) { + return; + } + + getState().selectedItemKey = key; + fireEvent(new SingleSelectionChange<>(AbstractSingleSelect.this, + getSelectedItem().orElse(null), true)); + } + + private void setSelectedItem(T item) { + // TODO creates a key if item not in data source + String key = Optional.ofNullable(item).map(getDataCommunicator() + .getKeyMapper()::key).orElse(null); + + if (Objects.equals(key, getState(false).selectedItemKey)) { + return; + } + + getState().selectedItemKey = key; + fireEvent(new SingleSelectionChange<>(AbstractSingleSelect.this, + item, false)); + } + } + + @Deprecated + private static final Method SELECTION_CHANGE_METHOD = ReflectTools + .findMethod(SingleSelectionListener.class, "accept", + SingleSelectionChange.class); + + /** + * 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( + SingleSelectionListener listener) { + Objects.requireNonNull(listener, "listener cannot be null"); + addListener(SingleSelectionChange.class, listener, + SELECTION_CHANGE_METHOD); + return () -> removeListener(SingleSelectionChange.class, listener); + } + + /** + * 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() { + return getSelectionModel().getSelectedItem(); + } + + @Override + protected AbstractSingleSelectState getState() { + return (AbstractSingleSelectState) super.getState(); + } + + @Override + protected AbstractSingleSelectState getState(boolean markAsDirty) { + return (AbstractSingleSelectState) super.getState(markAsDirty); + } +} diff --git a/shared/src/main/java/com/vaadin/shared/ui/AbstractSingleSelectState.java b/shared/src/main/java/com/vaadin/shared/ui/AbstractSingleSelectState.java new file mode 100644 index 0000000000..3889fdae2b --- /dev/null +++ b/shared/src/main/java/com/vaadin/shared/ui/AbstractSingleSelectState.java @@ -0,0 +1,34 @@ +/* + * 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 com.vaadin.shared.AbstractComponentState; + +/** + * Shared state for {@code AbstractSingleSelect}. + * + * @author Vaadin Ltd. + * + * @since 8.0 + */ +public class AbstractSingleSelectState extends AbstractComponentState { + + /** + * The key of the currently selected item or {@code null} if no item is + * selected. + */ + public String selectedItemKey; +} -- 2.39.5