Example usage for org.apache.wicket MarkupContainer getId

List of usage examples for org.apache.wicket MarkupContainer getId

Introduction

In this page you can find the example usage for org.apache.wicket MarkupContainer getId.

Prototype

@Override
public String getId() 

Source Link

Document

Gets the id of this component.

Usage

From source file:org.cast.cwm.xml.component.XmlComponent.java

License:Open Source License

/** 
 * <p>//  w  w  w.  j  av  a 2  s.  c o m
 * Find all elements in this component's markup that have a wicket:id, and attach a dynamic component for them.
 * This supports nested Wicket Components.  A container can add components to itself elsewhere and those components 
 * will not be overridden/replaced by this method.  
 * 
 */
protected void addDynamicComponents() {
    TransformResult transformResult = xmlService.getTransformed(getModel(), transformName, transformParameters);
    Element dom = transformResult.getElement();
    NodeList componentNodes = xmlService.getWicketNodes(dom, true);
    Map<Element, Component> componentMap = new HashMap<Element, Component>(); // Mapping of Nodes to Components; used for nesting
    final Set<String> wicketIds = new HashSet<String>();

    for (int i = 0; i < componentNodes.getLength(); i++) {

        Element e = (Element) componentNodes.item(i);

        String id = e.getAttributeNS(WICKET_NS, "id");

        // Traverse up through this node's parents.  If a parent
        // maps to a wicket component, then that must be a MarkupContainer
        // for the current component.
        MarkupContainer container = null;
        Node parent = e.getParentNode();
        while (container == null && parent != null) {
            container = (MarkupContainer) componentMap.get(parent);
            parent = parent.getParentNode();
        }

        // Check to see if a component is a direct child and if
        // one already exists with that id. If so, no need to 
        // regenerate, but mark that it's valid.
        if (updateable && container == null && get(id) != null) {
            wicketIds.add(id);
            continue;
        }

        // If no container, add directly to the document. If container exists, add 
        // the component only if one does not already exist with that wicket:id.  This allows
        // panels, subclasses, etc to add components to this markup and not be overridden
        // or duplicated.
        if (container == null) {
            log.trace("Adding Dynamic Component to Root Panel {}: {}.", id, e);
            wicketIds.add(id); // Valid child, for removing stale children later.
            Component c = getDynamicComponent(id, e);
            add(c);
            componentMap.put(e, c);

        } else {
            Component c = container.get(id);
            if (c == null) {
                c = getDynamicComponent(id, e);
                log.trace("Adding Dynamic Component ({}) to Container ({}).", id, container.getId());
                container.add(c);
                componentMap.put(e, c);
            } else {
                componentMap.put(e, c);
            }
        }
    }

    // Remove any stale components that might have been added during previous renders.
    List<Component> removeList = new ArrayList<Component>(); // To be removed
    if (updateable) {
        for (int i = 0; i < size(); i++) {
            if (!wicketIds.contains(get(i).getId()))
                removeList.add(get(i));
        }
        for (Component c : removeList)
            c.remove();
    }

}

From source file:org.opensingular.lib.wicket.util.bootstrap.layout.BSDropDownMenu.java

License:Apache License

public BSDropDownMenu appendLink(Component label, MarkupContainer link) {
    return appendTag("li", true, null, itemId -> new TemplatePanel(itemId,
            () -> "<a wicket:id='" + link.getId() + "'><span wicket:id='" + label.getId() + "'></span></a>")
                    .add(link.add(label)).setRenderBodyOnly(false));
}

From source file:org.wicketstuff.security.components.markup.html.links.SecureContainerLink.java

License:Apache License

/**
 * Performs the replacement, only if an actual replacement was constructed.
 * /*  ww w.  j av a  2s. c o m*/
 * @see org.apache.wicket.markup.html.link.Link#onClick()
 * @see #getReplacementFor(Component, String, Class)
 * @throws WicketRuntimeException
 *             if a problem occurs in replacing the container.
 */
@Override
public final void onClick() {
    Component replaceMe = getComponentToBeReplaced();
    if (replaceMe == null)
        throw new WicketRuntimeException(
                "unable to find child with id: " + containerId + " on parent: " + containerParent);
    Class<? extends MarkupContainer> myReplacementClass = getReplacementClass();
    MarkupContainer replacement = getReplacementFor(replaceMe, containerId, myReplacementClass);
    if (replacement == null)
        return; // do nothing
    if (!containerId.equals(replacement.getId()))
        throw new WicketRuntimeException("The replacement does not have the specified id: " + containerId
                + ", but id: " + replacement.getId());
    if (myReplacementClass.isAssignableFrom(replacement.getClass()))
        containerParent.replace(replacement);
    else
        throw new WicketRuntimeException("The replacement for " + containerId + " on " + containerParent
                + " is not assignable from " + myReplacementClass);

}