pl.art.go.mdb.util.feedbackPanel.ComponentVisualErrorBehavior.java Source code

Java tutorial

Introduction

Here is the source code for pl.art.go.mdb.util.feedbackPanel.ComponentVisualErrorBehavior.java

Source

package pl.art.go.mdb.util.feedbackPanel;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.model.Model;

/**
 * Behavior that checks if a {@link org.apache.wicket.markup.html.form.FormComponent} is valid. Valid {@link org.apache.wicket.markup.html.form.FormComponent} objects get the CSS class
 * 'formcomponent valid' and invalid {@link org.apache.wicket.markup.html.form.FormComponent} objects get the CSS class 'formcomponent invalid'.
 * <p/>
 * See {@link org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior} for more details over the parent class.
 * <p/>
 * You can use this code under Apache 2.0 license, as long as you retain the copyright messages.
 * <p/>
 * Tested with Wicket 1.3.4
 *
 * @author Daan, StuQ.nl
 */
public class ComponentVisualErrorBehavior extends AjaxFormComponentUpdatingBehavior {

    /**
     * Field updateComponent holds the component that must be updated when validation is done.
     */
    private Component updateComponent = null;

    /**
     * Constructor.
     *
     * @param event           of type {@link String} (for example 'onblur', 'onkeyup', etc.)
     * @param updateComponent is the {@link org.apache.wicket.Component} that must be updated
     *                        containing the error message for this {@link org.apache.wicket.markup.html.form.FormComponent})
     */
    public ComponentVisualErrorBehavior(String event, Component updateComponent) {
        super(event);
        this.updateComponent = updateComponent;
    }

    /**
     * Listener invoked on the ajax request. This listener is invoked after the {@link org.apache.wicket.Component}'s model has been
     * updated. Handles the change of a css class when an error has occurred.
     *
     * @param ajaxRequestTarget of type AjaxRequestTarget
     * @param e                 of type RuntimeException
     */
    @Override
    protected void onError(AjaxRequestTarget ajaxRequestTarget, RuntimeException e) {
        changeCssClass(ajaxRequestTarget, false, "error");
    }

    /**
     * Listener invoked on the ajax request. This listener is invoked after the {@link org.apache.wicket.Component}'s model has been
     * updated. Handles the change of a css class when validation was succesful.
     *
     * @param ajaxRequestTarget of type AjaxRequestTarget
     */
    @Override
    protected void onUpdate(AjaxRequestTarget ajaxRequestTarget) {
        changeCssClass(ajaxRequestTarget, true, "");
    }

    /**
     * Changes the CSS class of the linked {@link org.apache.wicket.markup.html.form.FormComponent} via AJAX.
     *
     * @param ajaxRequestTarget of type AjaxRequestTarget
     * @param valid             Was the validation succesful?
     * @param cssClass          The CSS class that must be set on the linked {@link org.apache.wicket.markup.html.form.FormComponent}
     */
    private void changeCssClass(AjaxRequestTarget ajaxRequestTarget, boolean valid, String cssClass) {
        FormComponent formComponent = getFormComponent();

        if (formComponent.isValid() == valid) {
            formComponent.add(new AttributeReplacer("class", new Model(FormFieldFeedbackMessageModel.SEPARATOR),
                    "form-field-fb-.*"));
            formComponent.add(new AttributeAppender("class", true,
                    new Model(FormFieldFeedbackMessageModel.FORM_FIELD_FEEDBACK_SUFFIX_CSS + cssClass),
                    FormFieldFeedbackMessageModel.SEPARATOR));
            ajaxRequestTarget.add(formComponent);
        }

        if (updateComponent != null) {
            ajaxRequestTarget.add(updateComponent);
        }
    }
}