Search in sources :

Example 1 with InlineEnclosure

use of org.apache.wicket.markup.html.internal.InlineEnclosure in project wicket by apache.

the class InlineEnclosureHandler method onComponentTag.

@Override
protected MarkupElement onComponentTag(final ComponentTag tag) throws ParseException {
    // We only need ComponentTags
    if (tag instanceof WicketTag) {
        return tag;
    }
    // Has wicket:enclosure attribute?
    String enclosureAttr = getAttribute(tag, null);
    if (enclosureAttr != null) {
        if (tag.isOpen()) {
            // Make sure 'wicket:id' and 'id' are consistent
            String htmlId = tag.getAttribute("id");
            if ((tag.getId() != null) && !Strings.isEmpty(htmlId) && !htmlId.equals(tag.getId())) {
                throw new ParseException("Make sure that 'id' and 'wicket:id' are the same if both are provided. Tag:" + tag.toString(), tag.getPos());
            }
            // if it doesn't have a wicket-id already, then assign one now.
            if (Strings.isEmpty(tag.getId())) {
                if (Strings.isEmpty(htmlId)) {
                    String id = getWicketNamespace() + "_" + INLINE_ENCLOSURE_ID_PREFIX + getRequestUniqueId();
                    tag.setId(id);
                } else {
                    tag.setId(htmlId);
                }
                tag.setAutoComponentTag(true);
                tag.setAutoComponentFactory(new ComponentTag.IAutoComponentFactory() {

                    @Override
                    public Component newComponent(MarkupContainer container, ComponentTag tag) {
                        String attributeName = getInlineEnclosureAttributeName(null);
                        String childId = tag.getAttribute(attributeName);
                        return new InlineEnclosure(tag.getId(), childId);
                    }
                });
                tag.setModified(true);
            }
            // Put the enclosure on the stack. The most current one will be on top
            if (enclosures == null) {
                enclosures = new ArrayDeque<>();
            }
            enclosures.push(tag);
        } else {
            throw new ParseException("Open-close tags don't make sense for InlineEnclosure. Tag:" + tag.toString(), tag.getPos());
        }
    } else // Are we within an enclosure?
    if ((enclosures != null) && (enclosures.size() > 0)) {
        // first ComponentTag's id found as the controlling child to the enclosure.
        if (tag.isOpen() && (tag.getId() != null) && !(tag instanceof WicketTag) && !tag.isAutoComponentTag()) {
            Iterator<ComponentTag> componentTagIterator = enclosures.descendingIterator();
            while (componentTagIterator.hasNext()) {
                ComponentTag lastEnclosure = componentTagIterator.next();
                String attr = getAttribute(lastEnclosure, null);
                if (Strings.isEmpty(attr) == true) {
                    lastEnclosure.getAttributes().put(getInlineEnclosureAttributeName(null), tag.getId());
                    lastEnclosure.setModified(true);
                }
            }
        } else if (tag.isClose() && tag.closes(enclosures.peek())) {
            ComponentTag lastEnclosure = enclosures.pop();
            String attr = getAttribute(lastEnclosure, null);
            if (Strings.isEmpty(attr) == true) {
                throw new ParseException("Did not find any child for InlineEnclosure. Tag:" + lastEnclosure.toString(), tag.getPos());
            }
        }
    }
    return tag;
}
Also used : WicketTag(org.apache.wicket.markup.WicketTag) InlineEnclosure(org.apache.wicket.markup.html.internal.InlineEnclosure) MarkupContainer(org.apache.wicket.MarkupContainer) ComponentTag(org.apache.wicket.markup.ComponentTag) Iterator(java.util.Iterator) ParseException(java.text.ParseException) Component(org.apache.wicket.Component)

Aggregations

ParseException (java.text.ParseException)1 Iterator (java.util.Iterator)1 Component (org.apache.wicket.Component)1 MarkupContainer (org.apache.wicket.MarkupContainer)1 ComponentTag (org.apache.wicket.markup.ComponentTag)1 WicketTag (org.apache.wicket.markup.WicketTag)1 InlineEnclosure (org.apache.wicket.markup.html.internal.InlineEnclosure)1