From 1619d1f0bbd41de3f175b9eb2f34b468ec7e0367 Mon Sep 17 00:00:00 2001 From: Marco Collovati Date: Fri, 29 Dec 2017 10:36:38 +0100 Subject: Treat fields as readonly when bound with null setter (#10477) Fixes #10476 --- server/src/main/java/com/vaadin/data/Binder.java | 15 +++++++++++++++ .../src/test/java/com/vaadin/data/BeanBinderTest.java | 17 ++++++++++++----- server/src/test/java/com/vaadin/data/BinderTest.java | 15 +++++++++++++-- 3 files changed, 40 insertions(+), 7 deletions(-) (limited to 'server') diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java index 26354138ce..8f25e92aba 100644 --- a/server/src/main/java/com/vaadin/data/Binder.java +++ b/server/src/main/java/com/vaadin/data/Binder.java @@ -228,6 +228,10 @@ public class Binder implements Serializable { * binder.forField(nameField).bind(Person::getName, Person::setName); * * + *

+ * Note: when a {@code null} setter is given the field will be + * marked as readonly by invoking ({@link HasValue::setReadOnly}. + * * @param getter * the function to get the value of the property to the * field, not null @@ -256,6 +260,10 @@ public class Binder implements Serializable { * an accessible setter; in that case the property value is never * updated and the binding is said to be read-only. * + *

+ * Note: when the binding is read-only the field will be + * marked as readonly by invoking ({@link HasValue::setReadOnly}. + * * @param propertyName * the name of the property to bind, not null * @return the newly created binding @@ -741,6 +749,9 @@ public class Binder implements Serializable { if (getBinder().getBean() != null) { binding.initFieldValue(getBinder().getBean()); } + if (setter == null) { + binding.getField().setReadOnly(true); + } getBinder().fireStatusChangeEvent(false); bound = true; @@ -1444,6 +1455,10 @@ public class Binder implements Serializable { * binder.bind(nameField, Person::getName, Person::setName); * * + *

+ * Note: when a {@code null} setter is given the field will be + * marked as readonly by invoking ({@link HasValue::setReadOnly}. + * * @param * the value type of the field * @param field diff --git a/server/src/test/java/com/vaadin/data/BeanBinderTest.java b/server/src/test/java/com/vaadin/data/BeanBinderTest.java index 567bdcc3e8..e6740f35ba 100644 --- a/server/src/test/java/com/vaadin/data/BeanBinderTest.java +++ b/server/src/test/java/com/vaadin/data/BeanBinderTest.java @@ -310,22 +310,29 @@ public class BeanBinderTest assertEquals(propertyValue, item.getReadOnlyProperty()); } + @Test + public void bindReadOnlyPropertyShouldMarkFieldAsReadonly() { + binder.bind(nameField, "readOnlyProperty"); + + assertTrue("Name field should be readonly", nameField.isReadOnly()); + } + @Test public void setReadonlyShouldIgnoreBindingsForReadOnlyProperties() { binder.bind(nameField, "readOnlyProperty"); binder.setReadOnly(true); - assertFalse("Name field should be ignored and not be readonly", nameField.isReadOnly()); + assertTrue("Name field should be ignored and be readonly", nameField.isReadOnly()); binder.setReadOnly(false); - assertFalse("Name field should be ignored and not be readonly", nameField.isReadOnly()); + assertTrue("Name field should be ignored and be readonly", nameField.isReadOnly()); - nameField.setReadOnly(true); + nameField.setReadOnly(false); binder.setReadOnly(true); - assertTrue("Name field should be ignored and be readonly", nameField.isReadOnly()); + assertFalse("Name field should be ignored and not be readonly", nameField.isReadOnly()); binder.setReadOnly(false); - assertTrue("Name field should be ignored and be readonly", nameField.isReadOnly()); + assertFalse("Name field should be ignored and not be readonly", nameField.isReadOnly()); } @Test diff --git a/server/src/test/java/com/vaadin/data/BinderTest.java b/server/src/test/java/com/vaadin/data/BinderTest.java index 5c6c9657a5..131dcb29b0 100644 --- a/server/src/test/java/com/vaadin/data/BinderTest.java +++ b/server/src/test/java/com/vaadin/data/BinderTest.java @@ -616,11 +616,11 @@ public class BinderTest extends BinderTestBase, Person> { .bind(Person::getAge, Person::setAge); binder.setReadOnly(true); - assertFalse("Name field should be ignored and not be readonly", nameField.isReadOnly()); + assertTrue("Name field should be ignored but should be readonly", nameField.isReadOnly()); assertTrue("Age field should be readonly", ageField.isReadOnly()); binder.setReadOnly(false); - assertFalse("Name field should be ignored and remain not readonly", nameField.isReadOnly()); + assertTrue("Name field should be ignored and should remain readonly", nameField.isReadOnly()); assertFalse("Age field should not be readonly", ageField.isReadOnly()); nameField.setReadOnly(false); @@ -1044,4 +1044,15 @@ public class BinderTest extends BinderTestBase, Person> { Person::getFirstName, Person::setFirstName); binder.removeBinding(binding); } + + @Test + public void bindWithNullSetterShouldMarkFieldAsReadonly() { + binder.bind(nameField, Person::getFirstName, null); + binder.forField(ageField) + .withConverter(new StringToIntegerConverter("")) + .bind(Person::getAge, Person::setAge); + + assertTrue("Name field should be readonly", nameField.isReadOnly()); + assertFalse("Name field should be readonly", ageField.isReadOnly()); + } } -- cgit v1.2.3