Search in sources :

Example 1 with ComponentNotFoundException

use of org.apache.wicket.core.request.handler.ComponentNotFoundException in project wicket by apache.

the class AutoLabelResolver method resolve.

@Override
public Component resolve(final MarkupContainer container, final MarkupStream markupStream, final ComponentTag tag) {
    if (!tag.getId().startsWith(LABEL_ATTR)) {
        return null;
    }
    final String id = tag.getAttribute(getWicketNamespace(markupStream) + WICKET_FOR).trim();
    Component component = findRelatedComponent(container, id);
    if (component == null) {
        throw new ComponentNotFoundException("Could not find form component with id '" + id + "' while trying to resolve wicket:for attribute");
    }
    if (!(component instanceof ILabelProvider)) {
        throw new WicketRuntimeException("Component pointed to by wicket:for attribute '" + id + "' does not implement " + ILabelProvider.class.getName());
    }
    if (!component.getOutputMarkupId()) {
        component.setOutputMarkupId(true);
        if (component.hasBeenRendered()) {
            logger.warn("Component: {} is referenced via a wicket:for attribute but does not have its outputMarkupId property set to true", component.toString(false));
        }
    }
    if (component instanceof FormComponent) {
        component.setMetaData(MARKER_KEY, new AutoLabelMarker((FormComponent<?>) component));
    }
    return new AutoLabel(tag.getId(), component);
}
Also used : ComponentNotFoundException(org.apache.wicket.core.request.handler.ComponentNotFoundException) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) Component(org.apache.wicket.Component)

Example 2 with ComponentNotFoundException

use of org.apache.wicket.core.request.handler.ComponentNotFoundException 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)2 ComponentNotFoundException (org.apache.wicket.core.request.handler.ComponentNotFoundException)2 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 WicketTag (org.apache.wicket.markup.WicketTag)1 AutoLabel (org.apache.wicket.markup.html.form.AutoLabelResolver.AutoLabel)1