Search in sources :

Example 1 with UniqueIdVendor

use of jakarta.faces.component.UniqueIdVendor 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 2 with UniqueIdVendor

use of jakarta.faces.component.UniqueIdVendor in project mojarra by eclipse-ee4j.

the class ComponentTagHandlerDelegateImpl method assignUniqueId.

protected void assignUniqueId(FaceletContext ctx, UIComponent parent, String id, UIComponent c) {
    if (this.id != null && !(this.id.isLiteral() && IterationIdManager.registerLiteralId(ctx, this.id.getValue()))) {
        c.setId(this.id.getValue(ctx));
    } else {
        UIViewRoot root = ComponentSupport.getViewRoot(ctx, parent);
        if (root != null) {
            String uid;
            IdMapper mapper = IdMapper.getMapper(ctx.getFacesContext());
            String mid = mapper != null ? mapper.getAliasedId(id) : id;
            UIComponent ancestorNamingContainer = parent.getNamingContainer();
            if (null != ancestorNamingContainer && ancestorNamingContainer instanceof UniqueIdVendor) {
                uid = ((UniqueIdVendor) ancestorNamingContainer).createUniqueId(ctx.getFacesContext(), mid);
            } else {
                uid = root.createUniqueId(ctx.getFacesContext(), mid);
            }
            c.setId(uid);
        }
    }
    if (rendererType != null) {
        c.setRendererType(rendererType);
    }
}
Also used : UIComponent(jakarta.faces.component.UIComponent) IdMapper(com.sun.faces.facelets.impl.IdMapper) UIViewRoot(jakarta.faces.component.UIViewRoot) UniqueIdVendor(jakarta.faces.component.UniqueIdVendor)

Example 3 with UniqueIdVendor

use of jakarta.faces.component.UniqueIdVendor in project mojarra by eclipse-ee4j.

the class CompositeComponentTagHandler method applyCompositeComponent.

// --------------------------------------------------------- Private Methods
private void applyCompositeComponent(FaceletContext ctx, UIComponent c) throws IOException {
    FacesContext facesContext = ctx.getFacesContext();
    VariableMapper orig = ctx.getVariableMapper();
    UIPanel facetComponent;
    if (ComponentHandler.isNew(c)) {
        facetComponent = (UIPanel) facesContext.getApplication().createComponent("jakarta.faces.Panel");
        facetComponent.setRendererType("jakarta.faces.Group");
        facetComponent.setId((c instanceof UniqueIdVendor ? (UniqueIdVendor) c : getViewRoot(ctx, c)).createUniqueId(facesContext, null));
        c.getFacets().put(UIComponent.COMPOSITE_FACET_NAME, facetComponent);
    } else {
        facetComponent = (UIPanel) c.getFacets().get(UIComponent.COMPOSITE_FACET_NAME);
    }
    assert null != facetComponent;
    try {
        VariableMapper wrapper = new VariableMapperWrapper(orig) {

            @Override
            public ValueExpression resolveVariable(String variable) {
                return super.resolveVariable(variable);
            }
        };
        ctx.setVariableMapper(wrapper);
        /*
             * We need to use includeFacelet because our facelet component map expects each Facelet component to generate a unique
             * id (MARK_ID).
             */
        ctx.includeFacelet(facetComponent, ccResource.getURL());
    } finally {
        ctx.setVariableMapper(orig);
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) VariableMapper(jakarta.el.VariableMapper) UIPanel(jakarta.faces.component.UIPanel) VariableMapperWrapper(com.sun.faces.facelets.el.VariableMapperWrapper) UniqueIdVendor(jakarta.faces.component.UniqueIdVendor)

Example 4 with UniqueIdVendor

use of jakarta.faces.component.UniqueIdVendor in project mojarra by eclipse-ee4j.

the class UITextHandler method apply.

@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
    if (parent != null) {
        try {
            ELText nt = txt.apply(ctx.getExpressionFactory(), ctx);
            UIComponent c = new UIText(alias, nt);
            String uid;
            UIComponent ancestorNamingContainer = parent.getNamingContainer();
            if (null != ancestorNamingContainer && ancestorNamingContainer instanceof UniqueIdVendor) {
                uid = ((UniqueIdVendor) ancestorNamingContainer).createUniqueId(ctx.getFacesContext(), null);
            } else {
                uid = ComponentSupport.getViewRoot(ctx, parent).createUniqueId();
            }
            c.setId(uid);
            addComponent(ctx, parent, c);
        } catch (Exception e) {
            throw new ELException(alias + ": " + e.getMessage(), e.getCause());
        }
    }
}
Also used : ELText(com.sun.faces.facelets.el.ELText) UIComponent(jakarta.faces.component.UIComponent) ELException(jakarta.el.ELException) UniqueIdVendor(jakarta.faces.component.UniqueIdVendor) IOException(java.io.IOException) ELException(jakarta.el.ELException)

Example 5 with UniqueIdVendor

use of jakarta.faces.component.UniqueIdVendor in project myfaces by apache.

the class ComponentSupport method createFacetUIPanel.

/**
 * Create a new UIPanel for the use as a dynamically
 * created container for multiple children in a facet.
 * Duplicate in jakarta.faces.webapp.UIComponentClassicTagBase.
 * @param facesContext
 * @return
 */
private static UIComponent createFacetUIPanel(FaceletContext ctx, UIComponent parent, String facetName) {
    FacesContext facesContext = ctx.getFacesContext();
    UIComponent panel = facesContext.getApplication().createComponent(facesContext, UIPanel.COMPONENT_TYPE, null);
    // The panel created by this method is special. To be restored properly and do not
    // create duplicate ids or any other unwanted conflicts, it requires an unique id.
    // This code is usually called when more than one component is added to a facet and
    // it is necessary to create a shared container.
    // Use FaceletCompositionContext.generateUniqueComponentId() is not possible, because
    // <c:if> blocks inside a facet will make component ids unstable. Use UniqueIdVendor
    // is feasible but also will be affected by <c:if> blocks inside a facet.
    // The only solution that will generate real unique ids is use the parent id and the
    // facet name and derive an unique id that cannot be generated by SectionUniqueIdCounter,
    // doing the same trick as with metadata: use a double __ and add a prefix (f).
    // Note this id will never be printed into the response, because this is just a container.
    FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(ctx);
    UniqueIdVendor uniqueIdVendor = mctx.getUniqueIdVendorFromStack();
    if (uniqueIdVendor == null) {
        uniqueIdVendor = ComponentSupport.getViewRoot(ctx, parent);
    }
    if (uniqueIdVendor != null) {
        String cleanFacetName = facetName.replace('.', '_').replace('{', '_').replace('}', '_');
        panel.setId(uniqueIdVendor.createUniqueId(facesContext, mctx.getSharedStringBuilder().append(parent.getId()).append("__f_").append(cleanFacetName).toString()));
    }
    panel.getAttributes().put(FACET_CREATED_UIPANEL_MARKER, Boolean.TRUE);
    panel.getAttributes().put(ComponentSupport.COMPONENT_ADDED_BY_HANDLER_MARKER, Boolean.TRUE);
    return panel;
}
Also used : FacesContext(jakarta.faces.context.FacesContext) FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) UIComponent(jakarta.faces.component.UIComponent) UniqueIdVendor(jakarta.faces.component.UniqueIdVendor)

Aggregations

UniqueIdVendor (jakarta.faces.component.UniqueIdVendor)14 UIComponent (jakarta.faces.component.UIComponent)10 FaceletCompositionContext (org.apache.myfaces.view.facelets.FaceletCompositionContext)7 FacesContext (jakarta.faces.context.FacesContext)5 AbstractFaceletContext (org.apache.myfaces.view.facelets.AbstractFaceletContext)4 UIViewRoot (jakarta.faces.component.UIViewRoot)3 IdMapper (com.sun.faces.facelets.impl.IdMapper)2 VariableMapper (jakarta.el.VariableMapper)2 UIPanel (jakarta.faces.component.UIPanel)2 FaceletContext (jakarta.faces.view.facelets.FaceletContext)2 TagException (jakarta.faces.view.facelets.TagException)2 AjaxHandler (org.apache.myfaces.view.facelets.tag.faces.core.AjaxHandler)2 ELText (com.sun.faces.facelets.el.ELText)1 VariableMapperWrapper (com.sun.faces.facelets.el.VariableMapperWrapper)1 ELException (jakarta.el.ELException)1 FacesException (jakarta.faces.FacesException)1 Resource (jakarta.faces.application.Resource)1 EditableValueHolder (jakarta.faces.component.EditableValueHolder)1 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)1 VisitContext (jakarta.faces.component.visit.VisitContext)1