Search in sources :

Example 1 with AutoLabel

use of org.apache.wicket.markup.html.form.AutoLabelResolver.AutoLabel in project wicket by apache.

the class AutoLabelTextResolver method resolve.

@Override
public Component resolve(MarkupContainer container, MarkupStream markupStream, ComponentTag tag) {
    if (tag instanceof WicketTag && "label".equals(tag.getName())) {
        // We need to find a FormComponent...
        Component related = null;
        // ...which could be explicitly specified...
        String forAttributeValue = tag.getAttribute("for");
        if (forAttributeValue != null) {
            Component component = AutoLabelResolver.findRelatedComponent(container, forAttributeValue);
            related = component;
        }
        if (related == null) {
            // ...or available through an AutoLabel, either directly above us...
            if (container instanceof AutoLabel) {
                related = ((AutoLabel) container).getRelatedComponent();
            }
            if (related == null) {
                // ...or perhaps further up...
                AutoLabel autoLabel = container.findParent(AutoLabel.class);
                if (autoLabel != null) {
                    related = autoLabel.getRelatedComponent();
                }
            }
        }
        if (related == null) {
            // ...or it might just not be available.
            String forAttr = forAttributeValue != null ? " for=\"" + forAttributeValue + "\"" : "";
            throw new ComponentNotFoundException("no related component found for <wicket:label" + forAttr + ">");
        } else {
            // ...found the form component, so we can return our label.
            return new TextLabel(tag.getId(), related);
        }
    }
    return null;
}
Also used : WicketTag(org.apache.wicket.markup.WicketTag) ComponentNotFoundException(org.apache.wicket.core.request.handler.ComponentNotFoundException) AutoLabel(org.apache.wicket.markup.html.form.AutoLabelResolver.AutoLabel) Component(org.apache.wicket.Component)

Aggregations

Component (org.apache.wicket.Component)1 ComponentNotFoundException (org.apache.wicket.core.request.handler.ComponentNotFoundException)1 WicketTag (org.apache.wicket.markup.WicketTag)1 AutoLabel (org.apache.wicket.markup.html.form.AutoLabelResolver.AutoLabel)1