Search in sources :

Example 1 with Container

use of com.github.bordertech.wcomponents.Container in project wcomponents by BorderTech.

the class VelocityRenderer method fillContext.

/**
 * Fills the given velocity context with data from the component which is being rendered. A map of components is
 * also built up, in order to support deferred rendering.
 *
 * @param component the current component being rendered.
 * @param context the velocity context to modify.
 * @param componentsByKey a map to store components for deferred rendering.
 */
private void fillContext(final WComponent component, final VelocityContext context, final Map<String, WComponent> componentsByKey) {
    // Also make the component available under the "this" key.
    context.put("this", component);
    // Make the UIContext available under the "uicontext" key.
    UIContext uic = UIContextHolder.getCurrent();
    context.put("uicontext", uic);
    context.put("uic", uic);
    if (component instanceof VelocityProperties) {
        Map<?, ?> map = ((VelocityProperties) component).getVelocityMap();
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            String key = (String) entry.getKey();
            Object value = entry.getValue();
            context.put(key, value);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Handling children");
    }
    // As well as going into their own named slots, visible children are also
    // placed into a list called children
    ArrayList<String> children = new ArrayList<>();
    if (component instanceof Container) {
        Container container = (Container) component;
        for (int i = 0; i < container.getChildCount(); i++) {
            WComponent child = container.getChildAt(i);
            String tag = child.getTag();
            if (tag != null || child.isVisible()) {
                // The key needs to be something which would never be output by a Velocity template.
                String key = "<VelocityLayout" + child.getId() + "/>";
                componentsByKey.put(key, child);
                if (tag != null) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Adding child " + tag + " to context");
                    }
                    addToContext(context, tag, key);
                }
                if (child.isVisible()) {
                    children.add(key);
                }
            }
        }
        context.put("children", children);
    }
    // Put the context in the context
    context.put("context", context);
}
Also used : AbstractWComponent(com.github.bordertech.wcomponents.AbstractWComponent) WComponent(com.github.bordertech.wcomponents.WComponent) Container(com.github.bordertech.wcomponents.Container) UIContext(com.github.bordertech.wcomponents.UIContext) ArrayList(java.util.ArrayList) VelocityProperties(com.github.bordertech.wcomponents.velocity.VelocityProperties) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Container

use of com.github.bordertech.wcomponents.Container in project wcomponents by BorderTech.

the class DebugStructureInterceptor method writeDebugInfo.

/**
 * Writes debugging information for the given component.
 *
 * @param component the component to write debugging information for.
 * @param xml the writer to send the debug output to.
 */
protected void writeDebugInfo(final WComponent component, final XmlStringBuilder xml) {
    if (component != null && (component.isVisible() || component instanceof WInvisibleContainer)) {
        xml.appendTagOpen("ui:debugInfo");
        xml.appendAttribute("for", component.getId());
        xml.appendAttribute("class", component.getClass().getName());
        xml.appendOptionalAttribute("type", getType(component));
        xml.appendClose();
        xml.appendTagOpen("ui:debugDetail");
        xml.appendAttribute("key", "defaultState");
        xml.appendAttribute("value", component.isDefaultState());
        xml.appendEnd();
        xml.appendEndTag("ui:debugInfo");
        if (component instanceof WRepeater) {
            // special case for WRepeaters - we must paint the info for each row.
            WRepeater repeater = (WRepeater) component;
            List<UIContext> contexts = repeater.getRowContexts();
            for (int i = 0; i < contexts.size(); i++) {
                UIContextHolder.pushContext(contexts.get(i));
                try {
                    writeDebugInfo(repeater.getRepeatedComponent(i), xml);
                } finally {
                    UIContextHolder.popContext();
                }
            }
        } else if (component instanceof WCardManager) {
            writeDebugInfo(((WCardManager) component).getVisible(), xml);
        } else if (component instanceof Container) {
            final int size = ((Container) component).getChildCount();
            for (int i = 0; i < size; i++) {
                writeDebugInfo(((Container) component).getChildAt(i), xml);
            }
        }
    }
}
Also used : Container(com.github.bordertech.wcomponents.Container) WInvisibleContainer(com.github.bordertech.wcomponents.WInvisibleContainer) UIContext(com.github.bordertech.wcomponents.UIContext) WCardManager(com.github.bordertech.wcomponents.WCardManager) WInvisibleContainer(com.github.bordertech.wcomponents.WInvisibleContainer) WRepeater(com.github.bordertech.wcomponents.WRepeater)

Example 3 with Container

use of com.github.bordertech.wcomponents.Container in project wcomponents by BorderTech.

the class UicStats method addStats.

/**
 * Recursively adds statistics for a component and its children to the stats map.
 *
 * @param statsMap the stats map to add to.
 * @param comp the component to analyse.
 */
private void addStats(final Map<WComponent, Stat> statsMap, final WComponent comp) {
    Stat stat = createStat(comp);
    statsMap.put(comp, stat);
    if (comp instanceof Container) {
        Container container = (Container) comp;
        int childCount = container.getChildCount();
        for (int i = 0; i < childCount; i++) {
            WComponent child = container.getChildAt(i);
            addStats(statsMap, child);
        }
    }
}
Also used : AbstractWComponent(com.github.bordertech.wcomponents.AbstractWComponent) WComponent(com.github.bordertech.wcomponents.WComponent) Container(com.github.bordertech.wcomponents.Container)

Example 4 with Container

use of com.github.bordertech.wcomponents.Container in project wcomponents by BorderTech.

the class TreeUtil method doTraverse.

/**
 * Internal implementation of tree traversal method.
 *
 * @param node the node to traverse.
 * @param visibleOnly if true, only visit visible components.
 * @param visitor the visitor to notify as the tree is traversed.
 * @return how the traversal should continue.
 */
private static VisitorResult doTraverse(final WComponent node, final boolean visibleOnly, final WComponentTreeVisitor visitor) {
    if (visibleOnly) {
        // Certain components have their visibility altered to implement custom processing.
        if (node instanceof WInvisibleContainer) {
            WComponent parent = node.getParent();
            // If inside a CardManager, skip the InvisibleContainer and process the visible card.
            if (parent instanceof WCardManager) {
                WComponent visible = ((WCardManager) node.getParent()).getVisible();
                if (visible == null) {
                    return VisitorResult.ABORT_BRANCH;
                }
                return doTraverse(visible, visibleOnly, visitor);
            } else if (parent instanceof WWindow) {
                // Abort branch if WWindow is not in ACTIVE state
                if (((WWindow) parent).getState() != WWindow.ACTIVE_STATE) {
                    return VisitorResult.ABORT_BRANCH;
                }
            }
        } else if (node instanceof WRepeatRoot) {
        // Let be processed.
        } else if (!node.isVisible()) {
            // For most components, we just need to see if they're marked as visible
            return VisitorResult.ABORT_BRANCH;
        }
    }
    VisitorResult result = visitor.visit(node);
    switch(result) {
        case ABORT_BRANCH:
            // Continue processing, but not down this branch
            return VisitorResult.CONTINUE;
        case CONTINUE:
            // Process repeater rows
            if (node instanceof WRepeater) {
                // Get parent repeater
                WRepeater repeater = (WRepeater) node;
                // Get row contexts
                List<UIContext> rowContextList = repeater.getRowContexts();
                WRepeatRoot repeatRoot = (WRepeatRoot) repeater.getRepeatedComponent().getParent();
                for (UIContext rowContext : rowContextList) {
                    UIContextHolder.pushContext(rowContext);
                    try {
                        result = doTraverse(repeatRoot, visibleOnly, visitor);
                    } finally {
                        UIContextHolder.popContext();
                    }
                    if (VisitorResult.ABORT.equals(result)) {
                        return VisitorResult.ABORT;
                    }
                }
            } else if (node instanceof Container) {
                Container container = (Container) node;
                for (int i = 0; i < container.getChildCount(); i++) {
                    result = doTraverse(container.getChildAt(i), visibleOnly, visitor);
                    if (VisitorResult.ABORT.equals(result)) {
                        return VisitorResult.ABORT;
                    }
                }
            }
            return VisitorResult.CONTINUE;
        default:
            // Abort entire traversal
            return VisitorResult.ABORT;
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) VisitorResult(com.github.bordertech.wcomponents.util.WComponentTreeVisitor.VisitorResult) Container(com.github.bordertech.wcomponents.Container) WInvisibleContainer(com.github.bordertech.wcomponents.WInvisibleContainer) WRepeatRoot(com.github.bordertech.wcomponents.WRepeater.WRepeatRoot) UIContext(com.github.bordertech.wcomponents.UIContext) WCardManager(com.github.bordertech.wcomponents.WCardManager) WWindow(com.github.bordertech.wcomponents.WWindow) WInvisibleContainer(com.github.bordertech.wcomponents.WInvisibleContainer) WRepeater(com.github.bordertech.wcomponents.WRepeater)

Aggregations

Container (com.github.bordertech.wcomponents.Container)4 UIContext (com.github.bordertech.wcomponents.UIContext)3 WComponent (com.github.bordertech.wcomponents.WComponent)3 AbstractWComponent (com.github.bordertech.wcomponents.AbstractWComponent)2 WCardManager (com.github.bordertech.wcomponents.WCardManager)2 WInvisibleContainer (com.github.bordertech.wcomponents.WInvisibleContainer)2 WRepeater (com.github.bordertech.wcomponents.WRepeater)2 WRepeatRoot (com.github.bordertech.wcomponents.WRepeater.WRepeatRoot)1 WWindow (com.github.bordertech.wcomponents.WWindow)1 VisitorResult (com.github.bordertech.wcomponents.util.WComponentTreeVisitor.VisitorResult)1 VelocityProperties (com.github.bordertech.wcomponents.velocity.VelocityProperties)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1