Fixes #10476tags/8.3.0.alpha1
* binder.forField(nameField).bind(Person::getName, Person::setName); | * binder.forField(nameField).bind(Person::getName, Person::setName); | ||||
* </pre> | * </pre> | ||||
* | * | ||||
* <p> | |||||
* <strong>Note:</strong> when a {@code null} setter is given the field will be | |||||
* marked as readonly by invoking ({@link HasValue::setReadOnly}. | |||||
* | |||||
* @param getter | * @param getter | ||||
* the function to get the value of the property to the | * the function to get the value of the property to the | ||||
* field, not null | * field, not null | ||||
* an accessible setter; in that case the property value is never | * an accessible setter; in that case the property value is never | ||||
* updated and the binding is said to be <i>read-only</i>. | * updated and the binding is said to be <i>read-only</i>. | ||||
* | * | ||||
* <p> | |||||
* <strong>Note:</strong> when the binding is <i>read-only</i> the field will be | |||||
* marked as readonly by invoking ({@link HasValue::setReadOnly}. | |||||
* | |||||
* @param propertyName | * @param propertyName | ||||
* the name of the property to bind, not null | * the name of the property to bind, not null | ||||
* @return the newly created binding | * @return the newly created binding | ||||
if (getBinder().getBean() != null) { | if (getBinder().getBean() != null) { | ||||
binding.initFieldValue(getBinder().getBean()); | binding.initFieldValue(getBinder().getBean()); | ||||
} | } | ||||
if (setter == null) { | |||||
binding.getField().setReadOnly(true); | |||||
} | |||||
getBinder().fireStatusChangeEvent(false); | getBinder().fireStatusChangeEvent(false); | ||||
bound = true; | bound = true; | ||||
* binder.bind(nameField, Person::getName, Person::setName); | * binder.bind(nameField, Person::getName, Person::setName); | ||||
* </pre> | * </pre> | ||||
* | * | ||||
* <p> | |||||
* <strong>Note:</strong> when a {@code null} setter is given the field will be | |||||
* marked as readonly by invoking ({@link HasValue::setReadOnly}. | |||||
* | |||||
* @param <FIELDVALUE> | * @param <FIELDVALUE> | ||||
* the value type of the field | * the value type of the field | ||||
* @param field | * @param field |
assertEquals(propertyValue, item.getReadOnlyProperty()); | assertEquals(propertyValue, item.getReadOnlyProperty()); | ||||
} | } | ||||
@Test | |||||
public void bindReadOnlyPropertyShouldMarkFieldAsReadonly() { | |||||
binder.bind(nameField, "readOnlyProperty"); | |||||
assertTrue("Name field should be readonly", nameField.isReadOnly()); | |||||
} | |||||
@Test | @Test | ||||
public void setReadonlyShouldIgnoreBindingsForReadOnlyProperties() { | public void setReadonlyShouldIgnoreBindingsForReadOnlyProperties() { | ||||
binder.bind(nameField, "readOnlyProperty"); | binder.bind(nameField, "readOnlyProperty"); | ||||
binder.setReadOnly(true); | 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); | 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); | 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); | 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 | @Test |
.bind(Person::getAge, Person::setAge); | .bind(Person::getAge, Person::setAge); | ||||
binder.setReadOnly(true); | 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()); | assertTrue("Age field should be readonly", ageField.isReadOnly()); | ||||
binder.setReadOnly(false); | 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()); | assertFalse("Age field should not be readonly", ageField.isReadOnly()); | ||||
nameField.setReadOnly(false); | nameField.setReadOnly(false); | ||||
Person::getFirstName, Person::setFirstName); | Person::getFirstName, Person::setFirstName); | ||||
binder.removeBinding(binding); | 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()); | |||||
} | |||||
} | } |
layout.addComponent(errorLabel); | layout.addComponent(errorLabel); | ||||
Binder<Void> binder = new Binder<>(); | Binder<Void> binder = new Binder<>(); | ||||
binder.forField(dateField).withStatusLabel(errorLabel).bind(o -> dateField.getValue(), null); | |||||
binder.forField(dateField).withStatusLabel(errorLabel).bind(o -> dateField.getValue(), (aVoid, date) -> {}); | |||||
Button buttonValidate = new Button("Validate!"); | Button buttonValidate = new Button("Validate!"); | ||||
buttonValidate.addClickListener(event1 -> { | buttonValidate.addClickListener(event1 -> { |