From 1f59cb51af097b8e314fde6d7c3a25821d13868d Mon Sep 17 00:00:00 2001 From: Teemu Suo-Anttila Date: Tue, 19 Sep 2017 16:09:56 +0300 Subject: Add API for running BindingValidationStatusHandlers (#9966) This patch adds an API to Binding for accessing the validation status handlers from a custom BinderValidationStatusHandler. --- server/src/main/java/com/vaadin/data/Binder.java | 19 ++++++++---- .../com/vaadin/data/BinderValidationStatus.java | 35 ++++++++++++++++++++-- .../vaadin/data/BinderValidationStatusHandler.java | 6 ++-- 3 files changed, 49 insertions(+), 11 deletions(-) (limited to 'server/src/main') diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java index 0582182d16..75d210eb61 100644 --- a/server/src/main/java/com/vaadin/data/Binder.java +++ b/server/src/main/java/com/vaadin/data/Binder.java @@ -126,6 +126,15 @@ public class Binder implements Serializable { */ public BindingValidationStatus validate(); + /** + * Gets the validation status handler for this Binding. + * + * @return the validation status handler for this binding + * + * @since 8.2 + */ + public BindingValidationStatusHandler getValidationStatusHandler(); + /** * Unbinds the binding from its respective {@code Binder} * Removes any {@code ValueChangeListener} {@code Registration} from @@ -134,7 +143,6 @@ public class Binder implements Serializable { * @since 8.2 */ public void unbind(); - } /** @@ -990,8 +998,9 @@ public class Binder implements Serializable { return binder; } - private void notifyStatusHandler(BindingValidationStatus status) { - statusHandler.statusChange(status); + @Override + public BindingValidationStatusHandler getValidationStatusHandler() { + return statusHandler; } } @@ -1948,9 +1957,7 @@ public class Binder implements Serializable { protected void handleBinderValidationStatus( BinderValidationStatus binderStatus) { // let field events go to binding status handlers - binderStatus.getFieldValidationStatuses() - .forEach(status -> ((BindingImpl) status.getBinding()) - .notifyStatusHandler(status)); + binderStatus.notifyBindingValidationStatusHandlers(); // show first possible error or OK status in the label if set if (getStatusLabel().isPresent()) { diff --git a/server/src/main/java/com/vaadin/data/BinderValidationStatus.java b/server/src/main/java/com/vaadin/data/BinderValidationStatus.java index 90600f1800..151a4b549b 100644 --- a/server/src/main/java/com/vaadin/data/BinderValidationStatus.java +++ b/server/src/main/java/com/vaadin/data/BinderValidationStatus.java @@ -23,6 +23,7 @@ import java.util.Objects; import java.util.stream.Collectors; import com.vaadin.data.Binder.BindingBuilder; +import com.vaadin.server.SerializablePredicate; /** * Binder validation status change. Represents the outcome of binder level @@ -33,8 +34,8 @@ import com.vaadin.data.Binder.BindingBuilder; * Note: if there are any field level validation errors, the bean level * validation is not run. *

- * Use {@link Binder#setValidationStatusHandler(BinderValidationStatusHandler)} to handle - * form level validation status changes. + * Use {@link Binder#setValidationStatusHandler(BinderValidationStatusHandler)} + * to handle form level validation status changes. * * @author Vaadin Ltd * @@ -188,4 +189,34 @@ public class BinderValidationStatus implements Serializable { return binderStatuses.stream().filter(ValidationResult::isError) .collect(Collectors.toList()); } + + /** + * Notifies all validation status handlers in bindings. + * + * @see #notifyBindingValidationStatusHandlers(SerializablePredicate) + * + * @since 8.2 + */ + public void notifyBindingValidationStatusHandlers() { + notifyBindingValidationStatusHandlers(t -> true); + } + + /** + * Notifies validation status handlers for bindings that pass given filter. + * The filter should return {@code true} for each + * {@link BindingValidationStatus} that should be delegated to the status + * handler in the binding. + * + * @see #notifyBindingValidationStatusHandlers() + * + * @param filter + * the filter to select bindings to run status handling for + * + * @since 8.2 + */ + public void notifyBindingValidationStatusHandlers( + SerializablePredicate> filter) { + bindingStatuses.stream().filter(filter).forEach(s -> s.getBinding() + .getValidationStatusHandler().statusChange(s)); + } } diff --git a/server/src/main/java/com/vaadin/data/BinderValidationStatusHandler.java b/server/src/main/java/com/vaadin/data/BinderValidationStatusHandler.java index 9f144ac430..3bfa9c863a 100644 --- a/server/src/main/java/com/vaadin/data/BinderValidationStatusHandler.java +++ b/server/src/main/java/com/vaadin/data/BinderValidationStatusHandler.java @@ -21,8 +21,9 @@ import com.vaadin.ui.AbstractComponent; /** * Handler for {@link BinderValidationStatus} changes. *

- * {@link Binder#setValidationStatusHandler(BinderValidationStatusHandler) Register} an - * instance of this class to be able to customize validation status handling. + * {@link Binder#setValidationStatusHandler(BinderValidationStatusHandler) + * Register} an instance of this class to be able to customize validation status + * handling. *

* The default handler will show * {@link AbstractComponent#setComponentError(com.vaadin.server.ErrorMessage) an @@ -53,5 +54,4 @@ public interface BinderValidationStatusHandler * the changed status */ void statusChange(BinderValidationStatus statusChange); - } -- cgit v1.2.3