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.

SingleSelection.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.selection;
  17. import java.lang.reflect.Method;
  18. import java.util.Objects;
  19. import java.util.Optional;
  20. import com.vaadin.data.HasValue.ValueChange;
  21. import com.vaadin.event.EventListener;
  22. import com.vaadin.shared.Registration;
  23. import com.vaadin.shared.data.selection.SelectionModel.Single;
  24. import com.vaadin.shared.data.selection.SelectionServerRpc;
  25. import com.vaadin.ui.AbstractListing;
  26. import com.vaadin.util.ReflectTools;
  27. /**
  28. * A {@code SelectionModel} for selecting a single value. Implements
  29. * {@code Extension} to provide the communication logic for single selection for
  30. * the listing it extends.
  31. *
  32. * @author Vaadin Ltd.
  33. *
  34. * @param <T>
  35. * the type of the items to select
  36. *
  37. * @since 8.0
  38. */
  39. public class SingleSelection<T> extends AbstractSelectionModel<T>
  40. implements Single<T> {
  41. /**
  42. * Fired when the selection changes.
  43. *
  44. * @param <T>
  45. * the type of the selected item
  46. */
  47. public static class SingleSelectionChange<T> extends ValueChange<T> {
  48. /**
  49. * Creates a new selection change event.
  50. *
  51. * @param source
  52. * the listing that fired the event
  53. * @param selectedItem
  54. * the selected item or {@code null} if deselected
  55. * @param userOriginated
  56. * {@code true} if this event originates from the client,
  57. * {@code false} otherwise.
  58. */
  59. public SingleSelectionChange(AbstractListing<T, ?> source,
  60. T selectedItem, boolean userOriginated) {
  61. super(source, selectedItem, userOriginated);
  62. }
  63. /**
  64. * Returns an optional of the item that was selected, or an empty
  65. * optional if a previously selected item was deselected.
  66. *
  67. * @return the selected item or an empty optional if deselected
  68. *
  69. * @see SelectionModel.Single#getSelectedItem()
  70. */
  71. public Optional<T> getSelectedItem() {
  72. return Optional.ofNullable(getValue());
  73. }
  74. }
  75. /**
  76. * A listener for selection events.
  77. *
  78. * @param <T>
  79. * the type of the selected item
  80. *
  81. * @see SingleSelectionChange
  82. */
  83. @FunctionalInterface
  84. public interface SingleSelectionListener<T>
  85. extends EventListener<SingleSelectionChange<T>> {
  86. @Override
  87. public void accept(SingleSelectionChange<T> event);
  88. }
  89. @Deprecated
  90. private static final Method SELECTION_CHANGE_METHOD = ReflectTools
  91. .findMethod(SingleSelectionListener.class, "accept",
  92. SingleSelectionChange.class);
  93. /**
  94. * Creates a new {@code SingleSelection} extending the given parent listing.
  95. *
  96. * @param parent
  97. * the parent listing
  98. */
  99. public SingleSelection(
  100. AbstractListing<T, ? super SingleSelection<T>> parent) {
  101. registerRpc(new SelectionServerRpc() {
  102. @Override
  103. public void select(String key) {
  104. if (!Objects.equals(selectedItem, getData(key))) {
  105. doSelect(getData(key), true);
  106. }
  107. }
  108. @Override
  109. public void deselect(String key) {
  110. if (Objects.equals(selectedItem, getData(key))) {
  111. doSelect(null, true);
  112. }
  113. }
  114. });
  115. extend(parent);
  116. }
  117. private T selectedItem = null;
  118. @Override
  119. public Optional<T> getSelectedItem() {
  120. return Optional.ofNullable(selectedItem);
  121. }
  122. @Override
  123. public void select(T item) {
  124. doSelect(item, false);
  125. }
  126. @Override
  127. public void deselect(T value) {
  128. if (Objects.equals(selectedItem, value)) {
  129. doSelect(null, false);
  130. }
  131. }
  132. @Override
  133. public void remove() {
  134. if (selectedItem != null) {
  135. refresh(selectedItem);
  136. }
  137. super.remove();
  138. }
  139. /**
  140. * Adds a selection listener. The listener is called when the value of this
  141. * {@code SingleSelection} is changed either by the user or
  142. * programmatically.
  143. *
  144. * @param listener
  145. * the value change listener, not null
  146. * @return a registration for the listener
  147. */
  148. public Registration addSelectionListener(
  149. SingleSelectionListener<T> listener) {
  150. Objects.requireNonNull(listener, "listener cannot be null");
  151. addListener(SingleSelectionChange.class, listener,
  152. SELECTION_CHANGE_METHOD);
  153. return () -> removeListener(SingleSelectionChange.class, listener);
  154. }
  155. /**
  156. * Selects the given item or deselects the current one if given
  157. * {@code null}.
  158. *
  159. * @param item
  160. * the item to select or {@code null} to deselect
  161. * @param userOriginated
  162. * {@code true} if this event originates from the client,
  163. * {@code false} otherwise.
  164. */
  165. protected void doSelect(T item, boolean userOriginated) {
  166. if (!Objects.equals(item, selectedItem)) {
  167. if (selectedItem != null) {
  168. refresh(selectedItem);
  169. }
  170. selectedItem = item;
  171. if (selectedItem != null) {
  172. refresh(selectedItem);
  173. }
  174. fireEvent(new SingleSelectionChange<>(getParent(), selectedItem,
  175. userOriginated));
  176. }
  177. }
  178. }