Example usage for org.apache.wicket.markup.html.form Form visitFormComponentsPostOrder

List of usage examples for org.apache.wicket.markup.html.form Form visitFormComponentsPostOrder

Introduction

In this page you can find the example usage for org.apache.wicket.markup.html.form Form visitFormComponentsPostOrder.

Prototype

public final <R> R visitFormComponentsPostOrder(final IVisitor<? extends FormComponent<?>, R> visitor) 

Source Link

Document

Convenient and typesafe way to visit all the form components on a form postorder (deepest first)

Usage

From source file:com.doculibre.constellio.wicket.behaviors.SetFocusBehavior.java

License:Open Source License

/**
 * @see org.apache.wicket.behavior.AbstractBehavior#renderHead(org.apache.wicket.markup.html.IHeaderResponse)
 *//*from  w  w w  .j av a  2 s  .com*/
public void renderHead(final IHeaderResponse response) {
    Component focusComponent = (Component) focusComponentModel.getObject();
    if (focusComponent != null) {
        // If the focusComponent is a form, we will set the focus on one of
        // its fields.
        if (focusComponent instanceof Form) {
            Form form = (Form) focusComponent;

            // If the form has been submitted, we want to set the focus on
            // its first invalid field.
            if (form.isSubmitted()) {
                form.visitFormComponentsPostOrder(new IVisitor() {
                    public Object formComponent(IFormVisitorParticipant formParticipant) {
                        Object result;
                        if (formParticipant instanceof FormComponent) {
                            FormComponent formComponent = (FormComponent) formParticipant;
                            // We cannot set focus on an hidden field or a button.
                            if (!(formComponent instanceof HiddenField) && !(formComponent instanceof Button)
                                    && !formComponent.isValid()) {
                                setFocus(formComponent, response);
                                // Will break the loop.
                                result = Component.IVisitor.STOP_TRAVERSAL;
                            } else {
                                // Will continue the loop.
                                result = Component.IVisitor.CONTINUE_TRAVERSAL;
                            }
                        } else {
                            // Will continue the loop.
                            result = Component.IVisitor.CONTINUE_TRAVERSAL;
                        }
                        return result;
                    }
                });
            } else {
                // Since the form has not been submitted, we will set the
                // focus on its first field.
                form.visitFormComponentsPostOrder(new IVisitor() {
                    public Object formComponent(IFormVisitorParticipant formParticipant) {
                        Object result;
                        if (formParticipant instanceof FormComponent) {
                            FormComponent formComponent = (FormComponent) formParticipant;
                            // We cannot set focus on an hidden field or a button.
                            if (!(formComponent instanceof HiddenField) && !(formComponent instanceof Button)
                                    && formComponent.isEnabled() && formComponent.isVisible()) {
                                setFocus(formComponent, response);
                                // Will break the loop.
                                result = Component.IVisitor.STOP_TRAVERSAL;
                            } else {
                                // Will continue the loop.
                                result = Component.IVisitor.CONTINUE_TRAVERSAL;
                            }
                        } else {
                            // Will continue the loop.
                            result = Component.IVisitor.CONTINUE_TRAVERSAL;
                        }
                        return result;
                    }
                });
            }
        } else {
            // The focusComponent is not a form, so we will set the focus on
            // it.
            setFocus(focusComponent, response);
        }
    }
}