Example usage for org.apache.wicket Component remove

List of usage examples for org.apache.wicket Component remove

Introduction

In this page you can find the example usage for org.apache.wicket Component remove.

Prototype

public final void remove() 

Source Link

Document

Removes this component from its parent.

Usage

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

License:Open Source License

/** 
 * <p>//  w w  w. j a  v  a2 s  .com
 * 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();
    }

}