Search in sources :

Example 1 with TemplateContext

use of org.apache.myfaces.view.facelets.TemplateContext in project myfaces by apache.

the class UserTagHandler method apply.

@Override
public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException, FaceletException, ELException {
    if (name != null) {
        if (this._handlers == null) {
            return false;
        }
        DefineHandler handler = (DefineHandler) this._handlers.get(name);
        if (handler != null) {
            AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
            TemplateContext itc = actx.popTemplateContext();
            try {
                handler.applyDefinition(ctx, parent);
            } finally {
                actx.pushTemplateContext(itc);
            }
            return true;
        } else {
            return false;
        }
    } else {
        AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
        TemplateContext itc = actx.popTemplateContext();
        try {
            this.nextHandler.apply(ctx, parent);
        } finally {
            actx.pushTemplateContext(itc);
        }
        return true;
    }
}
Also used : DefineHandler(org.apache.myfaces.view.facelets.tag.ui.DefineHandler) TemplateContext(org.apache.myfaces.view.facelets.TemplateContext) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext)

Example 2 with TemplateContext

use of org.apache.myfaces.view.facelets.TemplateContext in project myfaces by apache.

the class DefaultFaceletContext method pushCompositeComponentClient.

@Override
public void pushCompositeComponentClient(final TemplateClient client) {
    TemplateContext itc = new TemplateContextImpl();
    itc.setCompositeComponentClient(new CompositeComponentTemplateManager(this._facelet, client, getPageContext()));
    _isolatedTemplateContext.add(itc);
    _currentTemplateContext++;
    _defaultVarMapper.setTemplateContext(itc);
}
Also used : TemplateContext(org.apache.myfaces.view.facelets.TemplateContext)

Example 3 with TemplateContext

use of org.apache.myfaces.view.facelets.TemplateContext in project myfaces by apache.

the class CompositeComponentResourceTagHandler method apply.

@Override
public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException, FaceletException, ELException {
    if (_dynamicCompositeComponent) {
        AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
        FaceletCompositionContext fcc = actx.getFaceletCompositionContext();
        UIComponent innerCompositeComponent = fcc.getCompositeComponentFromStack();
        // In a programatical addition, the code that process the composite component only takes effect
        // when the composite component is added to the view.
        Integer step = (Integer) innerCompositeComponent.getAttributes().get(CREATE_CC_ON_POST_ADD_TO_VIEW);
        if (step != null && step == 1) {
            if (name != null) {
                // 1. Initialize map used to retrieve facets
                if (innerCompositeComponent.getFacetCount() == 0) {
                    checkFacetRequired(ctx, name);
                    return true;
                }
                UIComponent facet = innerCompositeComponent.getFacet(name);
                if (facet != null) {
                    // Insert facet
                    innerCompositeComponent.getFacets().remove(name);
                    parent.getFacets().put(name, facet);
                    return true;
                } else {
                    checkFacetRequired(ctx, name);
                    return true;
                }
            } else {
                if (innerCompositeComponent.getChildCount() > 0) {
                    String facetName = (String) parent.getAttributes().get(org.apache.myfaces.view.facelets.tag.faces.core.FacetHandler.KEY);
                    // Insert children
                    List<UIComponent> children = new ArrayList<>(innerCompositeComponent.getChildCount());
                    while (innerCompositeComponent.getChildCount() > 0) {
                        children.add(innerCompositeComponent.getChildren().remove(0));
                    }
                    while (children.size() > 0) {
                        UIComponent child = children.remove(0);
                        child.getAttributes().put(InsertChildrenHandler.INSERT_CHILDREN_USED, Boolean.TRUE);
                        if (facetName != null) {
                            ComponentSupport.addFacet(ctx, parent, child, facetName);
                        } else {
                            parent.getChildren().add(child);
                        }
                    }
                }
                return true;
            }
        } else if (step != null && step > 1) {
            // is no relative order (it is always on the same spot).
            if (name == null) {
                String facetName = (String) parent.getAttributes().get(org.apache.myfaces.view.facelets.tag.faces.core.FacetHandler.KEY);
                // refresh case, remember the inserted children does not have any
                // associated tag handler, so in this case we just need to remove and add them in the same order
                // we found them
                List<UIComponent> children = null;
                if (facetName == null) {
                    children = new ArrayList<>(parent.getChildCount());
                    int i = 0;
                    while (parent.getChildCount() - i > 0) {
                        UIComponent child = parent.getChildren().get(i);
                        if (Boolean.TRUE.equals(child.getAttributes().get(InsertChildrenHandler.INSERT_CHILDREN_USED))) {
                            children.add(parent.getChildren().remove(i));
                        } else {
                            i++;
                        }
                    }
                } else {
                    children = new ArrayList<UIComponent>();
                    UIComponent child = parent.getFacet(facetName);
                    if (Boolean.TRUE.equals(child.getAttributes().get(InsertChildrenHandler.INSERT_CHILDREN_USED))) {
                        parent.getFacets().remove(facetName);
                        children.add(child);
                    } else {
                        UIComponent parentToApply = child;
                        int i = 0;
                        while (parentToApply.getChildCount() - i > 0) {
                            child = parentToApply.getChildren().get(i);
                            if (Boolean.TRUE.equals(child.getAttributes().get(InsertChildrenHandler.INSERT_CHILDREN_USED))) {
                                children.add(parentToApply.getChildren().remove(i));
                            } else {
                                i++;
                            }
                        }
                    }
                }
                while (children.size() > 0) {
                    UIComponent child = children.remove(0);
                    if (facetName != null) {
                        ComponentSupport.addFacet(ctx, parent, child, facetName);
                    } else {
                        parent.getChildren().add(child);
                    }
                }
            }
        }
        return true;
    }
    if (name != null) {
        // 1. Initialize map used to retrieve facets
        if (_facetHandlers == null || _facetHandlers.isEmpty()) {
            checkFacetRequired(ctx, name);
            return true;
        }
        initFacetHandlersMap(ctx);
        FaceletHandler handler = _facetHandlersMap.get(name);
        if (handler != null) {
            AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
            // Pop the current composite component on stack, so #{cc} references
            // can be resolved correctly, because they are relative to the page
            // that define it.
            FaceletCompositionContext fcc = actx.getFaceletCompositionContext();
            UIComponent innerCompositeComponent = fcc.getCompositeComponentFromStack();
            fcc.popCompositeComponentToStack();
            // Pop the template context, so ui:xx tags and nested composite component
            // cases could work correctly
            TemplateContext itc = actx.popTemplateContext();
            try {
                handler.apply(ctx, parent);
            } finally {
                actx.pushTemplateContext(itc);
                fcc.pushCompositeComponentToStack(innerCompositeComponent);
            }
            return true;
        } else {
            checkFacetRequired(ctx, name);
            return true;
        }
    } else {
        AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
        // Pop the current composite component on stack, so #{cc} references
        // can be resolved correctly, because they are relative to the page
        // that define it.
        FaceletCompositionContext fcc = actx.getFaceletCompositionContext();
        UIComponent innerCompositeComponent = fcc.getCompositeComponentFromStack();
        fcc.popCompositeComponentToStack();
        // Pop the template context, so ui:xx tags and nested composite component
        // cases could work correctly
        TemplateContext itc = actx.popTemplateContext();
        try {
            for (int i = 0; i < _componentHandlers.size(); i++) {
                _componentHandlers.get(i).apply(ctx, parent);
            }
        } finally {
            actx.pushTemplateContext(itc);
            fcc.pushCompositeComponentToStack(innerCompositeComponent);
        }
        return true;
    }
}
Also used : FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) FaceletHandler(jakarta.faces.view.facelets.FaceletHandler) UIComponent(jakarta.faces.component.UIComponent) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) TemplateContext(org.apache.myfaces.view.facelets.TemplateContext) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext)

Example 4 with TemplateContext

use of org.apache.myfaces.view.facelets.TemplateContext in project myfaces by apache.

the class DefaultFaceletContext method popTemplateContext.

@Override
public TemplateContext popTemplateContext() {
    if (_currentTemplateContext > 0) {
        TemplateContext itc = _isolatedTemplateContext.get(_currentTemplateContext);
        _isolatedTemplateContext.remove(_currentTemplateContext);
        _currentTemplateContext--;
        _defaultVarMapper.setTemplateContext(_isolatedTemplateContext.get(_currentTemplateContext));
        return itc;
    }
    return null;
}
Also used : TemplateContext(org.apache.myfaces.view.facelets.TemplateContext)

Aggregations

TemplateContext (org.apache.myfaces.view.facelets.TemplateContext)4 AbstractFaceletContext (org.apache.myfaces.view.facelets.AbstractFaceletContext)2 UIComponent (jakarta.faces.component.UIComponent)1 FaceletHandler (jakarta.faces.view.facelets.FaceletHandler)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 FaceletCompositionContext (org.apache.myfaces.view.facelets.FaceletCompositionContext)1 DefineHandler (org.apache.myfaces.view.facelets.tag.ui.DefineHandler)1