Skip to content

Commit

Permalink
Improve Binder JavaDocs and APIs (#10347)
Browse files Browse the repository at this point in the history
This patch also adds ValueChangeEvent as a parameter to field value change method in Binder.
  • Loading branch information
tsuoanttila committed Nov 21, 2017
1 parent 9d804fa commit 5bc0a2f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
29 changes: 22 additions & 7 deletions server/src/main/java/com/vaadin/data/Binder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ private FIELDVALUE convertDataToFieldType(BEAN bean) {
private void handleFieldValueChange(
ValueChangeEvent<FIELDVALUE> event) {
// Inform binder of changes; if setBean: writeIfValid
getBinder().handleFieldValueChange(this);
getBinder().handleFieldValueChange(this, event);
getBinder().fireValueChangeEvent(event);
}

Expand Down Expand Up @@ -1246,9 +1246,12 @@ protected Binder(PropertySet<BEAN> propertySet) {
*
* @param binding
* the binding whose value has been changed
* @param event
* the value change event
* @since 8.2
*/
protected void handleFieldValueChange(Binding<BEAN, ?> binding) {
protected void handleFieldValueChange(Binding<BEAN, ?> binding,
ValueChangeEvent<?> event) {
changedBindings.add(binding);
if (getBean() != null) {
doWriteIfValid(getBean(), changedBindings);
Expand Down Expand Up @@ -1692,7 +1695,9 @@ private BinderValidationStatus<BEAN> doWriteIfValid(BEAN bean,
}

/**
* Restores the state of the bean from the given values.
* Restores the state of the bean from the given values. This method is used
* together with {@link #getBeanState(Object, Collection)} to provide a way
* to revert changes in case the bean validation fails after save.
*
* @param bean
* the bean
Expand All @@ -1714,7 +1719,9 @@ protected void restoreBeanState(BEAN bean,
}

/**
* Stores the state of the given bean.
* Stores the state of the given bean. This method is used together with
* {@link #restoreBeanState(Object, Map)} to provide a way to revert changes
* in case the bean validation fails after save.
*
* @param bean
* the bean to store the state of
Expand Down Expand Up @@ -1842,8 +1849,8 @@ public BinderValidationStatus<BEAN> validate() {

/**
* Validates the values of all bound fields and returns the validation
* status. This method can skip firing the event, based on the given
* {@code boolean}.
* status. This method can fire validation status events. Firing the events
* depends on the given {@code boolean}.
*
* @param fireEvent
* {@code true} to fire validation status events; {@code false}
Expand Down Expand Up @@ -2683,9 +2690,17 @@ public void removeBinding(HasValue<?> field) {
* the binding to remove
*
* @since 8.2
*
* @throws IllegalArgumentException
* if the given Binding is not in this Binder
*/
public void removeBinding(Binding<BEAN, ?> binding) {
public void removeBinding(Binding<BEAN, ?> binding)
throws IllegalArgumentException {
Objects.requireNonNull(binding, "Binding can not be null");
if (!bindings.contains(binding)) {
throw new IllegalArgumentException(
"Provided Binding is not in this Binder");
}
binding.unbind();
}

Expand Down
8 changes: 8 additions & 0 deletions server/src/test/java/com/vaadin/data/BinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1011,4 +1011,12 @@ public void refreshValueFromBean() {
assertEquals("Name should be read again from the item",
item.getFirstName(), nameField.getValue());
}

@Test(expected = IllegalArgumentException.class)
public void remove_binding_from_different_binder() {
Binder<Person> anotherBinder = new Binder<>();
Binding<Person, String> binding = anotherBinder.bind(nameField,
Person::getFirstName, Person::setFirstName);
binder.removeBinding(binding);
}
}

0 comments on commit 5bc0a2f

Please sign in to comment.