diff options
author | Johannes Dahlström <johannesd@vaadin.com> | 2016-09-08 20:53:43 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-09-12 07:33:40 +0000 |
commit | ddfafb7388c4f1f37034620c34e75e5d995257f7 (patch) | |
tree | a408736369ea22658aa7bda11381f1ff51e3f739 /server/src/main/java/com/vaadin | |
parent | 4ee640120878974fedff271691700cad3d198547 (diff) | |
download | vaadin-framework-ddfafb7388c4f1f37034620c34e75e5d995257f7.tar.gz vaadin-framework-ddfafb7388c4f1f37034620c34e75e5d995257f7.zip |
Add a simple non-connector single selection model
To eventually replace SingleSelection.
Change-Id: I623dfa962bce62067a5b35dc14be26b3de333e9b
Diffstat (limited to 'server/src/main/java/com/vaadin')
4 files changed, 273 insertions, 55 deletions
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<T> extends AbstractSelectionModel<T> implements Single<T> { - /** - * Fired when the selection changes. - * - * @param <T> - * the type of the selected item - */ - public static class SingleSelectionChange<T> extends ValueChange<T> { - - /** - * 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<T, ?> 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<T> getSelectedItem() { - return Optional.ofNullable(getValue()); - } - } - - /** - * A listener for selection events. - * - * @param <T> - * the type of the selected item - * - * @see SingleSelectionChange - */ - @FunctionalInterface - public interface SingleSelectionListener<T> - extends EventListener<SingleSelectionChange<T>> { - - @Override - public void accept(SingleSelectionChange<T> 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 <T> + * the type of the selected item + * @since 8.0 + */ +public class SingleSelectionChange<T> extends ValueChange<T> { + + /** + * 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<T, ?> 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<T> getSelectedItem() { + return Optional.ofNullable(getValue()); + } + + @Override + @SuppressWarnings("unchecked") + public AbstractListing<T, ?> getSource() { + return (AbstractListing<T, ?>) 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 <T> + * the type of the selected item + * + * @see SingleSelectionChange + * + * @since 8.0 + */ +@FunctionalInterface +public interface SingleSelectionListener<T> extends + EventListener<SingleSelectionChange<T>> { + + @Override + public void accept(SingleSelectionChange<T> 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 <T> + * the item date type + * + * @see com.vaadin.shared.data.selection.SelectionModel.Single + * + * @since + */ +public abstract class AbstractSingleSelect<T> extends + AbstractListing<T, AbstractSingleSelect<T>.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<T> { + + /** + * 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<T> 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<T> 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<T> getSelectedItem() { + return getSelectionModel().getSelectedItem(); + } + + @Override + protected AbstractSingleSelectState getState() { + return (AbstractSingleSelectState) super.getState(); + } + + @Override + protected AbstractSingleSelectState getState(boolean markAsDirty) { + return (AbstractSingleSelectState) super.getState(markAsDirty); + } +} |