summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>2017-11-13 13:58:31 +0100
committercaalador <mikael.grankvist@gmail.com>2017-11-13 14:58:31 +0200
commita8b09b222ae00e4cf88c02efa67e7c7d89a0eb97 (patch)
tree1b7c3e51b206a2a49cdb5d043c6bccbc0a452317
parent70219e5fa6a7beffbee5d2fd5814d9203512a06c (diff)
downloadvaadin-framework-a8b09b222ae00e4cf88c02efa67e7c7d89a0eb97.tar.gz
vaadin-framework-a8b09b222ae00e4cf88c02efa67e7c7d89a0eb97.zip
Add API to read Binding value from bean (#10316)
-rw-r--r--server/src/main/java/com/vaadin/data/Binder.java25
-rw-r--r--server/src/test/java/com/vaadin/data/BinderTest.java21
2 files changed, 43 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 8ca90ebaf8..03a6c1eb52 100644
--- a/server/src/main/java/com/vaadin/data/Binder.java
+++ b/server/src/main/java/com/vaadin/data/Binder.java
@@ -165,6 +165,16 @@ public class Binder<BEAN> implements Serializable {
* @since 8.2
*/
public void unbind();
+
+ /**
+ * Reads the value from given item and stores it to the bound field.
+ *
+ * @param bean
+ * the bean to read from
+ *
+ * @since 8.2
+ */
+ public void read(BEAN bean);
}
/**
@@ -516,9 +526,11 @@ public class Binder<BEAN> implements Serializable {
TARGET nullRepresentation) {
return withConverter(
fieldValue -> Objects.equals(fieldValue, nullRepresentation)
- ? null : fieldValue,
+ ? null
+ : fieldValue,
modelValue -> Objects.isNull(modelValue)
- ? nullRepresentation : modelValue);
+ ? nullRepresentation
+ : modelValue);
}
/**
@@ -1099,6 +1111,12 @@ public class Binder<BEAN> implements Serializable {
public BindingValidationStatusHandler getValidationStatusHandler() {
return statusHandler;
}
+
+ @Override
+ public void read(BEAN bean) {
+ field.setValue(converterValidatorChain.convertToPresentation(
+ getter.apply(bean), createValueContext()));
+ }
}
/**
@@ -2299,7 +2317,8 @@ public class Binder<BEAN> implements Serializable {
Converter<FIELDVALUE, FIELDVALUE> nullRepresentationConverter = Converter
.from(fieldValue -> fieldValue,
modelValue -> Objects.isNull(modelValue)
- ? field.getEmptyValue() : modelValue,
+ ? field.getEmptyValue()
+ : modelValue,
exception -> exception.getMessage());
ConverterDelegate<FIELDVALUE> converter = new ConverterDelegate<>(
nullRepresentationConverter);
diff --git a/server/src/test/java/com/vaadin/data/BinderTest.java b/server/src/test/java/com/vaadin/data/BinderTest.java
index 7b45ee95e0..b3bce11e87 100644
--- a/server/src/test/java/com/vaadin/data/BinderTest.java
+++ b/server/src/test/java/com/vaadin/data/BinderTest.java
@@ -990,4 +990,25 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> {
assertNull("Age field should still be ok.",
ageField.getComponentError());
}
+
+ @Test
+ public void refreshValueFromBean() {
+ Binding<Person, String> binding = binder.bind(nameField,
+ Person::getFirstName, Person::setFirstName);
+
+ binder.readBean(item);
+
+ assertEquals("Name should be read from the item", item.getFirstName(),
+ nameField.getValue());
+
+ nameField.setValue("foo");
+
+ assertNotEquals("Name should be different from the item",
+ item.getFirstName(), nameField.getValue());
+
+ binding.read(item);
+
+ assertEquals("Name should be read again from the item",
+ item.getFirstName(), nameField.getValue());
+ }
}