Person person = new Person("John Doe", 1957);
// Updates the value in each bound field component
-binder.load(person);
+binder.readBean(person);
Button saveButton = new Button("Save",
event -> {
try {
- binder.save(person);
+ binder.writeBean(person);
// A real application would also save the updated person
// using the application's backend
} catch (BindingException e) {
// Updates the fields again with the previously saved values
Button resetButton = new Button("Reset",
- event -> binder.load(person));
+ event -> binder.readBean(person));
----
With these basic steps, we have defined everything that is needed for loading, editing and saving values for a form.
[source, java]
----
// Resets the form to show default values by populating the fields with the default values from the bean
-binder.load(new Person());
+binder.readBean(new Person());
// This will make all current validation errors visible
ValidationStatus<Person> status = binder.validate();
[source, java]
----
try {
- binder.save(person);
+ binder.writeBean(person);
} catch (ValidationException e) {
Notification.show("Validation error count: "
+ e.getValidationErrors().size());
--
[source, java]
----
-binder.save(person,
+binder.writeBean(person,
// Callback invoked if there is an error
errors -> {
Notification.show("Validation error count: "
--
[source, java]
----
-boolean saved = binder.saveIfValid(person);
+boolean saved = binder.writeBeanIfValid(person);
if (!saved) {
Notification.show("Validation error count: "
+ binder.getValidationErrors().size());
----
// Invoked when the value of any bound field component changes
binder.addFieldValueChangeListener(event -> {
- if (binder.saveIfValid(person)) {
+ if (binder.writeBeanIfValid(person)) {
// We only get here if there are no validation errors
// TODO: Do something with the updated person instance
----
binder.addFieldValueChangeListener(event -> {
Binding<Person, ?> binding = event.getBinding();
- if (binding.saveIfValid(person)) {
+ if (binder.writeIfValid(person)) {
// We get here if the updated binding had no validation errors
// TODO: Do something with the updated person instance
// Loads the values from the person instance
// Sets person to be updated when any bound field is updated
-binder.bind(person);
+binder.setBean(person);
Button saveButton = new Button("Save", event -> {
if (binder.isValid()) {
When using the [methodname]#bind# method, the business object instance will be updated whenever the user changes the value in any bound field.
If some other part of the application is also using the same instance, then that part might show changes before the user has clicked the save button.
-The [methodname]#bind# method returns an [interfacename]#ItemBinding# instance that we can use to further configure the binding.
-We can change the binding to use a different business object, cancel the binding, or change whether a validation error prevents other values from being saved.
-
-[source, java]
-----
-ItemBinding<Person> binding = binder.bind(person);
-
-// Makes the binding save new values for valid fields even if
-// other fields are invalid
-binding.setSaveWhenInvalid(true);
-
-// Field changes will update anotherPerson instead of person
-binding.bind(anotherPerson);
-
-// Field changes will no longer update any person instance
-binding.cancel();
-----
-
== Binding Beans to Forms
The business objects used in an application are in most cases implemented as Java beans.
Person person = // e.g. JPA entity or bean from Grid
// Load person data to a form
-binder.load(person);
+binder.readBean(person);
Button saveButton = new Button("Save", event -> {
// Using saveIfValid to avoid the try-catch block that is
// needed if using the regular save method
- if (binder.saveIfValid(person)) {
+ if (binder.writeBeanIfValid(person)) {
// Person is valid and updated
// TODO Store in the database
}
public PersonForm(Person person) {
binder.bindInstanceFields(this);
- binder.load(person);
+ binder.readBean(person);
save.addClickListener(event -> {
- if (binder.saveIfValid(person)) {
+ if (binder.writeBeanIfValid(person)) {
// TODO: Do something with the updated person instance
}
});
* Bean level validators can be added using the
* {@link #withValidator(Validator)} method and will be run on the bound bean
* once it has been updated from the values of the bound fields. Bean level
- * validators are also run as part of {@link #save(Object)} and
- * {@link #saveIfValid(Object)} if all field level validators pass.
+ * validators are also run as part of {@link #writeBean(Object)} and
+ * {@link #writeBeanIfValid(Object)} if all field level validators pass.
* <p>
* Note: For bean level validators, the bean must be updated before the
- * validators are run. If a bean level validator fails in {@link #save(Object)}
- * or {@link #saveIfValid(Object)}, the bean will be reverted to the previous
- * state before returning from the method. You should ensure that the
- * getters/setters in the bean do not have side effects.
+ * validators are run. If a bean level validator fails in
+ * {@link #writeBean(Object)} or {@link #writeBeanIfValid(Object)}, the bean
+ * will be reverted to the previous state before returning from the method. You
+ * should ensure that the getters/setters in the bean do not have side effects.
* <p>
* Unless otherwise specified, {@code Binder} method arguments cannot be null.
*
* update the field value from the property and to store the field value
* to the property, respectively.
* <p>
- * When a bean is bound with {@link Binder#bind(BEAN)}, the field value
- * is set to the return value of the given getter. The property value is
- * then updated via the given setter whenever the field value changes.
- * The setter may be null; in that case the property value is never
- * updated and the binding is said to be <i>read-only</i>.
+ * When a bean is bound with {@link Binder#setBean(BEAN)}, the field
+ * value is set to the return value of the given getter. The property
+ * value is then updated via the given setter whenever the field value
+ * changes. The setter may be null; in that case the property value is
+ * never updated and the binding is said to be <i>read-only</i>.
* <p>
* If the Binder is already bound to some bean, the newly bound field is
* associated with the corresponding bean property as described above.
* the function to get the value of the property to the
* field, not null
* @param setter
- * the function to save the field value to the property or
+ * the function to write the field value to the property or
* null if read-only
* @throws IllegalStateException
* if {@code bind} has already been called on this binding
/**
* Adds a validator to this binding. Validators are applied, in
- * registration order, when the field value is saved to the backing
+ * registration order, when the field value is written to the backing
* property. If any validator returns a failure, the property value is
* not updated.
*
* {@link Validator#from(SerializablePredicate, String)} factory method.
* <p>
* Validators are applied, in registration order, when the field value
- * is saved to the backing property. If any validator returns a failure,
- * the property value is not updated.
+ * is written to the backing property. If any validator returns a
+ * failure, the property value is not updated.
*
* @see #withValidator(Validator)
* @see Validator#from(SerializablePredicate, String)
* <p>
* The method {@link #withConverter(Converter)} calls this method with
* {@code true} provided as the second argument value.
- *
+ *
* @see #withConverter(Converter)
- *
+ *
* @param converter
* the converter to use, not null
* @param resetNullRepresentation
return new ValueContext(findLocale());
}
- private void unbind() {
+ private void reset() {
onValueChange.remove();
}
private void handleFieldValueChange(BEAN bean) {
getBinder().setHasChanges(true);
// store field value if valid
- ValidationStatus<TARGET> fieldValidationStatus = storeFieldValue(
+ ValidationStatus<TARGET> fieldValidationStatus = writeFieldValue(
bean);
List<Result<?>> binderValidationResults;
// if all field level validations pass, run bean level validation
}
/**
- * Saves the field value by invoking the setter function on the given
+ * Write the field value by invoking the setter function on the given
* bean, if the value passes all registered validators.
*
* @param bean
* the bean to set the property value to
*/
- private ValidationStatus<TARGET> storeFieldValue(BEAN bean) {
+ private ValidationStatus<TARGET> writeFieldValue(BEAN bean) {
assert bean != null;
ValidationStatus<TARGET> validationStatus = doValidation();
* {@link #forField(HasValue)} and
* {@link Binding#withNullRepresentation(Object))}.
* <p>
- * When a bean is bound with {@link Binder#bind(BEAN)}, the field value is
- * set to the return value of the given getter. The property value is then
- * updated via the given setter whenever the field value changes. The setter
- * may be null; in that case the property value is never updated and the
- * binding is said to be <i>read-only</i>.
+ * When a bean is bound with {@link Binder#setBean(BEAN)}, the field value
+ * is set to the return value of the given getter. The property value is
+ * then updated via the given setter whenever the field value changes. The
+ * setter may be null; in that case the property value is never updated and
+ * the binding is said to be <i>read-only</i>.
* <p>
* If the Binder is already bound to some bean, the newly bound field is
* associated with the corresponding bean property as described above.
* the function to get the value of the property to the field,
* not null
* @param setter
- * the function to save the field value to the property or null
+ * the function to write the field value to the property or null
* if read-only
*/
public <FIELDVALUE> void bind(HasValue<FIELDVALUE> field,
}
/**
- * Binds the given bean to all the fields added to this Binder. To remove
- * the binding, call {@link #unbind()}.
+ * Binds the given bean to all the fields added to this Binder. A
+ * {@code null} value removes a currently bound bean.
* <p>
* When a bean is bound, the field values are updated by invoking their
* corresponding getter functions. Any changes to field values are reflected
* {@link Binding} and bean level validation for this binder (bean level
* validators are added using {@link Binder#withValidator(Validator)}.
*
- * @see #load(Object)
- * @see #save(Object)
- * @see #saveIfValid(Object)
+ * @see #readBean(Object)
+ * @see #writeBean(Object)
+ * @see #writeBeanIfValid(Object)
*
* @param bean
- * the bean to edit, not null
+ * the bean to edit, or {@code null} to remove a currently bound
+ * bean
*/
- public void bind(BEAN bean) {
- Objects.requireNonNull(bean, "bean cannot be null");
- doUnbind(false);
- this.bean = bean;
- bindings.forEach(b -> b.bind(bean));
- // if there has been field value change listeners that trigger
- // validation, need to make sure the validation errors are cleared
- getValidationStatusHandler()
- .accept(BinderValidationStatus.createUnresolvedStatus(this));
- fireStatusChangeEvent(false);
+ public void setBean(BEAN bean) {
+ if (bean == null) {
+ if (this.bean != null) {
+ doRemoveBean(true);
+ }
+ } else {
+ doRemoveBean(false);
+ this.bean = bean;
+ bindings.forEach(b -> b.bind(bean));
+ // if there has been field value change listeners that trigger
+ // validation, need to make sure the validation errors are cleared
+ getValidationStatusHandler().accept(
+ BinderValidationStatus.createUnresolvedStatus(this));
+ fireStatusChangeEvent(false);
+ }
}
/**
- * Unbinds the currently bound bean if any. If there is no bound bean, does
+ * Removes the currently set bean, if any. If there is no bound bean, does
* nothing.
+ * <p>
+ * This is a shorthand for {@link #setBean(Object)} with {@code null} bean.
*/
- public void unbind() {
- doUnbind(true);
+ public void removeBean() {
+ setBean(null);
}
/**
* <p>
* The bean is not otherwise associated with this binder; in particular its
* property values are not bound to the field value changes. To achieve
- * that, use {@link #bind(BEAN)}.
+ * that, use {@link #setBean(BEAN)}.
*
- * @see #bind(Object)
- * @see #saveIfValid(Object)
- * @see #save(Object)
+ * @see #setBean(Object)
+ * @see #writeBeanIfValid(Object)
+ * @see #writeBean(Object)
*
* @param bean
* the bean whose property values to read, not null
*/
- public void load(BEAN bean) {
+ public void readBean(BEAN bean) {
Objects.requireNonNull(bean, "bean cannot be null");
setHasChanges(false);
bindings.forEach(binding -> binding.setFieldValue(bean));
}
/**
- * Saves changes from the bound fields to the given bean if all validators
+ * Writes changes from the bound fields to the given bean if all validators
* (binding and bean level) pass.
* <p>
- * If any field binding validator fails, no values are saved and a
+ * If any field binding validator fails, no values are written and a
* {@code ValidationException} is thrown.
* <p>
* If all field level validators pass, the given bean is updated and bean
* fails, the bean updates are reverted and a {@code ValidationException} is
* thrown.
*
- * @see #saveIfValid(Object)
- * @see #load(Object)
- * @see #bind(Object)
+ * @see #writeBeanIfValid(Object)
+ * @see #readBean(Object)
+ * @see #setBean(Object)
*
* @param bean
- * the object to which to save the field values, not null
+ * the object to which to write the field values, not
+ * {@code null}
* @throws ValidationException
* if some of the bound field values fail to validate
*/
- public void save(BEAN bean) throws ValidationException {
- BinderValidationStatus<BEAN> status = doSaveIfValid(bean);
+ public void writeBean(BEAN bean) throws ValidationException {
+ BinderValidationStatus<BEAN> status = doWriteIfValid(bean);
if (status.hasErrors()) {
throw new ValidationException(status.getFieldValidationErrors(),
status.getBeanValidationErrors());
}
/**
- * Saves changes from the bound fields to the given bean if all validators
+ * Writes changes from the bound fields to the given bean if all validators
* (binding and bean level) pass.
* <p>
- * If any field binding validator fails, no values are saved and
+ * If any field binding validator fails, no values are written and
* <code>false</code> is returned.
* <p>
* If all field level validators pass, the given bean is updated and bean
* level validators are run on the updated bean. If any bean level validator
* fails, the bean updates are reverted and <code>false</code> is returned.
*
- * @see #save(Object)
- * @see #load(Object)
- * @see #bind(Object)
+ * @see #writeBean(Object)
+ * @see #readBean(Object)
+ * @see #setBean(Object)
*
* @param bean
- * the object to which to save the field values, not null
+ * the object to which to write the field values, not
+ * {@code null}
* @return {@code true} if there was no validation errors and the bean was
* updated, {@code false} otherwise
*/
- public boolean saveIfValid(BEAN bean) {
- return doSaveIfValid(bean).isOk();
+ public boolean writeBeanIfValid(BEAN bean) {
+ return doWriteIfValid(bean).isOk();
}
/**
- * Saves the field values into the given bean if all field level validators
- * pass. Runs bean level validators on the bean after saving.
+ * Writes the field values into the given bean if all field level validators
+ * pass. Runs bean level validators on the bean after writing.
*
* @param bean
- * the bean to save field values into
+ * the bean to write field values into
* @return a list of field validation errors if such occur, otherwise a list
* of bean validation errors.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
- private BinderValidationStatus<BEAN> doSaveIfValid(BEAN bean) {
+ private BinderValidationStatus<BEAN> doWriteIfValid(BEAN bean) {
Objects.requireNonNull(bean, "bean cannot be null");
// First run fields level validation
List<ValidationStatus<?>> bindingStatuses = validateBindings();
Collections.emptyList());
}
- // Save old bean values so we can restore them if validators fail
+ // Store old bean values so we can restore them if validators fail
Map<Binding<BEAN, ?, ?>, Object> oldValues = new HashMap<>();
bindings.forEach(
binding -> oldValues.put(binding, binding.getter.apply(bean)));
- bindings.forEach(binding -> binding.storeFieldValue(bean));
+ bindings.forEach(binding -> binding.writeFieldValue(bean));
// Now run bean level validation against the updated bean
List<Result<?>> binderResults = validateBean(bean);
boolean hasErrors = binderResults.stream().filter(Result::isError)
bindings.forEach((BindingImpl binding) -> binding.setter
.accept(bean, oldValues.get(binding)));
} else {
- // Save successful, reset hasChanges to false
+ // Write successful, reset hasChanges to false
setHasChanges(false);
}
fireStatusChangeEvent(hasErrors);
* updated. If the validators fail, the bean instance is reverted to its
* previous state.
*
- * @see #save(Object)
- * @see #saveIfValid(Object)
+ * @see #writeBean(Object)
+ * @see #writeBeanIfValid(Object)
*
* @param validator
* the validator to add, not null
* Validates the values of all bound fields and returns the validation
* status.
* <p>
- * If all field level validators pass, and {@link #bind(Object)} has been
+ * If all field level validators pass, and {@link #setBean(Object)} has been
* used to bind to a bean, bean level validators are run for that bean. Bean
* level validators are ignored if there is no bound bean or if any field
* level validator fails.
* happens:
* <ul>
* <li>if it's bound and any of its bound field or select has been changed
- * <li>{@link #save(Object)} or {@link #saveIfValid(Object)} is called
- * <li>{@link #load(Object)} is called
- * <li>{@link #bind(Object)} is called
- * <li>{@link #unbind(Object)} is called
+ * <li>{@link #writeBean(Object)} or {@link #writeBeanIfValid(Object)} is
+ * called
+ * <li>{@link #readBean(Object)} is called
+ * <li>{@link #setBean(Object)} is called
+ * <li>{@link #removeBean()} is called
* <li>{@link Binding#bind(SerializableFunction, SerializableBiConsumer)} is
* called
* <li>{@link Binder#validate()} or {@link Binding#validate()} is called
* </ul>
*
- * @see #load(Object)
- * @see #save(Object)
- * @see #saveIfValid(Object)
- * @see #bind(Object)
- * @see #unbind()
+ * @see #readBean(Object)
+ * @see #writeBean(Object)
+ * @see #writeBeanIfValid(Object)
+ * @see #setBean(Object)
+ * @see #removeBean()
* @see #forField(HasValue)
* @see #forSelect(AbstractMultiSelect)
* @See {@link #validate()}
}
/**
- * Handles a validation error emitted when trying to save the value of the
+ * Handles a validation error emitted when trying to write the value of the
* given field. The default implementation sets the
* {@link AbstractComponent#setComponentError(ErrorMessage) component error}
* of the field if it is a Component, otherwise does nothing.
/**
* Sets whether the values of the fields this binder is bound to have
- * changed since the last explicit call to either bind, save or load.
+ * changed since the last explicit call to either bind, write or read.
*
* @param hasChanges
* whether this binder should be marked to have changes
/**
* Check whether any of the bound fields' values have changed since last
- * explicit call to bind, save or load. Unsuccessful save operations will
- * not affect this value.
+ * explicit call to {@link #setBean(Object)}, {@link #writeBean(Object)} or
+ * {@link #readBean(Object)}. Unsuccessful write operations will not affect
+ * this value.
*
* @return whether any bound field's value has changed since last call to
- * bind, save or load
+ * setBean, writeBean or readBean
*/
public boolean hasChanges() {
return hasChanges;
return eventRouter;
}
- private void doUnbind(boolean fireStatusEvent) {
+ private void doRemoveBean(boolean fireStatusEvent) {
setHasChanges(false);
if (bean != null) {
bean = null;
- bindings.forEach(BindingImpl::unbind);
+ bindings.forEach(BindingImpl::reset);
}
getValidationStatusHandler()
.accept(BinderValidationStatus.createUnresolvedStatus(this));
package com.vaadin.data;
import java.util.EventObject;
-import java.util.function.BiConsumer;
-import java.util.function.Function;
import com.vaadin.data.Binder.Binding;
+import com.vaadin.server.SerializableBiConsumer;
+import com.vaadin.server.SerializableFunction;
/**
* Binder status change event.
* The {@link Binder} status is changed whenever any of the following happens:
* <ul>
* <li>if it's bound and any of its bound field or select has been changed
- * <li>{@link #save(Object)} or {@link #saveIfValid(Object)} is called
- * <li>{@link #load(Object)} is called
- * <li>{@link #bind(Object)} is called
- * <li>{@link #unbind(Object)} is called
- * <li>{@link Binding#bind(Function, BiConsumer)} is called
+ * <li>{@link Binder#writeBean(Object)} or
+ * {@link Binder#writeBeanIfValid(Object)} is called
+ * <li>{@link Binder#readBean(Object)} is called
+ * <li>{@link Binder#setBean(Object)} is called
+ * <li>{@link Binder#removeBean()} is called
+ * <li>{@link Binding#bind(SerializableFunction, SerializableBiConsumer)} is
+ * called
* <li>{@link Binder#validate()} or {@link Binding#validate()} is called
* </ul>
- *
+ *
* @see StatusChangeListener#statusChange(StatusChangeEvent)
* @see Binder#addStatusChangeListener(StatusChangeListener)
- *
+ *
* @author Vaadin Ltd
*
*/
/**
* Create a new status change event for given {@code binder} using its
* current validation status.
- *
+ *
* @param binder
* the event source binder
* @param hasValidationErrors
/**
* Gets the binder validation status.
- *
+ *
* @return {@code true} if the binder has validation errors, {@code false}
* otherwise
*/
/**
* Gets the binder.
- *
+ *
* @return the binder
*/
public Binder<?> getBinder() {
import java.util.stream.Collectors;
/**
- * Indicates validation errors in a {@link Binder} when save is requested.
+ * Indicates validation errors in a {@link Binder} when a field value is
+ * validated.
*
- * @see Binder#save(Object)
+ * @see Binder#writeBean(Object)
+ * @see Binder#writeBeanIfValid(Object)
*
* @author Vaadin Ltd
* @since 8.0
person.setFirstName("foo");
person.setBirthDate(LocalDate.now());
- binder.bind(person);
+ binder.setBean(person);
Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
Assert.assertEquals(person.getBirthDate(), form.birthDate.getValue());
Person person = new Person();
person.setFirstName("foo");
- binder.bind(person);
+ binder.setBean(person);
Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
Person person = new Person();
person.setFirstName("foo");
- binder.bind(person);
+ binder.setBean(person);
Assert.assertNull(form.firstName);
}
Person person = new Person();
person.setFirstName("foo");
- binder.bind(person);
+ binder.setBean(person);
Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
Person person = new Person();
person.setFirstName("foo");
- binder.bind(person);
+ binder.setBean(person);
Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
Person person = new Person();
person.setFirstName("foo");
- binder.bind(person);
+ binder.setBean(person);
Assert.assertEquals(name, form.firstName);
}
person.setFirstName("foo");
person.setBirthDate(LocalDate.now());
- binder.bind(person);
+ binder.setBean(person);
Assert.assertEquals(person.getFirstName(), form.nameField.getValue());
Assert.assertEquals(person.getBirthDate(),
person.setFirstName(personName);
person.setBirthDate(LocalDate.now());
- binder.bind(person);
+ binder.setBean(person);
Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
Assert.assertEquals(person.getBirthDate(), form.birthDate.getValue());
person.setFirstName(personName);
person.setAge(age);
- binder.bind(person);
+ binder.setBean(person);
Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
Assert.assertEquals(String.valueOf(person.getAge()),
@Test
public void fieldBound_bindBean_fieldValueUpdated() {
binder.bind(nameField, "firstname");
- binder.bind(item);
+ binder.setBean(item);
assertEquals("Johannes", nameField.getValue());
}
@Test
public void beanBound_bindField_fieldValueUpdated() {
- binder.bind(item);
+ binder.setBean(item);
binder.bind(nameField, "firstname");
assertEquals("Johannes", nameField.getValue());
@Test
public void beanBound_setValidFieldValue_propertyValueChanged() {
- binder.bind(item);
+ binder.setBean(item);
binder.bind(nameField, "firstname");
nameField.setValue("Henri");
@Test
public void readOnlyPropertyBound_setFieldValue_ignored() {
binder.bind(nameField, "readOnlyProperty");
- binder.bind(item);
+ binder.setBean(item);
String propertyValue = item.getReadOnlyProperty();
nameField.setValue("Foo");
@Test
public void beanBound_setInvalidFieldValue_validationError() {
- binder.bind(item);
+ binder.setBean(item);
binder.bind(nameField, "firstname");
nameField.setValue("H"); // too short
@Test(expected = ClassCastException.class)
public void fieldWithIncompatibleTypeBound_bindBean_throws() {
binder.bind(ageField, "age");
- binder.bind(item);
+ binder.setBean(item);
}
@Test(expected = ClassCastException.class)
public void fieldWithIncompatibleTypeBound_loadBean_throws() {
binder.bind(ageField, "age");
- binder.load(item);
+ binder.readBean(item);
}
@Test(expected = ClassCastException.class)
throws Throwable {
try {
binder.bind(ageField, "age");
- binder.save(item);
+ binder.writeBean(item);
} catch (RuntimeException e) {
throw e.getCause();
}
public void fieldWithConverterBound_bindBean_fieldValueUpdated() {
binder.forField(ageField)
.withConverter(Integer::valueOf, String::valueOf).bind("age");
- binder.bind(item);
+ binder.setBean(item);
assertEquals("32", ageField.getValue());
}
public void fieldWithInvalidConverterBound_bindBean_fieldValueUpdated() {
binder.forField(ageField).withConverter(Float::valueOf, String::valueOf)
.bind("age");
- binder.bind(item);
+ binder.setBean(item);
assertEquals("32", ageField.getValue());
}
public void beanBinderWithBoxedType() {
binder.forField(ageField)
.withConverter(Integer::valueOf, String::valueOf).bind("age");
- binder.bind(item);
+ binder.setBean(item);
ageField.setValue(String.valueOf(20));
assertEquals(20, item.getAge());
@Test
public void loadingFromBusinessObjects() {
// this test is just to make sure the code snippet in the book compiles
- binder.load(new BookPerson(1969, 50000));
+ binder.readBean(new BookPerson(1969, 50000));
BinderValidationStatus<BookPerson> status = binder.validate();
public void handlingCheckedException() {
// another test just to verify that book examples actually compile
try {
- binder.save(new BookPerson(2000, 50000));
+ binder.writeBean(new BookPerson(2000, 50000));
} catch (ValidationException e) {
Notification.show("Validation error count: "
+ e.getValidationErrors().size());
// Test that the book code works
BookPerson bookPerson = new BookPerson(1972, 4);
- binder.bind(bookPerson);
+ binder.setBean(bookPerson);
Assert.assertEquals(4.0, salaryLevelField.getValue().doubleValue(), 0);
Assert.assertEquals("1,972", yearOfBirthField.getValue());
bookPerson.setSalaryLevel(8);
- binder.load(bookPerson);
+ binder.readBean(bookPerson);
Assert.assertEquals(8.0, salaryLevelField.getValue().doubleValue(), 0);
bookPerson.setYearOfBirth(123);
- binder.load(bookPerson);
+ binder.readBean(bookPerson);
Assert.assertEquals("123", yearOfBirthField.getValue());
yearOfBirthField.setValue("2016");
"Please enter a number")
.bind(BookPerson::getYearOfBirth, BookPerson::setYearOfBirth);
- binder.bind(new BookPerson(1900, 5));
+ binder.setBean(new BookPerson(1900, 5));
yearOfBirthField.setValue("abc");
binder.validate();
Assert.assertEquals("Please enter a number",
BookPerson person = new BookPerson(1900, 5);
person.setEmail("Old Email");
// Load person data to a form
- binder.load(person);
+ binder.readBean(person);
Button saveButton = new Button("Save", event -> {
// Using saveIfValid to avoid the try-catch block that is
// needed if using the regular save method
- if (binder.saveIfValid(person)) {
+ if (binder.writeBeanIfValid(person)) {
// Person is valid and updated
// TODO Store in the database
}
});
emailField.setValue("foo@bar.com");
- Assert.assertTrue(binder.saveIfValid(person));
+ Assert.assertTrue(binder.writeBeanIfValid(person));
// Person updated
Assert.assertEquals("foo@bar.com", person.getEmail());
emailField.setValue("");
- Assert.assertFalse(binder.saveIfValid(person));
+ Assert.assertFalse(binder.writeBeanIfValid(person));
// Person updated because phone and email are both empty
Assert.assertEquals("foo@bar.com", person.getEmail());
}
yearOfBirthField.setValue("1950");
Assert.assertFalse(binder.validate().hasErrors());
BookPerson person = new BookPerson(1500, 12);
- binder.save(person);
+ binder.writeBean(person);
Assert.assertEquals(1950, person.getYearOfBirth());
}
.bind(BookPerson::getYearOfBirth, BookPerson::setYearOfBirth);
BookPerson p = new BookPerson(1500, 12);
- binder.bind(p);
+ binder.setBean(p);
yearOfBirthField.setValue("abc");
Assert.assertTrue(binder.validate().hasErrors());
Assert.assertTrue(binder.validate().isOk());
p.setYearOfBirth(12500);
- binder.load(p);
+ binder.readBean(p);
Assert.assertEquals("12500", yearOfBirthField.getValue());
Assert.assertTrue(binder.validate().isOk());
}
.withValidator(bean -> bean.yearOfBirth == 2000
? Result.error(message2) : Result.ok(bean));
- binder.bind(p);
+ binder.setBean(p);
// first bean validator fails and passes error message to status label
yearOfBirth.setValue("2001");
.withValidator(bean -> bean.yearOfBirth == 2000
? Result.error(message2) : Result.ok(bean));
- binder.bind(p);
+ binder.setBean(p);
// first binding validation fails, no bean level validation is done
yearOfBirth.setValue("2001");
verifyEventIsFired(eventIsFired);
BookPerson person = new BookPerson(2000, 1);
- binder.load(person);
+ binder.readBean(person);
// no changes
Assert.assertFalse(saveButton.isEnabled());
Assert.assertFalse(resetButton.isEnabled());
Assert.assertTrue(resetButton.isEnabled());
Assert.assertTrue(eventIsFired.get());
- binder.saveIfValid(person);
+ binder.writeBeanIfValid(person);
// no changes
Assert.assertFalse(saveButton.isEnabled());
Assert.assertFalse(resetButton.isEnabled());
verifyEventIsFired(eventIsFired);
BookPerson person = new BookPerson(2000, 1);
- binder.bind(person);
+ binder.setBean(person);
// no changes
Assert.assertFalse(saveButton.isEnabled());
Assert.assertFalse(resetButton.isEnabled());
// set valid value
field.setValue("a");
verifyEventIsFired(eventIsFired);
- binder.saveIfValid(person);
+ binder.writeBeanIfValid(person);
// there are no changes.
Assert.assertFalse(saveButton.isEnabled());
Assert.assertFalse(resetButton.isEnabled());
field.setValue(initialValue);
Assert.assertEquals("Initial value of field unexpected", initialValue,
field.getValue());
- binder.bind(item);
+ binder.setBean(item);
Assert.assertEquals("Null representation for field failed",
field.getEmptyValue(), field.getValue());
field.setValue(initialValue);
.bind(Person::getSalaryDouble, Person::setSalaryDouble);
Person person = new Person();
- binder.bind(person);
+ binder.setBean(person);
salaryField.setValue("10");
assertEquals(10, person.getSalaryDouble(), 0);
salaryField.setValue("-1"); // Does not pass validator
public void convertDataToField() {
bindAgeWithValidatorConverterValidator();
binder.getBean().get().setAge(12);
- binder.load(binder.getBean().get());
+ binder.readBean(binder.getBean().get());
assertEquals("12", ageField.getValue());
}
public void convertNotValidatableDataToField() {
bindAgeWithValidatorConverterValidator();
binder.getBean().get().setAge(-12);
- binder.load(binder.getBean().get());
+ binder.readBean(binder.getBean().get());
assertEquals("-12", ageField.getValue());
}
}
});
binding.bind(StatusBean::getStatus, StatusBean::setStatus);
- binder.bind(bean);
+ binder.setBean(bean);
bean.setStatus("3");
- binder.load(bean);
+ binder.readBean(bean);
}
@Test
String msg = "foo";
binder.withValidator(Validator.from(bean -> false, msg));
Person person = new Person();
- binder.bind(person);
+ binder.setBean(person);
List<ValidationStatus<?>> errors = binder.validate()
.getFieldValidationErrors();
binder.withValidator(Validator.from(bean -> false, msg));
Person person = new Person();
- binder.bind(person);
+ binder.setBean(person);
List<ValidationStatus<?>> errors = binder.validate()
.getFieldValidationErrors();
String msg2 = "bar";
binder.withValidator(Validator.from(bean -> false, msg2));
Person person = new Person();
- binder.bind(person);
+ binder.setBean(person);
List<ValidationStatus<?>> errors = binder.validate()
.getFieldValidationErrors();
String msg = "foo";
binder.withValidator(Validator.from(bean -> true, msg));
Person person = new Person();
- binder.bind(person);
+ binder.setBean(person);
assertFalse(binder.validate().hasErrors());
assertTrue(binder.validate().isOk());
Person person = new Person();
String firstName = "first name";
person.setFirstName(firstName);
- binder.load(person);
+ binder.readBean(person);
nameField.setValue("");
- assertFalse(binder.saveIfValid(person));
+ assertFalse(binder.writeBeanIfValid(person));
// check that field level-validation failed and bean is not updated
assertEquals(firstName, person.getFirstName());
nameField.setValue("new name");
- assertFalse(binder.saveIfValid(person));
+ assertFalse(binder.writeBeanIfValid(person));
// Bean is updated but reverted
assertEquals(firstName, person.getFirstName());
}
"Name can't be empty"))
.bind(Person::getFirstName, Person::setFirstName);
assertFalse(binder.hasChanges());
- binder.bind(item);
+ binder.setBean(item);
assertFalse(binder.hasChanges());
nameField.setValue("foo");
assertTrue(binder.hasChanges());
- binder.load(item);
+ binder.readBean(item);
assertFalse(binder.hasChanges());
nameField.setValue("bar");
- binder.saveIfValid(new Person());
+ binder.writeBeanIfValid(new Person());
assertFalse(binder.hasChanges());
nameField.setValue("baz");
- binder.save(new Person());
+ binder.writeBean(new Person());
assertFalse(binder.hasChanges());
nameField.setValue("");
- binder.saveIfValid(new Person());
+ binder.writeBeanIfValid(new Person());
assertTrue(binder.hasChanges());
}
person.setFirstName(firstName);
nameField.setValue("");
try {
- binder.save(person);
+ binder.writeBean(person);
} finally {
// Bean should not have been updated
Assert.assertEquals(firstName, person.getFirstName());
Person person = new Person();
nameField.setValue("foo");
try {
- binder.save(person);
+ binder.writeBean(person);
} finally {
// Bean should have been updated for item validation but reverted
Assert.assertNull(person.getFirstName());
Person person = new Person();
person.setLastName("bar");
nameField.setValue("foo");
- binder.save(person);
+ binder.writeBean(person);
Assert.assertEquals(nameField.getValue(), person.getFirstName());
Assert.assertEquals("bar", person.getLastName());
}
Person person = new Person();
person.setFirstName("foo");
nameField.setValue("");
- Assert.assertFalse(binder.saveIfValid(person));
+ Assert.assertFalse(binder.writeBeanIfValid(person));
Assert.assertEquals("foo", person.getFirstName());
}
person.setFirstName("foo");
nameField.setValue("bar");
- Assert.assertTrue(binder.saveIfValid(person));
+ Assert.assertTrue(binder.writeBeanIfValid(person));
Assert.assertEquals("bar", person.getFirstName());
}
Person person = new Person();
person.setFirstName("foo");
nameField.setValue("");
- Assert.assertFalse(binder.saveIfValid(person));
+ Assert.assertFalse(binder.writeBeanIfValid(person));
Assert.assertEquals("foo", person.getFirstName());
}
nameField.setValue("null");
- binder.save(person);
+ binder.writeBean(person);
Assert.assertNull(person.getFirstName());
}
nameField.setValue("");
ageField.setValue("-1");
try {
- binder.save(person);
+ binder.writeBean(person);
Assert.fail();
} catch (ValidationException exception) {
List<ValidationStatus<?>> validationErrors = exception
? Result.error("error") : Result.ok(bean));
Person person = new Person();
person.setFirstName("");
- binder.bind(person);
+ binder.setBean(person);
// initial value is invalid but no error
Assert.assertNull(nameField.getComponentError());
// bind to another person to see that error is cleared
person = new Person();
person.setFirstName("");
- binder.bind(person);
+ binder.setBean(person);
// error has been cleared
Assert.assertNull(nameField.getComponentError());
Assert.assertNotNull(nameField.getComponentError());
// load should also clear error
- binder.load(person);
+ binder.readBean(person);
Assert.assertNull(nameField.getComponentError());
// bind a new field that has invalid value in bean
Assert.assertEquals("error", statusLabel.getValue());
// reload bean to clear error
- binder.load(person);
+ binder.readBean(person);
Assert.assertEquals("", statusLabel.getValue());
- // unbind() should clear all errors and status label
+ // reset() should clear all errors and status label
nameField.setValue("");
lastNameField.setValue("");
Assert.assertNotNull(nameField.getComponentError());
Assert.assertNotNull(lastNameField.getComponentError());
statusLabel.setComponentError(new UserError("ERROR"));
- binder.unbind();
+ binder.removeBean();
Assert.assertNull(nameField.getComponentError());
Assert.assertNull(lastNameField.getComponentError());
Assert.assertEquals("", statusLabel.getValue());
nameField.addValueChangeListener(v -> lastNameBinding.validate());
Person person = new Person();
- binder.bind(person);
+ binder.setBean(person);
Assert.assertNull(nameField.getComponentError());
Assert.assertNull(lastNameField.getComponentError());
Assert.assertNotNull(nameField.getComponentError());
Assert.assertNotNull(lastNameField.getComponentError());
- binder.bind(person);
+ binder.setBean(person);
Assert.assertNull(nameField.getComponentError());
Assert.assertNull(lastNameField.getComponentError());
protected void bindName() {
binder.bind(nameField, Person::getFirstName, Person::setFirstName);
- binder.bind(item);
+ binder.setBean(item);
}
protected void bindAgeWithValidatorConverterValidator() {
binder.forField(ageField).withValidator(notEmpty)
.withConverter(stringToInteger).withValidator(notNegative)
.bind(Person::getAge, Person::setAge);
- binder.bind(item);
+ binder.setBean(item);
}
@Test(expected = ValidationException.class)
Person person = new Person();
ageField.setValue("1");
try {
- binder.save(person);
+ binder.writeBean(person);
} finally {
// Bean should have been updated for item validation but reverted
Assert.assertEquals(0, person.getAge());
@Test
public void beanBound_bindSelectByShortcut_selectionUpdated() {
item.setEnums(Collections.singleton(TestEnum.ONE));
- binder.bind(item);
+ binder.setBean(item);
binder.bind(select, BeanWithEnums::getEnums, BeanWithEnums::setEnums);
assertEquals(Collections.singleton(TestEnum.ONE),
@Test
public void beanBound_bindSelect_selectionUpdated() {
item.setEnums(Collections.singleton(TestEnum.TWO));
- binder.bind(item);
+ binder.setBean(item);
binder.forField(select).bind(BeanWithEnums::getEnums,
BeanWithEnums::setEnums);
Set<TestEnum> enums = item.getEnums();
- binder.bind(new BeanWithEnums());
+ binder.setBean(new BeanWithEnums());
select.select(TestEnum.ONE);
assertEquals(Collections.singleton(TestEnum.TWO), enums);
public void unbound_changeSelection_beanValueNotUpdated() {
item.setEnums(Collections.singleton(TestEnum.ONE));
bindEnum();
- binder.unbind();
+ binder.removeBean();
select.select(TestEnum.TWO);
@Test
public void withConverter_load_selectUpdated() {
- converterBinder.load(new AtomicReference<>("TWO"));
+ converterBinder.readBean(new AtomicReference<>("TWO"));
assertEquals(Collections.singleton(TestEnum.TWO),
select.getSelectionModel().getSelectedItems());
select.select(TestEnum.TWO);
AtomicReference<String> reference = new AtomicReference<>("");
- converterBinder.saveIfValid(reference);
+ converterBinder.writeBeanIfValid(reference);
assertEquals("ONE,TWO", reference.get());
}
.withValidator(selection -> selection.size() % 2 == 1,
"Must select odd number of items")
.bind(BeanWithEnums::getEnums, BeanWithEnums::setEnums);
- binder.bind(item);
+ binder.setBean(item);
assertFalse(binder.validate().isOk());
protected void bindEnum() {
binder.forField(select).bind(BeanWithEnums::getEnums,
BeanWithEnums::setEnums);
- binder.bind(item);
+ binder.setBean(item);
}
}
@Test
public void personBound_bindSelectByShortcut_selectionUpdated() {
item.setSex(Sex.FEMALE);
- binder.bind(item);
+ binder.setBean(item);
binder.bind(select, Person::getSex, Person::setSex);
assertSame(Sex.FEMALE, select.getSelectedItem().orElse(null));
@Test
public void personBound_bindSelect_selectionUpdated() {
item.setSex(Sex.MALE);
- binder.bind(item);
+ binder.setBean(item);
binder.forField(select).bind(Person::getSex, Person::setSex);
assertSame(Sex.MALE, select.getSelectedItem().orElse(null));
public void unbound_changeSelection_beanValueNotUpdated() {
item.setSex(Sex.UNKNOWN);
bindSex();
- binder.unbind();
+ binder.removeBean();
select.select(Sex.FEMALE);
protected void bindSex() {
binder.forField(select).bind(Person::getSex, Person::setSex);
- binder.bind(item);
+ binder.setBean(item);
}
}
Assert.assertNull(event.get());
- binder.bind(item);
+ binder.setBean(item);
verifyEvent();
}
@Test
- public void unbindBinder_bound_singleEventWhenBound() {
- binder.bind(item);
+ public void removeBean_bound_singleEventWhenUnBound() {
+ binder.setBean(item);
- unbindBinder_unbound_singleEventWhenBound();
+ binder.addStatusChangeListener(this::statusChanged);
+
+ Assert.assertNull(event.get());
+ binder.removeBean();
+ verifyEvent();
}
@Test
- public void unbindBinder_unbound_singleEventWhenBound() {
+ public void removeBean_unbound_noEventWhenUnBound() {
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
- binder.unbind();
- verifyEvent();
+ binder.removeBean();
+ Assert.assertNull(event.get());
}
@Test
public void setValue_bound_singleEventOnSetValue() {
binder.forField(nameField).bind(Person::getFirstName,
Person::setFirstName);
- binder.bind(item);
+ binder.setBean(item);
binder.addStatusChangeListener(this::statusChanged);
binder.forField(ageField)
.withConverter(new StringToIntegerConverter(""))
.bind(Person::getAge, Person::setAge);
- binder.bind(item);
+ binder.setBean(item);
binder.addStatusChangeListener(this::statusChanged);
public void setInvalidValue_bound_singleEventOnSetValue() {
binder.forField(nameField).withValidator(name -> false, "")
.bind(Person::getFirstName, Person::setFirstName);
- binder.bind(item);
+ binder.setBean(item);
binder.addStatusChangeListener(this::statusChanged);
public void setInvalidBean_bound_singleEventOnSetValue() {
binder.forField(nameField).bind(Person::getFirstName,
Person::setFirstName);
- binder.bind(item);
+ binder.setBean(item);
binder.withValidator(Validator.from(bean -> false, ""));
Person::setFirstName);
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
- binder.load(item);
+ binder.readBean(item);
verifyEvent();
}
.bind(Person::getAge, Person::setAge);
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
- binder.load(item);
+ binder.readBean(item);
verifyEvent();
}
public void load_hasNoBindings_singleEvent() {
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
- binder.load(item);
+ binder.readBean(item);
verifyEvent();
}
public void save_hasNoBindings_singleEvent() throws ValidationException {
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
- binder.save(item);
+ binder.writeBean(item);
verifyEvent();
}
public void saveIfValid_hasNoBindings_singleEvent() {
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
- binder.saveIfValid(item);
+ binder.writeBeanIfValid(item);
verifyEvent();
}
public void save_hasBindings_singleEvent() throws ValidationException {
binder.forField(nameField).bind(Person::getFirstName,
Person::setFirstName);
- binder.load(item);
+ binder.readBean(item);
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
- binder.save(item);
+ binder.writeBean(item);
verifyEvent();
}
binder.forField(ageField)
.withConverter(new StringToIntegerConverter(""))
.bind(Person::getAge, Person::setAge);
- binder.load(item);
+ binder.readBean(item);
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
- binder.save(item);
+ binder.writeBean(item);
verifyEvent();
}
public void saveIfValid_hasBindings_singleEvent() {
binder.forField(nameField).bind(Person::getFirstName,
Person::setFirstName);
- binder.load(item);
+ binder.readBean(item);
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
- binder.saveIfValid(item);
+ binder.writeBeanIfValid(item);
verifyEvent();
}
binder.forField(ageField)
.withConverter(new StringToIntegerConverter(""))
.bind(Person::getAge, Person::setAge);
- binder.load(item);
+ binder.readBean(item);
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
- binder.saveIfValid(item);
+ binder.writeBeanIfValid(item);
verifyEvent();
}
public void saveInvalidValue_hasBindings_singleEvent() {
binder.forField(nameField).withValidator(name -> false, "")
.bind(Person::getFirstName, Person::setFirstName);
- binder.load(item);
+ binder.readBean(item);
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
try {
- binder.save(item);
+ binder.writeBean(item);
} catch (ValidationException ignore) {
}
verifyEvent(true);
public void saveIfValid_invalidValueAndBinderHasBindings_singleEvent() {
binder.forField(nameField).withValidator(name -> false, "")
.bind(Person::getFirstName, Person::setFirstName);
- binder.load(item);
+ binder.readBean(item);
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
- binder.saveIfValid(item);
+ binder.writeBeanIfValid(item);
verifyEvent(true);
}
binder.forField(ageField)
.withConverter(new StringToIntegerConverter(""))
.bind(Person::getAge, Person::setAge);
- binder.load(item);
+ binder.readBean(item);
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
- binder.saveIfValid(item);
+ binder.writeBeanIfValid(item);
verifyEvent(true);
}
public void saveInvalidBean_hasBindings_singleEvent() {
binder.forField(nameField).bind(Person::getFirstName,
Person::setFirstName);
- binder.load(item);
+ binder.readBean(item);
binder.withValidator(Validator.from(person -> false, ""));
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
try {
- binder.save(item);
+ binder.writeBean(item);
} catch (ValidationException ignore) {
}
verifyEvent(true);
public void saveIfValid_invalidBeanAndBinderHasBindings_singleEvent() {
binder.forField(nameField).bind(Person::getFirstName,
Person::setFirstName);
- binder.load(item);
+ binder.readBean(item);
binder.withValidator(Validator.from(person -> false, ""));
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
- binder.saveIfValid(item);
+ binder.writeBeanIfValid(item);
verifyEvent(true);
}
throws ValidationException {
binder.forField(nameField).bind(Person::getFirstName,
Person::setFirstName);
- binder.load(item);
+ binder.readBean(item);
binder.withValidator(Validator.from(person -> true, ""));
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
- binder.save(item);
+ binder.writeBean(item);
verifyEvent();
}
public void saveIfValid_validBeanAndBinderHasBindings_singleEvent() {
binder.forField(nameField).bind(Person::getFirstName,
Person::setFirstName);
- binder.load(item);
+ binder.readBean(item);
binder.withValidator(Validator.from(person -> true, ""));
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
- binder.saveIfValid(item);
+ binder.writeBeanIfValid(item);
verifyEvent();
}
binder.forField(ageField)
.withConverter(new StringToIntegerConverter(""))
.bind(Person::getAge, Person::setAge);
- binder.bind(item);
+ binder.setBean(item);
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
binder.forField(ageField)
.withConverter(new StringToIntegerConverter(""))
.bind(Person::getAge, Person::setAge);
- binder.bind(item);
+ binder.setBean(item);
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
binder.forField(ageField)
.withConverter(new StringToIntegerConverter(""))
.bind(Person::getAge, Person::setAge);
- binder.bind(item);
+ binder.setBean(item);
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
binder.forField(ageField)
.withConverter(new StringToIntegerConverter(""))
.bind(Person::getAge, Person::setAge);
- binder.bind(item);
+ binder.setBean(item);
binder.addStatusChangeListener(this::statusChanged);
Assert.assertNull(event.get());
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
import java.util.Objects;
item.setAge(32);
}
- @Test(expected = NullPointerException.class)
- public void bindNullBean_throws() {
- binder.bind(null);
+ @Test
+ public void bindNullBean_noBeanPresent() {
+ binder.setBean(item);
+ assertTrue(binder.getBean().isPresent());
+
+ binder.setBean(null);
+ assertFalse(binder.getBean().isPresent());
}
@Test(expected = NullPointerException.class)
public void fieldBound_bindItem_fieldValueUpdated() {
binder.forField(nameField).bind(Person::getFirstName,
Person::setFirstName);
- binder.bind(item);
+ binder.setBean(item);
assertEquals("Johannes", nameField.getValue());
}
@Test
public void beanBound_updateFieldValue_beanValueUpdated() {
- binder.bind(item);
+ binder.setBean(item);
binder.bind(nameField, Person::getFirstName, Person::setFirstName);
assertEquals("Johannes", nameField.getValue());
@Test
public void bound_getBean_returnsBoundBean() {
assertFalse(binder.getBean().isPresent());
- binder.bind(item);
+ binder.setBean(item);
assertSame(item, binder.getBean().get());
}
@Test
public void unbound_getBean_returnsNothing() {
- binder.bind(item);
- binder.unbind();
+ binder.setBean(item);
+ binder.removeBean();
assertFalse(binder.getBean().isPresent());
}
public void unbound_changeFieldValue_beanValueNotUpdated() {
bindName();
nameField.setValue("Henri");
- binder.unbind();
+ binder.removeBean();
nameField.setValue("Aleksi");
assertEquals("Henri", item.getFirstName());
}
@Test
public void bindNullSetter_valueChangesIgnored() {
binder.bind(nameField, Person::getFirstName, null);
- binder.bind(item);
+ binder.setBean(item);
nameField.setValue("Artur");
assertEquals(item.getFirstName(), "Johannes");
}
Person p2 = new Person();
p2.setFirstName("Marlon");
- binder.bind(p2);
+ binder.setBean(p2);
assertEquals("Marlon", nameField.getValue());
assertEquals("Leif", item.getFirstName());
assertSame(p2, binder.getBean().get());
int age = 10;
person.setAge(age);
- binder.save(person);
+ binder.writeBean(person);
Assert.assertEquals(age, person.getAge());
}
person.setFirstName("foo");
- binder.save(person);
+ binder.writeBean(person);
Assert.assertEquals(fieldValue, person.getFirstName());
}
String name = "bar";
person.setFirstName(name);
- binder.load(person);
+ binder.readBean(person);
Assert.assertEquals(name, nameField.getValue());
}
String name = "bar";
person.setFirstName(name);
- binder.load(person);
+ binder.readBean(person);
Assert.assertEquals("", nameField.getValue());
}
protected void bindName() {
binder.bind(nameField, Person::getFirstName, Person::setFirstName);
- binder.bind(item);
+ binder.setBean(item);
}
@Test
// Bind a person with null value and check that null representation is
// used
- binder.bind(namelessPerson);
+ binder.setBean(namelessPerson);
Assert.assertEquals(
"Null value from bean was not converted to explicit null representation",
nullRepresentation, nameField.getValue());
Person namelessPerson = new Person(null, "Doe", "", 25, Sex.UNKNOWN,
null);
binder.bind(nullTextField, Person::getFirstName, Person::setFirstName);
- binder.bind(namelessPerson);
+ binder.setBean(namelessPerson);
Assert.assertTrue(nullTextField.isEmpty());
Assert.assertEquals(null, namelessPerson.getFirstName());
Assert.assertFalse("First name in item should not be null",
Objects.isNull(item.getFirstName()));
- binder.bind(item);
+ binder.setBean(item);
Assert.assertEquals("Field value was not set correctly",
item.getFirstName(), nameField.getValue());
.withConverter(age -> age,
age -> age == null ? customNullConverter : age)
.bind(Person::getSalary, Person::setSalary);
- binder.bind(item);
+ binder.setBean(item);
Assert.assertEquals(customNullConverter.toString(),
ageField.getValue());
binder.forField(nameField).bind("firstName");
Person person = new Person();
- binder.bind(person);
+ binder.setBean(person);
Assert.assertEquals("", nameField.getValue());
}
.bind("firstName");
Person person = new Person();
- binder.bind(person);
+ binder.setBean(person);
Assert.assertEquals(customNullPointerRepresentation,
nameField.getValue());
.withValidator(new NotNullValidator(""))
.bind(Person::getFirstName, Person::setFirstName);
item.setFirstName(null);
- binder.bind(item);
+ binder.setBean(item);
Assert.assertEquals(nullRepresentation, nameField.getValue());
binder.setValidationStatusHandler(r -> {
statusCapture.set(r);
});
- binder.bind(item);
+ binder.setBean(item);
Assert.assertNull(nameField.getComponentError());
nameField.setValue("");
binder.setValidationStatusHandler(r -> {
statusCapture.set(r);
});
- binder.bind(item);
+ binder.setBean(item);
Assert.assertNull(nameField.getComponentError());
nameField.setValue("");
item.setAge(32);
binder.bind(nameField, "firstname");
- binder.bind(item);
+ binder.setBean(item);
assertEquals(name, nameField.getValue());
initFormFields(scheduleEventFieldLayout, event.getClass());
scheduleEventFieldGroup.setBuffered(true);
scheduleEventFieldGroup.setItemDataSource(item);
- scheduledEventBinder.load(event);
+ scheduledEventBinder.readBean(event);
}
private void setFormDateResolution(Resolution resolution) {
throws CommitException, ValidationException {
scheduleEventFieldGroup.commit();
BasicEvent event = getFormCalendarEvent();
- scheduledEventBinder.save(event);
+ scheduledEventBinder.writeBean(event);
if (event.getEnd() == null) {
event.setEnd(event.getStart());
}
private void discardCalendarEvent() {
scheduleEventFieldGroup.discard();
- scheduledEventBinder.load(getFormCalendarEvent());
+ scheduledEventBinder.readBean(getFormCalendarEvent());
removeWindow(scheduleEventPopup);
}
b -> Double.valueOf(b.getFloatValue() * 10.0),
(b, doubleValue) -> item.getItemProperty("floatValue")
.setValue((float) (doubleValue / 10.0)));
- binder.bind(bean);
+ binder.setBean(bean);
ProgressBar progressBar = new ProgressBar();
progressBar.setWidth("200px");
initFormFields(scheduleEventFieldLayout, event.getClass());
scheduleEventFieldGroup.setBuffered(true);
scheduleEventFieldGroup.setItemDataSource(item);
- scheduledEventBinder.load(event);
+ scheduledEventBinder.readBean(event);
}
private void setFormDateResolution(Resolution resolution) {
throws ValidationException, CommitException {
scheduleEventFieldGroup.commit();
BasicEvent event = getFormCalendarEvent();
- scheduledEventBinder.save(event);
+ scheduledEventBinder.writeBean(event);
if (event.getEnd() == null) {
event.setEnd(event.getStart());
}
private void discardCalendarEvent() {
scheduleEventFieldGroup.discard();
- scheduledEventBinder.load(getFormCalendarEvent());
+ scheduledEventBinder.readBean(getFormCalendarEvent());
getUI().removeWindow(scheduleEventPopup);
}