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.

ValueContext.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 java.util.Locale;
  19. import java.util.Objects;
  20. import java.util.Optional;
  21. import com.vaadin.ui.Component;
  22. import com.vaadin.ui.UI;
  23. /**
  24. * Value context for {@code Converter}s. Contains relevant information for
  25. * converting values.
  26. *
  27. * @author Vaadin Ltd.
  28. * @since 8.0
  29. */
  30. public class ValueContext implements Serializable {
  31. private final Component component;
  32. private final HasValue<?> hasValue;
  33. private final Locale locale;
  34. /**
  35. * Constructor for {@code ValueContext} without a {@code Locale}.
  36. */
  37. public ValueContext() {
  38. component = null;
  39. hasValue = null;
  40. locale = findLocale();
  41. }
  42. /**
  43. * Constructor for {@code ValueContext} without a {@code Component}.
  44. *
  45. * @param locale
  46. * The locale used with conversion. Can be null.
  47. */
  48. public ValueContext(Locale locale) {
  49. component = null;
  50. this.locale = locale;
  51. hasValue = null;
  52. }
  53. /**
  54. * Constructor for {@code ValueContext}.
  55. *
  56. * @param component
  57. * The component related to current value. Can be null. If the
  58. * component implements {@link HasValue}, it will be returned by
  59. * {@link #getHasValue()} as well.
  60. */
  61. @SuppressWarnings("unchecked")
  62. public ValueContext(Component component) {
  63. Objects.requireNonNull(component,
  64. "Component can't be null in ValueContext construction");
  65. this.component = component;
  66. if (component instanceof HasValue) {
  67. hasValue = (HasValue<?>) component;
  68. } else {
  69. hasValue = null;
  70. }
  71. locale = findLocale();
  72. }
  73. /**
  74. * Constructor for {@code ValueContext}.
  75. *
  76. * @param component
  77. * The component related to current value. Can be null.
  78. * @param hasValue
  79. * The value source related to current value. Can be null.
  80. * @since 8.1
  81. */
  82. public ValueContext(Component component, HasValue<?> hasValue) {
  83. Objects.requireNonNull(component,
  84. "Component can't be null in ValueContext construction");
  85. this.component = component;
  86. this.hasValue = hasValue;
  87. locale = findLocale();
  88. }
  89. /**
  90. * Constructor for {@code ValueContext}.
  91. *
  92. * @param component
  93. * The component can be {@code null}.
  94. * @param locale
  95. * The locale used with conversion. Can be {@code null}.
  96. * @param hasValue
  97. * The value source related to current value. Can be
  98. * {@code null}.
  99. * @since 8.1
  100. */
  101. public ValueContext(Component component, HasValue<?> hasValue,
  102. Locale locale) {
  103. this.component = component;
  104. this.hasValue = hasValue;
  105. this.locale = locale;
  106. }
  107. private Locale findLocale() {
  108. Locale l = null;
  109. if (component != null) {
  110. l = component.getLocale();
  111. }
  112. if (l == null && UI.getCurrent() != null) {
  113. l = UI.getCurrent().getLocale();
  114. }
  115. if (l == null) {
  116. l = Locale.getDefault();
  117. }
  118. return l;
  119. }
  120. /**
  121. * Returns an {@code Optional} for the {@code Component} related to value
  122. * conversion.
  123. *
  124. * @return the optional of component
  125. */
  126. public Optional<Component> getComponent() {
  127. return Optional.ofNullable(component);
  128. }
  129. /**
  130. * Returns an {@code Optional} for the {@code Locale} used in the value
  131. * conversion.
  132. *
  133. * @return the optional of locale
  134. */
  135. public Optional<Locale> getLocale() {
  136. return Optional.ofNullable(locale);
  137. }
  138. /**
  139. * Returns an {@code Optional} for the {@code HasValue} used in the value
  140. * conversion. In certain complicated cases, ex. cross-field validation,
  141. * HasValue might be not available.
  142. *
  143. * @return the optional of {@code HasValue}
  144. * @since 8.1
  145. */
  146. @SuppressWarnings("unused")
  147. public Optional<HasValue<?>> getHasValue() {
  148. return Optional.ofNullable(hasValue);
  149. }
  150. }