From 2a0c590bfae9b630838ae8f85637543c2a7d160f Mon Sep 17 00:00:00 2001 From: Johannes Dahlström Date: Tue, 13 Sep 2016 16:48:39 +0300 Subject: Implement support for binding single-select components Change-Id: I340e802e5c8e6e036b54f81ec46beeb5e1c34329 --- .../shared/data/selection/SelectionModel.java | 23 +++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'shared') 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 index 115c43013b..8711d6a9c8 100644 --- a/shared/src/main/java/com/vaadin/shared/data/selection/SelectionModel.java +++ b/shared/src/main/java/com/vaadin/shared/data/selection/SelectionModel.java @@ -57,12 +57,29 @@ public interface SelectionModel extends Serializable { */ 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 default Set getSelectedItems() { @@ -85,13 +102,13 @@ public interface SelectionModel extends Serializable { */ @Override public void select(T item); - } /** - * Returns an immutable set of the currently selected items. + * 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 + * Implementation note: the iteration order of the items in the * returned set should be well-defined and documented by the implementing * class. * -- cgit v1.2.3