summaryrefslogtreecommitdiffstats
path: root/shared
diff options
context:
space:
mode:
authorJohannes Dahlström <johannesd@vaadin.com>2016-08-24 11:26:12 +0300
committerVaadin Code Review <review@vaadin.com>2016-08-31 13:08:47 +0000
commit277b1a5c2884291ea9312d555dc45545430c1d9c (patch)
treebb44b2af9da21f0ca10287e31c24b540df89aad2 /shared
parentff3a48e73bb7318793746aa9c9ff1cf61b632c7f (diff)
downloadvaadin-framework-277b1a5c2884291ea9312d555dc45545430c1d9c.tar.gz
vaadin-framework-277b1a5c2884291ea9312d555dc45545430c1d9c.zip
Implement SingleSelection on client and server
Change-Id: I48192de092c6b6e6be7ca2580720d2765962e167
Diffstat (limited to 'shared')
-rw-r--r--shared/src/main/java/com/vaadin/shared/data/selection/SelectionModel.java129
-rw-r--r--shared/src/main/java/com/vaadin/shared/data/selection/SelectionServerRpc.java40
2 files changed, 169 insertions, 0 deletions
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
new file mode 100644
index 0000000000..8fff6ee434
--- /dev/null
+++ b/shared/src/main/java/com/vaadin/shared/data/selection/SelectionModel.java
@@ -0,0 +1,129 @@
+/*
+ * 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.Collections;
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * Models the selection logic of a {@code Listing} component. Determines how
+ * items can be selected and deselected.
+ *
+ * @author Vaadin Ltd.
+ *
+ * @param <T>
+ * the type of the items to select
+ * @since
+ */
+public interface SelectionModel<T> 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 <T>
+ * the type of the items to select
+ */
+ public interface Single<T> extends SelectionModel<T> {
+
+ /**
+ * 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<T> getSelectedItem();
+
+ /**
+ * 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
+ */
+ @Override
+ default Set<T> 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 <T>
+ * the type of the items to select
+ */
+ public interface Multi<T> extends SelectionModel<T> {
+
+ /**
+ * Adds the given items to the set of currently selected items.
+ */
+ @Override
+ public void select(T item);
+ }
+
+ /**
+ * Returns an immutable set of the currently selected items.
+ * <p>
+ * <i>Implementation note:</i> 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<T> 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);
+
+ /**
+ * 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/shared/src/main/java/com/vaadin/shared/data/selection/SelectionServerRpc.java b/shared/src/main/java/com/vaadin/shared/data/selection/SelectionServerRpc.java
new file mode 100644
index 0000000000..65cc55aebd
--- /dev/null
+++ b/shared/src/main/java/com/vaadin/shared/data/selection/SelectionServerRpc.java
@@ -0,0 +1,40 @@
+/*
+ * 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 com.vaadin.shared.communication.ServerRpc;
+
+/**
+ * Transmits SelectionModel selection events from the client to the server.
+ */
+public interface SelectionServerRpc extends ServerRpc {
+
+ /**
+ * Selects an item based on its key.
+ *
+ * @param key
+ * the key of the item to select
+ */
+ void select(String key);
+
+ /**
+ * Deselects an item based on its key.
+ *
+ * @param key
+ * the key of the item to deselect
+ */
+ void deselect(String key);
+}