summaryrefslogtreecommitdiffstats
path: root/server/src/main
diff options
context:
space:
mode:
authorIlia Motornyi <elmot@vaadin.com>2017-06-29 10:30:57 +0300
committerGitHub <noreply@github.com>2017-06-29 10:30:57 +0300
commit9c3868e897541c014e19341f3d58066734f8a6c0 (patch)
tree6e0d00326c37e80d6478895e5a2470b6a203e445 /server/src/main
parentb2c0ac2bac558a4bc497fa3a6d29b8f030511e1a (diff)
downloadvaadin-framework-9c3868e897541c014e19341f3d58066734f8a6c0.tar.gz
vaadin-framework-9c3868e897541c014e19341f3d58066734f8a6c0.zip
Add an optional HasValue to a ValueContext
Diffstat (limited to 'server/src/main')
-rw-r--r--server/src/main/java/com/vaadin/data/Binder.java4
-rw-r--r--server/src/main/java/com/vaadin/data/ValueContext.java54
-rw-r--r--server/src/main/java/com/vaadin/ui/AbstractDateField.java2
3 files changed, 57 insertions, 3 deletions
diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java
index c36029aa83..26720a3274 100644
--- a/server/src/main/java/com/vaadin/data/Binder.java
+++ b/server/src/main/java/com/vaadin/data/Binder.java
@@ -872,9 +872,9 @@ public class Binder<BEAN> implements Serializable {
*/
protected ValueContext createValueContext() {
if (field instanceof Component) {
- return new ValueContext((Component) field);
+ return new ValueContext((Component) field, field);
}
- return new ValueContext(findLocale());
+ return new ValueContext(null, field, findLocale());
}
/**
diff --git a/server/src/main/java/com/vaadin/data/ValueContext.java b/server/src/main/java/com/vaadin/data/ValueContext.java
index 07857d34af..4ce1111ed0 100644
--- a/server/src/main/java/com/vaadin/data/ValueContext.java
+++ b/server/src/main/java/com/vaadin/data/ValueContext.java
@@ -33,6 +33,7 @@ import com.vaadin.ui.UI;
public class ValueContext implements Serializable {
private final Component component;
+ private final HasValue<?> hasValue;
private final Locale locale;
/**
@@ -40,6 +41,7 @@ public class ValueContext implements Serializable {
*/
public ValueContext() {
component = null;
+ hasValue = null;
locale = findLocale();
}
@@ -52,6 +54,7 @@ public class ValueContext implements Serializable {
public ValueContext(Locale locale) {
component = null;
this.locale = locale;
+ hasValue = null;
}
/**
@@ -59,14 +62,54 @@ public class ValueContext implements Serializable {
*
* @param component
* The component related to current value. Can be null.
+ * If the component implements {@link HasValue}, it will be returned by {@link #getHasValue()} as well.
*/
+ @SuppressWarnings("unchecked")
public ValueContext(Component component) {
Objects.requireNonNull(component,
"Component can't be null in ValueContext construction");
this.component = component;
+ if(component instanceof HasValue) {
+ hasValue = (HasValue<?>) component;
+ } else {
+ hasValue = null;
+ }
+ locale = findLocale();
+ }
+
+ /**
+ * Constructor for {@code ValueContext}.
+ *
+ * @param component
+ * The component related to current value. Can be null.
+ * @param hasValue
+ * The value source related to current value. Can be null.
+ */
+ public ValueContext(Component component, HasValue<?> hasValue) {
+ Objects.requireNonNull(component,
+ "Component can't be null in ValueContext construction");
+ this.component = component;
+ this.hasValue = hasValue;
locale = findLocale();
}
+ /**
+ * Constructor for {@code ValueContext}.
+ *
+ * @param
+ * component
+ * The component can be {@code null}.
+ * @param locale
+ * The locale used with conversion. Can be {@code null}.
+ * @param hasValue
+ * The value source related to current value. Can be {@code null}.
+ */
+ public ValueContext(Component component, HasValue<?> hasValue, Locale locale) {
+ this.component = component;
+ this.hasValue = hasValue;
+ this.locale = locale;
+ }
+
private Locale findLocale() {
Locale l = null;
if (component != null) {
@@ -100,4 +143,15 @@ public class ValueContext implements Serializable {
public Optional<Locale> getLocale() {
return Optional.ofNullable(locale);
}
+
+ /**
+ * Returns an {@code Optional} for the {@code HasValue} used in the value
+ * conversion. In certain complicated cases, ex. cross-field validation, HasValue might be not available.
+ *
+ * @return the optional of {@code HasValue}
+ */
+ @SuppressWarnings("unused")
+ public Optional<HasValue<?>> getHasValue() {
+ return Optional.ofNullable(hasValue);
+ }
}
diff --git a/server/src/main/java/com/vaadin/ui/AbstractDateField.java b/server/src/main/java/com/vaadin/ui/AbstractDateField.java
index 06e6d74c30..0e738de253 100644
--- a/server/src/main/java/com/vaadin/ui/AbstractDateField.java
+++ b/server/src/main/java/com/vaadin/ui/AbstractDateField.java
@@ -684,7 +684,7 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
} else {
RangeValidator<T> validator = getRangeValidator();
ValidationResult result = validator.apply(value,
- new ValueContext(this));
+ new ValueContext(this, this));
if (result.isError()) {
setComponentError(new UserError(getDateOutOfRangeMessage()));
}