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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright 2000-2018 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.lang.reflect.Method;
  19. import java.util.EventObject;
  20. import java.util.Objects;
  21. import java.util.Optional;
  22. import com.vaadin.event.HasUserOriginated;
  23. import com.vaadin.event.SerializableEventListener;
  24. import com.vaadin.shared.Registration;
  25. import com.vaadin.ui.Component;
  26. import com.vaadin.util.ReflectTools;
  27. /**
  28. * A generic interface for field components and other user interface objects
  29. * that have a user-editable value. Emits change events whenever the value is
  30. * changed, either by the user or programmatically.
  31. *
  32. * @author Vaadin Ltd.
  33. *
  34. * @param <V>
  35. * the value type
  36. *
  37. * @since 8.0
  38. */
  39. public interface HasValue<V> extends Serializable {
  40. /**
  41. * An event fired when the value of a {@code HasValue} changes.
  42. *
  43. * @param <V>
  44. * the value type
  45. */
  46. public class ValueChangeEvent<V> extends EventObject
  47. implements HasUserOriginated {
  48. private final boolean userOriginated;
  49. private final Component component;
  50. private final V oldValue;
  51. private final V value;
  52. /**
  53. * Creates a new {@code ValueChange} event containing the current value
  54. * of the given value-bearing source component.
  55. *
  56. * @param <COMPONENT>
  57. * the type of the source component
  58. * @param component
  59. * the source component bearing the value, not null
  60. * @param oldValue
  61. * the previous value held by the source of this event
  62. * @param userOriginated
  63. * {@code true} if this event originates from the client,
  64. * {@code false} otherwise.
  65. */
  66. public <COMPONENT extends Component & HasValue<V>> ValueChangeEvent(
  67. COMPONENT component, V oldValue, boolean userOriginated) {
  68. this(component, component, oldValue, userOriginated);
  69. }
  70. /**
  71. * Creates a new {@code ValueChange} event containing the given value,
  72. * originating from the given source component.
  73. *
  74. * @param component
  75. * the component, not null
  76. * @param hasValue
  77. * the HasValue instance bearing the value, not null
  78. * @param oldValue
  79. * the previous value held by the source of this event
  80. * @param userOriginated
  81. * {@code true} if this event originates from the client,
  82. * {@code false} otherwise.
  83. */
  84. public ValueChangeEvent(Component component, HasValue<V> hasValue,
  85. V oldValue, boolean userOriginated) {
  86. super(hasValue);
  87. this.userOriginated = userOriginated;
  88. this.component = component;
  89. this.oldValue = oldValue;
  90. value = hasValue.getValue();
  91. }
  92. /**
  93. * Returns the value of the source before this value change event
  94. * occurred.
  95. *
  96. * @return the value previously held by the source of this event
  97. */
  98. public V getOldValue() {
  99. return oldValue;
  100. }
  101. /**
  102. * Returns the new value that triggered this value change event.
  103. *
  104. * @return the new value
  105. */
  106. public V getValue() {
  107. return value;
  108. }
  109. @Override
  110. public boolean isUserOriginated() {
  111. return userOriginated;
  112. }
  113. /**
  114. * Returns the component.
  115. *
  116. * @return the component, not null
  117. */
  118. public Component getComponent() {
  119. return component;
  120. }
  121. @SuppressWarnings("unchecked")
  122. @Override
  123. public HasValue<V> getSource() {
  124. return (HasValue<V>) super.getSource();
  125. }
  126. }
  127. /**
  128. * A listener for value change events.
  129. *
  130. * @param <V>
  131. * the value type
  132. *
  133. * @see ValueChangeEvent
  134. * @see Registration
  135. */
  136. @FunctionalInterface
  137. public interface ValueChangeListener<V> extends SerializableEventListener {
  138. /** For internal use only. Might be removed in the future. */
  139. @Deprecated
  140. public static final Method VALUE_CHANGE_METHOD = ReflectTools
  141. .findMethod(ValueChangeListener.class, "valueChange",
  142. ValueChangeEvent.class);
  143. /**
  144. * Invoked when this listener receives a value change event from an
  145. * event source to which it has been added.
  146. *
  147. * @param event
  148. * the received event, not null
  149. */
  150. public void valueChange(ValueChangeEvent<V> event);
  151. }
  152. /**
  153. * Sets the value of this object. If the new value is not equal to
  154. * {@code getValue()}, fires a value change event. May throw
  155. * {@code IllegalArgumentException} if the value is not acceptable.
  156. * <p>
  157. * <i>Implementation note:</i> the implementing class should document
  158. * whether null values are accepted or not.
  159. *
  160. * @param value
  161. * the new value
  162. * @throws IllegalArgumentException
  163. * if the value is invalid
  164. */
  165. public void setValue(V value);
  166. /**
  167. * Returns the current value of this object.
  168. * <p>
  169. * <i>Implementation note:</i> the implementing class should document
  170. * whether null values may be returned or not.
  171. *
  172. * @return the current value
  173. */
  174. public V getValue();
  175. /**
  176. * Adds a value change listener. The listener is called when the value of
  177. * this {@code HasValue} is changed either by the user or programmatically.
  178. *
  179. * @param listener
  180. * the value change listener, not null
  181. * @return a registration for the listener
  182. */
  183. public Registration addValueChangeListener(ValueChangeListener<V> listener);
  184. /**
  185. * Returns the value that represents an empty value.
  186. * <p>
  187. * By default {@link HasValue} is expected to support {@code null} as empty
  188. * values. Specific implementations might not support this.
  189. *
  190. * @return empty value
  191. * @see Binder#bind(HasValue, ValueProvider, com.vaadin.server.Setter)
  192. * Binder#bind(HasValue, ValueProvider, Setter)
  193. */
  194. public default V getEmptyValue() {
  195. return null;
  196. }
  197. /**
  198. * Returns the current value of this object, wrapped in an {@code Optional}.
  199. * <p>
  200. * The {@code Optional} will be empty if the value is {@code null} or
  201. * {@code isEmpty()} returns {@code true}.
  202. *
  203. * @return the current value, wrapped in an {@code Optional}
  204. */
  205. public default Optional<V> getOptionalValue() {
  206. return isEmpty() ? Optional.empty() : Optional.ofNullable(getValue());
  207. }
  208. /**
  209. * Returns whether this {@code HasValue} is considered to be empty.
  210. * <p>
  211. * By default this is an equality check between current value and empty
  212. * value.
  213. *
  214. * @return {@code true} if considered empty; {@code false} if not
  215. */
  216. public default boolean isEmpty() {
  217. return Objects.equals(getValue(), getEmptyValue());
  218. }
  219. /**
  220. * Sets the required indicator visible or not.
  221. * <p>
  222. * If set visible, it is visually indicated in the user interface.
  223. *
  224. * @param requiredIndicatorVisible
  225. * <code>true</code> to make the required indicator visible,
  226. * <code>false</code> if not
  227. */
  228. public void setRequiredIndicatorVisible(boolean requiredIndicatorVisible);
  229. /**
  230. * Checks whether the required indicator is visible.
  231. *
  232. * @return <code>true</code> if visible, <code>false</code> if not
  233. */
  234. public boolean isRequiredIndicatorVisible();
  235. /**
  236. * Sets the read-only mode of this {@code HasValue} to given mode. The user
  237. * can't change the value when in read-only mode.
  238. * <p>
  239. * A {@code HasValue} with a visual component in read-only mode typically
  240. * looks visually different to signal to the user that the value cannot be
  241. * edited.
  242. *
  243. * @param readOnly
  244. * a boolean value specifying whether the component is put
  245. * read-only mode or not
  246. */
  247. public void setReadOnly(boolean readOnly);
  248. /**
  249. * Returns whether this {@code HasValue} is in read-only mode or not.
  250. *
  251. * @return {@code false} if the user can modify the value, {@code true} if
  252. * not.
  253. */
  254. public boolean isReadOnly();
  255. /**
  256. * Resets the value to the empty one.
  257. * <p>
  258. * This is just a shorthand for resetting the value, see the methods
  259. * {@link #setValue(Object)} and {@link #getEmptyValue()}.
  260. *
  261. * @see #setValue(Object)
  262. * @see #getEmptyValue()
  263. */
  264. public default void clear() {
  265. setValue(getEmptyValue());
  266. }
  267. /**
  268. * Returns a validator that checks the internal state of the HasValue. This
  269. * should be overridden for components with internal value conversion or
  270. * validation, e.g. when the user is providing a string that has to be
  271. * parsed into a date. An invalid input from user will be exposed to a
  272. * {@link Binder} and can be seen as a validation failure.
  273. *
  274. * @since 8.1
  275. * @return internal state validator
  276. * @see Binder#validate()
  277. */
  278. public default Validator<V> getDefaultValidator() {
  279. return Validator.alwaysPass();
  280. }
  281. }