diff options
author | Denis <denis@vaadin.com> | 2017-01-16 13:12:21 +0200 |
---|---|---|
committer | Aleksi Hietanen <aleksi@vaadin.com> | 2017-01-16 13:12:21 +0200 |
commit | 352d12d977157f261c2e585e9e0a6f6354812c83 (patch) | |
tree | 1d0b96589b235b492f873a813d204a013412f294 /server | |
parent | 8a4ada326b8828c15fab72de56777edce298a1e2 (diff) | |
download | vaadin-framework-352d12d977157f261c2e585e9e0a6f6354812c83.tar.gz vaadin-framework-352d12d977157f261c2e585e9e0a6f6354812c83.zip |
Introduce Binder.setReadOnly() (#8241)
Closes #8232
Diffstat (limited to 'server')
-rw-r--r-- | server/src/main/java/com/vaadin/data/Binder.java | 17 | ||||
-rw-r--r-- | server/src/test/java/com/vaadin/data/BinderTest.java | 63 |
2 files changed, 80 insertions, 0 deletions
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 @@ -1909,6 +1909,23 @@ public class Binder<BEAN> implements Serializable { } /** + * Sets the read only state to the given value for all currently bound + * fields. + * <p> + * 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. * * @return the event router, not null 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<Binder<Person>, 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 |