Search in sources :

Example 1 with FaceletCompositionContext

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

the class CompositeComponentResourceTagHandler method applyNextHandler.

@SuppressWarnings("unchecked")
@Override
public void applyNextHandler(FaceletContext ctx, UIComponent c) throws IOException {
    FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(ctx);
    // when the composite component is added to the view.
    if (mctx.isDynamicCompositeComponentHandler()) {
        _dynamicCompositeComponent = true;
        try {
            mctx.setDynamicCompositeComponentHandler(false);
            // If the composite component needs to be created dynamically
            // 
            Integer step = (Integer) c.getAttributes().get(CREATE_CC_ON_POST_ADD_TO_VIEW);
            if (step == null) {
                // The flag is not found, so we are creating the component right now.
                // Add the flag and return.
                c.getAttributes().put(CREATE_CC_ON_POST_ADD_TO_VIEW, 0);
            } else if (step == 0) {
            // Should not happen, stop processing
            } else if (step == 1) {
                // The component was created, and the listener attached to PostAddToViewEvent
                // is executing right now. Do the necessary steps to process the
                // composite component dynamically.
                applyNextHandlerIfNotAppliedDynamically(ctx, c);
                applyCompositeComponentFacelet(ctx, c);
                applyFinalInitializationSteps(ctx, mctx, c);
                c.getAttributes().put(CREATE_CC_ON_POST_ADD_TO_VIEW, 2);
            } else {
                // Refresh over dynamic composite component
                applyCompositeComponentFacelet(ctx, c);
            }
        } finally {
            mctx.setDynamicCompositeComponentHandler(true);
        }
    } else {
        applyNextHandlerIfNotApplied(ctx, c);
        applyCompositeComponentFacelet(ctx, c);
        if (ComponentHandler.isNew(c)) {
            applyFinalInitializationSteps(ctx, mctx, c);
        }
    }
}
Also used : FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext)

Example 2 with FaceletCompositionContext

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

the class CreateDynamicCompositeComponentListener method processEvent.

@Override
public void processEvent(ComponentSystemEvent event) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    FaceletViewDeclarationLanguage vdl = (FaceletViewDeclarationLanguage) facesContext.getApplication().getViewHandler().getViewDeclarationLanguage(facesContext, facesContext.getViewRoot().getViewId());
    Facelet componentFacelet;
    FaceletFactory faceletFactory = vdl.getFaceletFactory();
    FaceletFactory.setInstance(faceletFactory);
    try {
        componentFacelet = faceletFactory.compileComponentFacelet(taglibURI, tagName, attributes);
    } finally {
        FaceletFactory.setInstance(null);
    }
    UIComponent component = event.getComponent();
    // The execution of this listener activated another call to PostAddToViewEvent, because
    // ComponentTagHandlerDelegate removes and add the component again. This is necessary because
    // the inner components also require the propagation of PostAddToViewEvent to refresh themselves.
    // but this check avoids the duplicate call to the facelet, even if the duplicate call does not
    // have any side effect (counts as a refresh).
    Integer step = (Integer) component.getAttributes().get(CompositeComponentResourceTagHandler.CREATE_CC_ON_POST_ADD_TO_VIEW);
    if (step != null && step == 0) {
        component.getAttributes().put(CompositeComponentResourceTagHandler.CREATE_CC_ON_POST_ADD_TO_VIEW, 1);
    } else {
        return;
    }
    try {
        facesContext.getAttributes().put(FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD, Boolean.TRUE);
        // Detect the relationship between parent and child, to ensure the component is properly created
        // and refreshed. In facelets this is usually done by core.FacetHandler, but since it is a
        // dynamic component, we need to do it here before apply the handler
        UIComponent parent = component.getParent();
        String facetName = null;
        if (parent.getFacetCount() > 0 && !parent.getChildren().contains(component)) {
            facetName = ComponentSupport.findFacetNameByComponentInstance(parent, component);
        }
        try {
            if (facetName != null) {
                parent.getAttributes().put(org.apache.myfaces.view.facelets.tag.faces.core.FacetHandler.KEY, facetName);
            }
            // The trick here is restore MARK_CREATED, just to allow ComponentTagHandlerDelegate to
            // find the component. Then we reset it to exclude it from facelets refresh algorithm.
            String markId = (String) component.getAttributes().get(FaceletViewDeclarationLanguage.GEN_MARK_ID);
            if (markId == null) {
                ((AbstractFacelet) componentFacelet).applyDynamicComponentHandler(facesContext, component, baseKey);
            } else {
                try {
                    component.getAttributes().put(ComponentSupport.MARK_CREATED, markId);
                    ((AbstractFacelet) componentFacelet).applyDynamicComponentHandler(facesContext, component.getParent(), baseKey);
                } finally {
                    component.getAttributes().put(ComponentSupport.MARK_CREATED, null);
                }
            }
            if (FaceletViewDeclarationLanguageBase.isDynamicComponentNeedsRefresh(facesContext)) {
                FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(facesContext);
                if (fcc == null) {
                    FaceletViewDeclarationLanguageBase.activateDynamicComponentRefreshTransientBuild(facesContext);
                    FaceletViewDeclarationLanguageBase.resetDynamicComponentNeedsRefreshFlag(facesContext);
                    component.subscribeToEvent(DynamicComponentRefreshTransientBuildEvent.class, new RefreshDynamicComponentListener(taglibURI, tagName, attributes, baseKey));
                    component.getAttributes().put(DynamicComponentRefreshTransientBuildEvent.DYN_COMP_REFRESH_FLAG, Boolean.TRUE);
                } else {
                    component.subscribeToEvent(FaceletDynamicComponentRefreshTransientBuildEvent.class, new RefreshDynamicComponentListener(taglibURI, tagName, attributes, baseKey));
                }
            }
        } finally {
            if (facetName != null) {
                parent.getAttributes().remove(org.apache.myfaces.view.facelets.tag.faces.core.FacetHandler.KEY);
            }
        }
    } catch (IOException e) {
        throw new LocationAwareFacesException(e, component);
    } finally {
        facesContext.getAttributes().remove(FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD);
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) AbstractFacelet(org.apache.myfaces.view.facelets.AbstractFacelet) Facelet(jakarta.faces.view.facelets.Facelet) FaceletFactory(org.apache.myfaces.view.facelets.FaceletFactory) AbstractFacelet(org.apache.myfaces.view.facelets.AbstractFacelet) UIComponent(jakarta.faces.component.UIComponent) FaceletViewDeclarationLanguage(org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage) IOException(java.io.IOException) RefreshDynamicComponentListener(org.apache.myfaces.view.facelets.compiler.RefreshDynamicComponentListener) LocationAwareFacesException(org.apache.myfaces.view.facelets.LocationAwareFacesException)

Example 3 with FaceletCompositionContext

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

the class ComponentTagHandlerDelegate method apply.

/**
 * Method handles UIComponent tree creation in accordance with the JSF 1.2 spec.
 * <ol>
 * <li>First determines this UIComponent's id by calling {@link #getId(FaceletContext) getId(FaceletContext)}.</li>
 * <li>Search the parent for an existing UIComponent of the id we just grabbed</li>
 * <li>If found, {@link FaceletCompositionContext#markForDeletion(UIComponent) mark} its children for deletion.</li>
 * <li>If <i>not</i> found, call {@link #createComponent(FaceletContext) createComponent}.
 * <ol>
 * <li>Only here do we apply TagHandler#setAttributes(FaceletCompositionContext, Object)</li>
 * <li>Set the UIComponent's id</li>
 * <li>Set the RendererType of this instance</li>
 * </ol>
 * </li>
 * <li>Now apply the nextHandler, passing the UIComponent we've created/found.</li>
 * <li>Now add the UIComponent to the passed parent</li>
 * <li>Lastly, if the UIComponent already existed (found),
 * then #finalizeForDeletion(FaceletCompositionContext, UIComponent)
 * for deletion.</li>
 * </ol>
 *
 * See jakarta.faces.view.facelets.FaceletHandler#apply(jakarta.faces.view.facelets.FaceletContext,
 * jakarta.faces.component.UIComponent)
 *
 * @throws TagException
 *             if the UIComponent parent is null
 */
@SuppressWarnings("unchecked")
@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
    // make sure our parent is not null
    if (parent == null) {
        throw new TagException(_delegate.getTag(), "Parent UIComponent was null");
    }
    FacesContext facesContext = ctx.getFacesContext();
    // possible facet scoped
    String facetName = this.getFacetName(ctx, parent);
    // our id
    String id = ctx.generateUniqueId(_delegate.getTagId());
    // Cast to use UniqueIdVendor stuff
    FaceletCompositionContext mctx = (FaceletCompositionContext) FaceletCompositionContext.getCurrentInstance(ctx);
    // grab our component
    UIComponent c = null;
    // Used to preserve the original parent. Note when the view is being refreshed, the real parent could be
    // another component.
    UIComponent oldParent = parent;
    if (mctx.isRefreshingSection()) {
        if (_relocatableResourceHandler != null) {
            c = _relocatableResourceHandler.findChildByTagId(ctx, parent, id);
        } else {
            if (facetName != null) {
                c = ComponentSupport.findChildInFacetByTagId(parent, id, facetName);
            } else {
                c = ComponentSupport.findChildInChildrenByTagId(parent, id);
            }
        }
    }
    boolean componentFound = false;
    if (c != null) {
        componentFound = true;
        // preserve the same context
        if (_delegate.getBinding() != null && c.getAttributes().containsKey(FaceletDynamicComponentRefreshTransientBuildEvent.DYNAMIC_COMPONENT_BINDING_NEEDS_REFRESH)) {
            VisitContext visitContext = (VisitContext) mctx.getVisitContextFactory().getVisitContext(facesContext, null, MyFacesVisitHints.SET_SKIP_ITERATION);
            c.visitTree(visitContext, PublishFaceletDynamicComponentRefreshTransientBuildCallback.INSTANCE);
        }
        mctx.incrementUniqueComponentId();
        // mark all children for cleaning
        if (log.isLoggable(Level.FINE)) {
            log.fine(_delegate.getTag() + " Component[" + id + "] Found, marking children for cleanup");
        }
        // The call for mctx.markForDeletion(c) is always necessary, because
        // component resource relocation occur as an effect of PostAddToViewEvent,
        // so at this point it is unknown if the component was relocated or not.
        mctx.markForDeletion(c);
        if (_relocatableResourceHandler != null) {
            mctx.markRelocatableResourceForDeletion(c);
        }
    } else {
        c = this.createComponent(ctx);
        if (log.isLoggable(Level.FINE)) {
            log.fine(_delegate.getTag() + " Component[" + id + "] Created: " + c.getClass().getName());
        }
        _delegate.setAttributes(ctx, c);
        // mark it owned by a facelet instance
        c.getAttributes().put(ComponentSupport.MARK_CREATED, id);
        if (facesContext.isProjectStage(ProjectStage.Development)) {
            c.getAttributes().put(UIComponent.VIEW_LOCATION_KEY, _delegate.getTag().getLocation());
        }
        // assign our unique id
        if (this._id != null) {
            mctx.incrementUniqueComponentId();
            c.setId(this._id.getValue(ctx));
        } else {
            String componentId = mctx.generateUniqueComponentId();
            UniqueIdVendor uniqueIdVendor = mctx.getUniqueIdVendorFromStack();
            if (uniqueIdVendor == null) {
                uniqueIdVendor = facesContext.getViewRoot();
                if (uniqueIdVendor == null) {
                    // facesContext.getViewRoot() returns null here if we are in
                    // phase restore view, so we have to try to get the view root
                    // via the method in ComponentSupport and our parent
                    uniqueIdVendor = ComponentSupport.getViewRoot(ctx, parent);
                }
            }
            if (uniqueIdVendor != null) {
                // UIViewRoot implements UniqueIdVendor, so there is no need to cast to UIViewRoot
                // and call createUniqueId()
                String uid = uniqueIdVendor.createUniqueId(facesContext, componentId);
                c.setId(uid);
            }
        }
        if (this._rendererType != null) {
            c.setRendererType(this._rendererType);
        }
        // hook method
        _delegate.onComponentCreated(ctx, c, parent);
        if (_relocatableResourceHandler != null && _relocatableResourceHandler instanceof ComponentRelocatableResourceHandler) {
            UIComponent parentCompositeComponent = mctx.getCompositeComponentFromStack();
            if (parentCompositeComponent != null) {
                c.getAttributes().put(CompositeComponentELUtils.LOCATION_KEY, parentCompositeComponent.getAttributes().get(CompositeComponentELUtils.LOCATION_KEY));
            }
        }
        if (mctx.isRefreshingTransientBuild() && _relocatableResourceHandler != null) {
            mctx.markRelocatableResourceForDeletion(c);
        }
    }
    c.pushComponentToEL(facesContext, c);
    if (c instanceof UniqueIdVendor) {
        mctx.pushUniqueIdVendorToStack((UniqueIdVendor) c);
    }
    if (mctx.isDynamicComponentTopLevel()) {
        mctx.setDynamicComponentTopLevel(false);
        _delegate.applyNextHandler(ctx, c);
        mctx.setDynamicComponentTopLevel(true);
    } else {
        // first allow c to get populated
        _delegate.applyNextHandler(ctx, c);
    }
    boolean oldProcessingEvents = facesContext.isProcessingEvents();
    // finish cleaning up orphaned children
    if (componentFound && !mctx.isDynamicComponentTopLevel()) {
        mctx.finalizeForDeletion(c);
        if (mctx.isRefreshingSection()) {
            facesContext.setProcessingEvents(false);
            if (_relocatableResourceHandler != null && parent != null && !parent.equals(c.getParent())) {
                // Replace parent with the relocated parent.
                parent = c.getParent();
                // Since we changed the parent, the facetName becomes invalid, because it points
                // to the component before relocation. We need to find the right facetName (if any) so we can
                // refresh the component properly.
                UIComponent c1 = ComponentSupport.findChildInChildrenByTagId(parent, id);
                if (c1 == null) {
                    facetName = ComponentSupport.findChildInFacetsByTagId(parent, id);
                } else {
                    facetName = null;
                }
            }
            ComponentSupport.setCachedFacesContext(c, facesContext);
        }
        if (facetName == null) {
            parent.getChildren().remove(c);
        } else {
            ComponentSupport.removeFacet(ctx, parent, c, facetName);
        }
        if (mctx.isRefreshingSection()) {
            ComponentSupport.setCachedFacesContext(c, null);
            facesContext.setProcessingEvents(oldProcessingEvents);
        }
    }
    if (!componentFound) {
        if (c instanceof ClientBehaviorHolder && !UIComponent.isCompositeComponent(c)) {
            Iterator<AjaxHandler> it = ((AbstractFaceletContext) ctx).getAjaxHandlers();
            if (it != null) {
                while (it.hasNext()) {
                    it.next().applyAttachedObject(facesContext, c);
                }
            }
        }
        if (c instanceof EditableValueHolder) {
            // add default validators here, because this feature
            // is only available in facelets (see MYFACES-2362 for details)
            addEnclosingAndDefaultValidators(ctx, mctx, facesContext, (EditableValueHolder) c);
        }
    }
    _delegate.onComponentPopulated(ctx, c, oldParent);
    if (!mctx.isDynamicComponentTopLevel() || !componentFound) {
        if (componentFound && mctx.isRefreshingSection()) {
            facesContext.setProcessingEvents(false);
            ComponentSupport.setCachedFacesContext(c, facesContext);
        }
        if (facetName == null) {
            parent.getChildren().add(c);
        } else {
            ComponentSupport.addFacet(ctx, parent, c, facetName);
        }
        if (componentFound && mctx.isRefreshingSection()) {
            ComponentSupport.setCachedFacesContext(c, null);
            facesContext.setProcessingEvents(oldProcessingEvents);
        }
    }
    if (c instanceof UniqueIdVendor) {
        mctx.popUniqueIdVendorToStack();
    }
    c.popComponentFromEL(facesContext);
    if (mctx.isMarkInitialState()) {
        // Call it only if we are using partial state saving
        c.markInitialState();
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) VisitContext(jakarta.faces.component.visit.VisitContext) UIComponent(jakarta.faces.component.UIComponent) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder) UniqueIdVendor(jakarta.faces.component.UniqueIdVendor) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext) AjaxHandler(org.apache.myfaces.view.facelets.tag.faces.core.AjaxHandler) TagException(jakarta.faces.view.facelets.TagException) EditableValueHolder(jakarta.faces.component.EditableValueHolder)

Example 4 with FaceletCompositionContext

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

the class CompositionHandler method apply.

/*
     * (non-Javadoc)
     * 
     * @see jakarta.faces.view.facelets.FaceletHandler#apply(jakarta.faces.view.facelets.FaceletContext,
     * jakarta.faces.component.UIComponent)
     */
@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, ELException {
    if (_template != null) {
        AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
        FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
        actx.extendClient(this);
        if (_params != null) {
            String uniqueId = fcc.generateUniqueComponentId();
            for (int i = 0; i < _params.length; i++) {
                _params[i].apply(ctx, parent, _params[i].getName(ctx), _params[i].getValue(ctx), uniqueId);
            }
        }
        try {
            ctx.includeFacelet(parent, _template.getValue(ctx));
        } finally {
            actx.popExtendedClient(this);
        }
    } else {
        this.nextHandler.apply(ctx, parent);
    }
}
Also used : FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext)

Example 5 with FaceletCompositionContext

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

the class DecorateHandler method apply.

/*
     * (non-Javadoc)
     * 
     * @see jakarta.faces.view.facelets.FaceletHandler#apply(jakarta.faces.view.facelets.FaceletContext,
     * jakarta.faces.component.UIComponent)
     */
@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, ELException {
    AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
    actx.pushClient(this);
    FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
    String uniqueId = null;
    try {
        if (!_template.isLiteral()) {
            uniqueId = actx.generateUniqueFaceletTagId(fcc.startComponentUniqueIdSection(), tagId);
        } else if (_params != null) {
            uniqueId = actx.generateUniqueFaceletTagId(fcc.generateUniqueComponentId(), tagId);
        }
        if (_params != null) {
            for (int i = 0; i < _params.length; i++) {
                _params[i].apply(ctx, parent, _params[i].getName(ctx), _params[i].getValue(ctx), uniqueId);
            }
        }
        String path;
        boolean markInitialState = false;
        if (!_template.isLiteral()) {
            String restoredPath = (String) ComponentSupport.restoreInitialTagState(ctx, fcc, parent, uniqueId);
            if (restoredPath != null) {
                // evaluated and if is not equals, trigger markInitialState stuff.
                if (!PhaseId.RESTORE_VIEW.equals(ctx.getFacesContext().getCurrentPhaseId())) {
                    path = this._template.getValue(ctx);
                    if (StringUtils.isBlank(path)) {
                        return;
                    }
                    if (!path.equals(restoredPath)) {
                        markInitialState = true;
                    }
                } else {
                    path = restoredPath;
                }
            } else {
                // No state restored, calculate path
                path = this._template.getValue(ctx);
            }
            ComponentSupport.saveInitialTagState(ctx, fcc, parent, uniqueId, path);
        } else {
            path = _template.getValue(ctx);
        }
        try {
            boolean oldMarkInitialState = false;
            Boolean isBuildingInitialState = null;
            if (markInitialState) {
                // set markInitialState flag
                oldMarkInitialState = fcc.isMarkInitialState();
                fcc.setMarkInitialState(true);
                isBuildingInitialState = (Boolean) ctx.getFacesContext().getAttributes().put(StateManager.IS_BUILDING_INITIAL_STATE, Boolean.TRUE);
            }
            try {
                ctx.includeFacelet(parent, path);
            } finally {
                if (markInitialState) {
                    // unset markInitialState flag
                    if (isBuildingInitialState == null) {
                        ctx.getFacesContext().getAttributes().remove(StateManager.IS_BUILDING_INITIAL_STATE);
                    } else {
                        ctx.getFacesContext().getAttributes().put(StateManager.IS_BUILDING_INITIAL_STATE, isBuildingInitialState);
                    }
                    fcc.setMarkInitialState(oldMarkInitialState);
                }
            }
        } finally {
            actx.popClient(this);
        }
    } finally {
        if (!_template.isLiteral()) {
            fcc.endComponentUniqueIdSection();
            if (fcc.isUsingPSSOnThisView() && fcc.isRefreshTransientBuildOnPSS() && !fcc.isRefreshingTransientBuild()) {
                // Mark the parent component to be saved and restored fully.
                ComponentSupport.markComponentToRestoreFully(ctx.getFacesContext(), parent);
            }
            if (fcc.isDynamicComponentSection()) {
                ComponentSupport.markComponentToRefreshDynamically(ctx.getFacesContext(), parent);
            }
        }
    }
}
Also used : FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext)

Aggregations

FaceletCompositionContext (org.apache.myfaces.view.facelets.FaceletCompositionContext)30 AbstractFaceletContext (org.apache.myfaces.view.facelets.AbstractFaceletContext)19 UIComponent (jakarta.faces.component.UIComponent)12 TagException (jakarta.faces.view.facelets.TagException)8 ValueExpression (jakarta.el.ValueExpression)7 UniqueIdVendor (jakarta.faces.component.UniqueIdVendor)7 VariableMapper (jakarta.el.VariableMapper)4 FacesContext (jakarta.faces.context.FacesContext)4 VariableMapperWrapper (org.apache.myfaces.view.facelets.el.VariableMapperWrapper)4 UIViewRoot (jakarta.faces.component.UIViewRoot)3 FaceletContext (jakarta.faces.view.facelets.FaceletContext)3 EditableValueHolder (jakarta.faces.component.EditableValueHolder)2 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)2 BeanDescriptor (java.beans.BeanDescriptor)2 PropertyDescriptor (java.beans.PropertyDescriptor)2 IOException (java.io.IOException)2 URL (java.net.URL)2 PageContext (org.apache.myfaces.view.facelets.PageContext)2 FaceletStateValueExpression (org.apache.myfaces.view.facelets.el.FaceletStateValueExpression)2 TemplateContextImpl (org.apache.myfaces.view.facelets.impl.TemplateContextImpl)2