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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.shared.data.selection;
  17. import java.io.Serializable;
  18. import java.util.Collections;
  19. import java.util.Optional;
  20. import java.util.Set;
  21. /**
  22. * Models the selection logic of a {@code Listing} component. Determines how
  23. * items can be selected and deselected.
  24. *
  25. * @author Vaadin Ltd.
  26. *
  27. * @param <T>
  28. * the type of the items to select
  29. * @since
  30. */
  31. public interface SelectionModel<T> extends Serializable {
  32. /**
  33. * A selection model in which at most one item can be selected at a time.
  34. * Selecting another item deselects the originally selected item.
  35. *
  36. * @param <T>
  37. * the type of the items to select
  38. */
  39. public interface Single<T> extends SelectionModel<T> {
  40. /**
  41. * Selects the given item. If another item was already selected, that
  42. * item is deselected.
  43. */
  44. @Override
  45. public void select(T item);
  46. /**
  47. * Returns the currently selected item, or an empty optional if no item
  48. * is selected.
  49. *
  50. * @return an optional of the selected item if any, an empty optional
  51. * otherwise
  52. */
  53. public Optional<T> getSelectedItem();
  54. /**
  55. * Returns a singleton set of the currently selected item or an empty
  56. * set if no item is selected.
  57. *
  58. * @return a singleton set of the selected item if any, an empty set
  59. * otherwise
  60. */
  61. @Override
  62. default Set<T> getSelectedItems() {
  63. return getSelectedItem().map(Collections::singleton)
  64. .orElse(Collections.emptySet());
  65. }
  66. }
  67. /**
  68. * A selection model in which multiple items can be selected at the same
  69. * time. Selecting an item adds it to the selection.
  70. *
  71. * @param <T>
  72. * the type of the items to select
  73. */
  74. public interface Multi<T> extends SelectionModel<T> {
  75. /**
  76. * Adds the given items to the set of currently selected items.
  77. */
  78. @Override
  79. public void select(T item);
  80. }
  81. /**
  82. * Returns an immutable set of the currently selected items.
  83. * <p>
  84. * <i>Implementation note:</i> the iteration order of the items in the
  85. * returned set should be well-defined and documented by the implementing
  86. * class.
  87. *
  88. * @return the items in the current selection, not null
  89. */
  90. public Set<T> getSelectedItems();
  91. /**
  92. * Selects the given item. Depending on the implementation, may cause other
  93. * items to be deselected. If the item is already selected, does nothing.
  94. *
  95. * @param item
  96. * the item to select, not null
  97. */
  98. public void select(T item);
  99. /**
  100. * Deselects the given item. If the item is not currently selected, does
  101. * nothing.
  102. *
  103. * @param item
  104. * the item to deselect, not null
  105. */
  106. public void deselect(T item);
  107. /**
  108. * Returns whether the given item is currently selected.
  109. *
  110. * @param item
  111. * the item to check, not null
  112. * @return {@code true} if the item is selected, {@code false} otherwise
  113. */
  114. public default boolean isSelected(T item) {
  115. return getSelectedItems().contains(item);
  116. }
  117. }