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

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