]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add a simple non-connector single selection model
authorJohannes Dahlström <johannesd@vaadin.com>
Thu, 8 Sep 2016 17:53:43 +0000 (20:53 +0300)
committerVaadin Code Review <review@vaadin.com>
Mon, 12 Sep 2016 07:33:40 +0000 (07:33 +0000)
To eventually replace SingleSelection.

Change-Id: I623dfa962bce62067a5b35dc14be26b3de333e9b

server/src/main/java/com/vaadin/data/selection/SingleSelection.java
server/src/main/java/com/vaadin/event/selection/SingleSelectionChange.java [new file with mode: 0644]
server/src/main/java/com/vaadin/event/selection/SingleSelectionListener.java [new file with mode: 0644]
server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java [new file with mode: 0644]
shared/src/main/java/com/vaadin/shared/ui/AbstractSingleSelectState.java [new file with mode: 0644]

index ef637d02ba902a9038da5d1d245d813374ee5fba..81260e0cc2ab0411121d06084ea085b9abb4b4dc 100644 (file)
@@ -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 (file)
index 0000000..fe120c1
--- /dev/null
@@ -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 (file)
index 0000000..04959fd
--- /dev/null
@@ -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 (file)
index 0000000..2169222
--- /dev/null
@@ -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);
+    }
+}
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 (file)
index 0000000..3889fda
--- /dev/null
@@ -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;
+}