Search in sources :

Example 1 with StateContext.getStateContext

use of com.sun.faces.context.StateContext.getStateContext in project mojarra by eclipse-ee4j.

the class FaceletViewHandlingStrategy method reapplyDynamicAdd.

/**
 * Reapply the dynamic add after Facelets reapply.
 *
 * @param context the Faces context.
 * @param struct the component struct.
 */
private void reapplyDynamicAdd(FacesContext context, ComponentStruct struct) {
    UIComponent parent = locateComponentByClientId(context, context.getViewRoot(), struct.getParentClientId());
    if (parent != null) {
        UIComponent child = locateComponentByClientId(context, parent, struct.getClientId());
        StateContext stateContext = StateContext.getStateContext(context);
        if (child == null) {
            child = stateContext.getDynamicComponents().get(struct.getClientId());
        }
        if (child != null) {
            if (struct.getFacetName() != null) {
                parent.getFacets().remove(struct.getFacetName());
                parent.getFacets().put(struct.getFacetName(), child);
                child.getClientId();
            } else {
                int childIndex = -1;
                if (child.getAttributes().containsKey(DYNAMIC_COMPONENT)) {
                    childIndex = (Integer) child.getAttributes().get(DYNAMIC_COMPONENT);
                }
                child.setId(struct.getId());
                if (childIndex >= parent.getChildCount() || childIndex == -1) {
                    parent.getChildren().add(child);
                } else {
                    parent.getChildren().add(childIndex, child);
                }
                child.getClientId();
                child.getAttributes().put(DYNAMIC_COMPONENT, child.getParent().getChildren().indexOf(child));
            }
            stateContext.getDynamicComponents().put(struct.getClientId(), child);
        }
    }
}
Also used : UIComponent(jakarta.faces.component.UIComponent) StateContext.getStateContext(com.sun.faces.context.StateContext.getStateContext) StateContext(com.sun.faces.context.StateContext)

Example 2 with StateContext.getStateContext

use of com.sun.faces.context.StateContext.getStateContext in project mojarra by eclipse-ee4j.

the class FaceletViewHandlingStrategy method buildView.

/**
 * @see ViewDeclarationLanguage#buildView(FacesContext, UIViewRoot)
 */
@Override
public void buildView(FacesContext ctx, UIViewRoot view) throws IOException {
    StateContext stateCtx = StateContext.getStateContext(ctx);
    if (isViewPopulated(ctx, view)) {
        Facelet facelet = faceletFactory.getFacelet(ctx, view.getViewId());
        // virute of re-applying the handlers.
        try {
            stateCtx.setTrackViewModifications(false);
            facelet.apply(ctx, view);
            reapplyDynamicActions(ctx);
            if (stateCtx.isPartialStateSaving(ctx, view.getViewId())) {
                markInitialStateIfNotMarked(view);
            }
        } finally {
            stateCtx.setTrackViewModifications(true);
        }
        return;
    }
    view.setViewId(view.getViewId());
    LOGGER.log(FINE, () -> "Building View: " + view.getViewId());
    if (faceletFactory == null) {
        faceletFactory = ApplicationAssociate.getInstance(ctx.getExternalContext()).getFaceletFactory();
    }
    RequestStateManager.set(ctx, FACELET_FACTORY, faceletFactory);
    Facelet facelet = faceletFactory.getFacelet(ctx, view.getViewId());
    // populate UIViewRoot
    try {
        ctx.getAttributes().put(IS_BUILDING_INITIAL_STATE, Boolean.TRUE);
        stateCtx.setTrackViewModifications(false);
        facelet.apply(ctx, view);
        if (facelet instanceof XMLFrontMatterSaver) {
            XMLFrontMatterSaver frontMatterSaver = (XMLFrontMatterSaver) facelet;
            Doctype doctype = frontMatterSaver.getSavedDoctype();
            if (doctype != null) {
                saveDOCTYPEToFacesContextAttributes(doctype);
            }
            String XMLDECL = frontMatterSaver.getSavedXMLDecl();
            if (XMLDECL != null) {
                saveXMLDECLToFacesContextAttributes(XMLDECL);
            }
        }
        Doctype doctype = getDOCTYPEFromFacesContextAttributes(ctx);
        if (doctype != null) {
            view.setDoctype(doctype);
        }
        if (!stateCtx.isPartialStateSaving(ctx, view.getViewId())) {
            reapplyDynamicActions(ctx);
        }
        startTrackViewModifications(ctx, view);
    } finally {
        ctx.getAttributes().remove(IS_BUILDING_INITIAL_STATE);
    }
    ctx.getApplication().publishEvent(ctx, PostAddToViewEvent.class, UIViewRoot.class, view);
    markInitialState(ctx, view);
    setViewPopulated(ctx, view);
}
Also used : Facelet(jakarta.faces.view.facelets.Facelet) XMLFrontMatterSaver(com.sun.faces.facelets.impl.XMLFrontMatterSaver) StateContext.getStateContext(com.sun.faces.context.StateContext.getStateContext) StateContext(com.sun.faces.context.StateContext) Doctype(jakarta.faces.component.Doctype)

Example 3 with StateContext.getStateContext

use of com.sun.faces.context.StateContext.getStateContext in project mojarra by eclipse-ee4j.

the class FaceletViewHandlingStrategy method reapplyDynamicActions.

/**
 * Reapply the dynamic actions after Facelets reapply.
 *
 * <p>
 * Note a precondition to this method is that tracking view modifications is turned off during the execution of this
 * method. The caller of this method is responsible for turning tracking view modifications off and on as required.
 * </p>
 *
 * @param context the Faces context.
 */
private void reapplyDynamicActions(FacesContext context) {
    StateContext stateContext = StateContext.getStateContext(context);
    List<ComponentStruct> actions = stateContext.getDynamicActions();
    if (actions != null) {
        for (ComponentStruct action : actions) {
            if (REMOVE.equals(action.getAction())) {
                reapplyDynamicRemove(context, action);
            }
            if (ADD.equals(action.getAction())) {
                reapplyDynamicAdd(context, action);
            }
        }
    }
}
Also used : StateContext.getStateContext(com.sun.faces.context.StateContext.getStateContext) StateContext(com.sun.faces.context.StateContext) ComponentStruct(com.sun.faces.util.ComponentStruct)

Example 4 with StateContext.getStateContext

use of com.sun.faces.context.StateContext.getStateContext in project mojarra by eclipse-ee4j.

the class FaceletViewHandlingStrategy method reapplyDynamicRemove.

/**
 * Reapply the dynamic remove after Facelets reapply.
 *
 * @param context the Faces context.
 * @param struct the component struct.
 */
private void reapplyDynamicRemove(FacesContext context, ComponentStruct struct) {
    UIComponent child = locateComponentByClientId(context, context.getViewRoot(), struct.getClientId());
    if (child != null) {
        StateContext stateContext = StateContext.getStateContext(context);
        stateContext.getDynamicComponents().put(struct.getClientId(), child);
        UIComponent parent = child.getParent();
        if (struct.getFacetName() != null) {
            parent.getFacets().remove(struct.getFacetName());
        } else {
            parent.getChildren().remove(child);
        }
    }
}
Also used : UIComponent(jakarta.faces.component.UIComponent) StateContext.getStateContext(com.sun.faces.context.StateContext.getStateContext) StateContext(com.sun.faces.context.StateContext)

Aggregations

StateContext (com.sun.faces.context.StateContext)4 StateContext.getStateContext (com.sun.faces.context.StateContext.getStateContext)4 UIComponent (jakarta.faces.component.UIComponent)2 XMLFrontMatterSaver (com.sun.faces.facelets.impl.XMLFrontMatterSaver)1 ComponentStruct (com.sun.faces.util.ComponentStruct)1 Doctype (jakarta.faces.component.Doctype)1 Facelet (jakarta.faces.view.facelets.Facelet)1