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.

HasValue.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 com.vaadin.event.ConnectorEvent;
  19. import com.vaadin.event.EventListener;
  20. import com.vaadin.server.ClientConnector;
  21. import com.vaadin.shared.Registration;
  22. /**
  23. * A generic interface for field components and other user interface objects
  24. * that have a user-editable value. Emits change events whenever the value is
  25. * changed, either by the user or programmatically.
  26. *
  27. * @author Vaadin Ltd.
  28. *
  29. * @param <V>
  30. * the value type
  31. *
  32. * @since
  33. */
  34. public interface HasValue<V> extends Serializable {
  35. /**
  36. * An event fired when the value of a {@code HasValue} changes.
  37. *
  38. * @param <V>
  39. * the value type
  40. */
  41. public class ValueChange<V> extends ConnectorEvent {
  42. private final V value;
  43. private final boolean userOriginated;
  44. /**
  45. * Creates a new {@code ValueChange} event containing the current value
  46. * of the given value-bearing source connector.
  47. *
  48. * @param <CONNECTOR>
  49. * the type of the source connector
  50. * @param source
  51. * the source connector bearing the value, not null
  52. * @param userOriginated
  53. * {@code true} if this event originates from the client,
  54. * {@code false} otherwise.
  55. */
  56. public <CONNECTOR extends ClientConnector & HasValue<V>> ValueChange(
  57. CONNECTOR source, boolean userOriginated) {
  58. this(source, source.getValue(), userOriginated);
  59. }
  60. /**
  61. * Creates a new {@code ValueChange} event containing the given value,
  62. * originating from the given source connector.
  63. *
  64. * @param source
  65. * the source connector, not null
  66. * @param value
  67. * the new value, may be null
  68. * @param userOriginated
  69. * {@code true} if this event originates from the client,
  70. * {@code false} otherwise.
  71. */
  72. public ValueChange(ClientConnector source, V value,
  73. boolean userOriginated) {
  74. super(source);
  75. this.value = value;
  76. this.userOriginated = userOriginated;
  77. }
  78. /**
  79. * Returns the new value of the source connector.
  80. *
  81. * @return the new value
  82. */
  83. public V getValue() {
  84. return value;
  85. }
  86. /**
  87. * Returns whether this event was triggered by user interaction, on the
  88. * client side, or programmatically, on the server side.
  89. *
  90. * @return {@code true} if this event originates from the client,
  91. * {@code false} otherwise.
  92. */
  93. public boolean isUserOriginated() {
  94. return userOriginated;
  95. }
  96. }
  97. /**
  98. * A listener for value change events.
  99. *
  100. * @param <V>
  101. * the value type
  102. *
  103. * @see ValueChange
  104. * @see Registration
  105. */
  106. @FunctionalInterface
  107. public interface ValueChangeListener<V> extends
  108. EventListener<ValueChange<V>> {
  109. /**
  110. * Invoked when this listener receives a value change event from an
  111. * event source to which it has been added.
  112. *
  113. * @param event
  114. * the received event, not null
  115. */
  116. @Override
  117. public void accept(ValueChange<V> event);
  118. }
  119. /**
  120. * Sets the value of this object. If the new value is not equal to
  121. * {@code getValue()}, fires a value change event. May throw
  122. * {@code IllegalArgumentException} if the value is not acceptable.
  123. * <p>
  124. * <i>Implementation note:</i> the implementing class should document
  125. * whether null values are accepted or not.
  126. *
  127. * @param value
  128. * the new value
  129. * @throws IllegalArgumentException
  130. * if the value is invalid
  131. */
  132. public void setValue(V value);
  133. /**
  134. * Returns the current value of this object.
  135. * <p>
  136. * <i>Implementation note:</i> the implementing class should document
  137. * whether null values may be returned or not.
  138. *
  139. * @return the current value
  140. */
  141. public V getValue();
  142. /**
  143. * Adds a value change listener. The listener is called when the value of
  144. * this {@code hasValue} is changed either by the user or programmatically.
  145. *
  146. * @param listener
  147. * the value change listener, not null
  148. * @return a registration for the listener
  149. */
  150. public Registration addValueChangeListener(
  151. ValueChangeListener<? super V> listener);
  152. }