Search in sources :

Example 6 with DebugSettings

use of org.apache.wicket.settings.DebugSettings in project wicket by apache.

the class Page method checkRendering.

/**
 * Throw an exception if not all components rendered.
 *
 * @param renderedContainer
 *            The page itself if it was a full page render or the container that was rendered
 *            standalone
 */
private void checkRendering(final MarkupContainer renderedContainer) {
    // If the application wants component uses checked and
    // the response is not a redirect
    final DebugSettings debugSettings = getApplication().getDebugSettings();
    if (debugSettings.getComponentUseCheck()) {
        final List<Component> unrenderedComponents = new ArrayList<Component>();
        final StringBuilder buffer = new StringBuilder();
        renderedContainer.visitChildren(new IVisitor<Component, Void>() {

            @Override
            public void component(final Component component, final IVisit<Void> visit) {
                // If component never rendered
                if (renderedComponents == null || !renderedComponents.contains(component)) {
                    // If not an auto component ...
                    if (!component.isAuto() && component.isVisibleInHierarchy()) {
                        // Increase number of unrendered components
                        unrenderedComponents.add(component);
                        // Add to explanatory string to buffer
                        buffer.append(Integer.toString(unrenderedComponents.size())).append(". ").append(component.toString(true)).append('\n');
                        String metadata = component.getMetaData(Component.CONSTRUCTED_AT_KEY);
                        if (metadata != null) {
                            buffer.append(metadata).append('\n');
                        }
                        metadata = component.getMetaData(Component.ADDED_AT_KEY);
                        if (metadata != null) {
                            buffer.append(metadata).append('\n');
                        }
                    } else {
                        // if the component is not visible in hierarchy we
                        // should not visit its children since they are also
                        // not visible
                        visit.dontGoDeeper();
                    }
                }
            }
        });
        // Throw exception if any errors were found
        if (unrenderedComponents.size() > 0) {
            renderedComponents = null;
            List<Component> transparentContainerChildren = Generics.newArrayList();
            Iterator<Component> iterator = unrenderedComponents.iterator();
            outerWhile: while (iterator.hasNext()) {
                Component component = iterator.next();
                // ignore it.
                for (Component transparentContainerChild : transparentContainerChildren) {
                    MarkupContainer parent = component.getParent();
                    while (parent != null) {
                        if (parent == transparentContainerChild) {
                            iterator.remove();
                            continue outerWhile;
                        }
                        parent = parent.getParent();
                    }
                }
                if (hasInvisibleTransparentChild(component.getParent(), component)) {
                    // component and only do a debug statement here.
                    if (log.isDebugEnabled()) {
                        log.debug("Component {} wasn't rendered but might have a transparent parent.", component);
                    }
                    transparentContainerChildren.add(component);
                    iterator.remove();
                    continue outerWhile;
                }
            }
            // if still > 0
            if (unrenderedComponents.size() > 0) {
                // Throw exception
                throw new WicketRuntimeException("The component(s) below failed to render. Possible reasons could be that:\n\t1) you have added a component in code but forgot to reference it in the markup (thus the component will never be rendered),\n\t2) if your components were added in a parent container then make sure the markup for the child container includes them in <wicket:extend>.\n\n" + buffer.toString());
            }
        }
    }
    // Get rid of set
    renderedComponents = null;
}
Also used : ArrayList(java.util.ArrayList) DebugSettings(org.apache.wicket.settings.DebugSettings)

Aggregations

DebugSettings (org.apache.wicket.settings.DebugSettings)6 Map (java.util.Map)2 IConverter (org.apache.wicket.util.convert.IConverter)2 IValueMap (org.apache.wicket.util.value.IValueMap)2 ArrayList (java.util.ArrayList)1 MarkupException (org.apache.wicket.markup.MarkupException)1 ResourceReference (org.apache.wicket.request.resource.ResourceReference)1 JavaScriptLibrarySettings (org.apache.wicket.settings.JavaScriptLibrarySettings)1