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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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
  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> extends
  85. 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. doSelect(getData(key), true);
  105. }
  106. @Override
  107. public void deselect(String key) {
  108. if (getData(key).equals(selectedItem)) {
  109. doSelect(null, true);
  110. }
  111. }
  112. });
  113. extend(parent);
  114. }
  115. private T selectedItem = null;
  116. @Override
  117. public Optional<T> getSelectedItem() {
  118. return Optional.ofNullable(selectedItem);
  119. }
  120. @Override
  121. public void select(T value) {
  122. doSelect(value, false);
  123. }
  124. @Override
  125. public void deselect(T value) {
  126. this.selectedItem = null;
  127. }
  128. @Override
  129. public void remove() {
  130. if (selectedItem != null) {
  131. refresh(selectedItem);
  132. }
  133. super.remove();
  134. }
  135. /**
  136. * Adds a selection listener. The listener is called when the value of this
  137. * {@code SingleSelection} is changed either by the user or
  138. * programmatically.
  139. *
  140. * @param listener
  141. * the value change listener, not null
  142. * @return a registration for the listener
  143. */
  144. public Registration addSelectionListener(
  145. SingleSelectionListener<T> listener) {
  146. Objects.requireNonNull(listener, "listener cannot be null");
  147. addListener(SingleSelectionChange.class, listener,
  148. SELECTION_CHANGE_METHOD);
  149. return () -> removeListener(SingleSelectionChange.class, listener);
  150. }
  151. /**
  152. * Selects the given item or deselects the current one if given
  153. * {@code null}.
  154. *
  155. * @param value
  156. * the item to select or {@code null} to deselect
  157. * @param userOriginated
  158. * {@code true} if this event originates from the client,
  159. * {@code false} otherwise.
  160. */
  161. protected void doSelect(T value, boolean userOriginated) {
  162. if (!Objects.equals(value, this.selectedItem)) {
  163. this.selectedItem = value;
  164. fireEvent(new SingleSelectionChange<>(getParent(), value,
  165. userOriginated));
  166. }
  167. }
  168. }