From 352d12d977157f261c2e585e9e0a6f6354812c83 Mon Sep 17 00:00:00 2001 From: Denis Date: Mon, 16 Jan 2017 13:12:21 +0200 Subject: [PATCH] Introduce Binder.setReadOnly() (#8241) Closes #8232 --- .../src/main/java/com/vaadin/data/Binder.java | 17 +++++ .../test/java/com/vaadin/data/BinderTest.java | 63 +++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java index 4695e372c1..7451c004ed 100644 --- a/server/src/main/java/com/vaadin/data/Binder.java +++ b/server/src/main/java/com/vaadin/data/Binder.java @@ -1908,6 +1908,23 @@ public class Binder implements Serializable { return hasChanges; } + /** + * Sets the read only state to the given value for all currently bound + * fields. + *

+ * This is just a shorthand for calling setReadOnly for all currently bound + * fields. It means that fields bound after this method call won't be set + * read-only. + * + * @param fieldsReadOnly + * true to set the fields to read only, false to set them to read + * write + */ + public void setReadOnly(boolean fieldsReadOnly) { + getBindings().stream().map(BindingImpl::getField) + .forEach(field -> field.setReadOnly(fieldsReadOnly)); + } + /** * Returns the event router for this binder. * diff --git a/server/src/test/java/com/vaadin/data/BinderTest.java b/server/src/test/java/com/vaadin/data/BinderTest.java index 1994821db0..e07aee9632 100644 --- a/server/src/test/java/com/vaadin/data/BinderTest.java +++ b/server/src/test/java/com/vaadin/data/BinderTest.java @@ -431,4 +431,67 @@ public class BinderTest extends BinderTestBase, Person> { public void noArgsConstructor_stringBind_throws() { binder.bind(new TextField(), "firstName"); } + + @Test + public void setReadOnly_unboundBinder() { + binder.forField(nameField).bind(Person::getFirstName, + Person::setFirstName); + + binder.forField(ageField); + + binder.setReadOnly(true); + + Assert.assertTrue(nameField.isReadOnly()); + Assert.assertFalse(ageField.isReadOnly()); + + binder.setReadOnly(false); + + Assert.assertFalse(nameField.isReadOnly()); + Assert.assertFalse(ageField.isReadOnly()); + } + + @Test + public void setReadOnly_boundBinder() { + binder.forField(nameField).bind(Person::getFirstName, + Person::setFirstName); + + binder.forField(ageField) + .withConverter(new StringToIntegerConverter("")) + .bind(Person::getAge, Person::setAge); + + binder.setBean(new Person()); + + binder.setReadOnly(true); + + Assert.assertTrue(nameField.isReadOnly()); + Assert.assertTrue(ageField.isReadOnly()); + + binder.setReadOnly(false); + + Assert.assertFalse(nameField.isReadOnly()); + Assert.assertFalse(ageField.isReadOnly()); + } + + @Test + public void setReadOnly_binderLoadedByReadBean() { + binder.forField(nameField).bind(Person::getFirstName, + Person::setFirstName); + + binder.forField(ageField) + .withConverter(new StringToIntegerConverter("")) + .bind(Person::getAge, Person::setAge); + + binder.readBean(new Person()); + + binder.setReadOnly(true); + + Assert.assertTrue(nameField.isReadOnly()); + Assert.assertTrue(ageField.isReadOnly()); + + binder.setReadOnly(false); + + Assert.assertFalse(nameField.isReadOnly()); + Assert.assertFalse(ageField.isReadOnly()); + } + } \ No newline at end of file -- 2.39.5