You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SelectionModel.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data;
  17. import java.io.Serializable;
  18. import java.util.Arrays;
  19. import java.util.Collections;
  20. import java.util.LinkedHashSet;
  21. import java.util.Objects;
  22. import java.util.Optional;
  23. import java.util.Set;
  24. import java.util.stream.Stream;
  25. import com.vaadin.event.selection.SelectionListener;
  26. import com.vaadin.shared.Registration;
  27. /**
  28. * Models the selection logic of a {@code Listing} component. Determines how
  29. * items can be selected and deselected.
  30. *
  31. * @author Vaadin Ltd.
  32. *
  33. * @param <T>
  34. * the type of the items to select
  35. * @since 8.0
  36. */
  37. public interface SelectionModel<T> extends Serializable {
  38. /**
  39. * A selection model in which at most one item can be selected at a time.
  40. * Selecting another item deselects the originally selected item.
  41. *
  42. * @param <T>
  43. * the type of the items to select
  44. */
  45. public interface Single<T> extends SelectionModel<T> {
  46. /**
  47. * Selects the given item. If another item was already selected, that
  48. * item is deselected.
  49. */
  50. @Override
  51. public void select(T item);
  52. /**
  53. * Returns the currently selected item, or an empty optional if no item
  54. * is selected.
  55. *
  56. * @return an optional of the selected item if any, an empty optional
  57. * otherwise
  58. */
  59. public Optional<T> getSelectedItem();
  60. /**
  61. * Sets the current selection to the given item, or clears selection if
  62. * given {@code null}.
  63. *
  64. * @param item
  65. * the item to select or {@code null} to clear selection
  66. */
  67. public default void setSelectedItem(T item) {
  68. if (item != null) {
  69. select(item);
  70. } else {
  71. getSelectedItem().ifPresent(this::deselect);
  72. }
  73. }
  74. @Override
  75. public default void deselectAll() {
  76. setSelectedItem(null);
  77. }
  78. /**
  79. * Returns a singleton set of the currently selected item or an empty
  80. * set if no item is selected.
  81. *
  82. * @return a singleton set of the selected item if any, an empty set
  83. * otherwise
  84. *
  85. * @see #getSelectedItem()
  86. */
  87. @Override
  88. public default Set<T> getSelectedItems() {
  89. return getSelectedItem().map(Collections::singleton)
  90. .orElse(Collections.emptySet());
  91. }
  92. @Override
  93. default Optional<T> getFirstSelectedItem() {
  94. return getSelectedItem();
  95. }
  96. /**
  97. * Sets whether it's allowed to deselect the selected row through the
  98. * UI. Deselection is allowed by default.
  99. *
  100. * @param deselectAllowed
  101. * <code>true</code> if the selected row can be deselected
  102. * without selecting another row instead; otherwise
  103. * <code>false</code>.
  104. */
  105. public void setDeselectAllowed(boolean deselectAllowed);
  106. /**
  107. * Gets whether it's allowed to deselect the selected row through the
  108. * UI.
  109. *
  110. * @return <code>true</code> if deselection is allowed; otherwise
  111. * <code>false</code>
  112. */
  113. public boolean isDeselectAllowed();
  114. }
  115. /**
  116. * A selection model in which multiple items can be selected at the same
  117. * time. Selecting an item adds it to the selection.
  118. *
  119. * @param <T>
  120. * the type of the items to select
  121. */
  122. public interface Multi<T> extends SelectionModel<T> {
  123. /**
  124. * Adds the given item to the set of currently selected items.
  125. * <p>
  126. * By default this does not clear any previous selection. To do that,
  127. * use {@link #deselectAll()}.
  128. * <p>
  129. * If the the item was already selected, this is a NO-OP.
  130. *
  131. * @param item
  132. * the item to add to selection, not {@code null}
  133. */
  134. @Override
  135. public default void select(T item) {
  136. Objects.requireNonNull(item);
  137. selectItems(item);
  138. };
  139. /**
  140. * Adds the given items to the set of currently selected items.
  141. * <p>
  142. * By default this does not clear any previous selection. To do that,
  143. * use {@link #deselectAll()}.
  144. * <p>
  145. * If the all the items were already selected, this is a NO-OP.
  146. * <p>
  147. * This is a short-hand for {@link #updateSelection(Set, Set)} with
  148. * nothing to deselect.
  149. *
  150. * @param items
  151. * to add to selection, not {@code null}
  152. */
  153. public default void selectItems(T... items) {
  154. Objects.requireNonNull(items);
  155. Stream.of(items).forEach(Objects::requireNonNull);
  156. updateSelection(new LinkedHashSet<>(Arrays.asList(items)),
  157. Collections.emptySet());
  158. }
  159. @SuppressWarnings("unchecked")
  160. @Override
  161. public default void deselect(T item) {
  162. deselectItems(item);
  163. }
  164. /**
  165. * Removes the given items from the set of currently selected items.
  166. * <p>
  167. * If the none of the items were selected, this is a NO-OP.
  168. * <p>
  169. * This is a short-hand for {@link #updateSelection(Set, Set)} with
  170. * nothing to select.
  171. *
  172. * @param items
  173. * to remove from selection, not {@code null}
  174. */
  175. public default void deselectItems(T... items) {
  176. Objects.requireNonNull(items);
  177. Stream.of(items).forEach(Objects::requireNonNull);
  178. updateSelection(Collections.emptySet(),
  179. new LinkedHashSet<>(Arrays.asList(items)));
  180. }
  181. /**
  182. * Updates the selection by adding and removing the given items from it.
  183. * <p>
  184. * If all the added items were already selected and the removed items
  185. * were not selected, this is a NO-OP.
  186. * <p>
  187. * Duplicate items (in both add &amp; remove sets) are ignored.
  188. *
  189. * @param addedItems
  190. * the items to add, not {@code null}
  191. * @param removedItems
  192. * the items to remove, not {@code null}
  193. */
  194. public void updateSelection(Set<T> addedItems, Set<T> removedItems);
  195. @Override
  196. default Optional<T> getFirstSelectedItem() {
  197. return getSelectedItems().stream().findFirst();
  198. }
  199. /**
  200. * Selects all available the items.
  201. */
  202. public void selectAll();
  203. }
  204. /**
  205. * Returns an immutable set of the currently selected items. It is safe to
  206. * invoke other {@code SelectionModel} methods while iterating over the set.
  207. * <p>
  208. * <em>Implementation note:</em> the iteration order of the items in the
  209. * returned set should be well-defined and documented by the implementing
  210. * class.
  211. *
  212. * @return the items in the current selection, not null
  213. */
  214. public Set<T> getSelectedItems();
  215. /**
  216. * Get first selected data item.
  217. * <p>
  218. * This is the same as {@link Single#getSelectedItem()} in case of single
  219. * selection and the first selected item from
  220. * {@link Multi#getSelectedItems()} in case of multiselection.
  221. *
  222. * @return the first selected item.
  223. */
  224. Optional<T> getFirstSelectedItem();
  225. /**
  226. * Selects the given item. Depending on the implementation, may cause other
  227. * items to be deselected. If the item is already selected, does nothing.
  228. *
  229. * @param item
  230. * the item to select, not null
  231. */
  232. public void select(T item);
  233. /**
  234. * Deselects the given item. If the item is not currently selected, does
  235. * nothing.
  236. *
  237. * @param item
  238. * the item to deselect, not null
  239. */
  240. public void deselect(T item);
  241. /**
  242. * Deselects all currently selected items, if any.
  243. */
  244. public void deselectAll();
  245. /**
  246. * Returns whether the given item is currently selected.
  247. *
  248. * @param item
  249. * the item to check, not null
  250. * @return {@code true} if the item is selected, {@code false} otherwise
  251. */
  252. public default boolean isSelected(T item) {
  253. return getSelectedItems().contains(item);
  254. }
  255. /**
  256. * Adds a generic listener to this selection model, accepting both single
  257. * and multiselection events.
  258. *
  259. * @param listener
  260. * the listener to add
  261. * @return a registration handle for removing the listener
  262. */
  263. public Registration addSelectionListener(SelectionListener<T> listener);
  264. }