Search in sources :

Example 6 with StateContext

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

the class FaceletFullStateManagementStrategy method restoreDynamicRemove.

/**
 * Method that takes care of restoring a dynamic remove.
 *
 * @param context the Faces context.
 * @param struct the component struct.
 */
private void restoreDynamicRemove(FacesContext context, ComponentStruct struct) {
    if (LOGGER.isLoggable(FINEST)) {
        LOGGER.finest("FaceletFullStateManagementStrategy.restoreDynamicRemove");
    }
    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(com.sun.faces.context.StateContext)

Example 7 with StateContext

use of com.sun.faces.context.StateContext 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 8 with StateContext

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

the class FaceletFullStateManagementStrategy method restoreView.

/**
 * Restore the view.
 *
 * @param context the Faces context.
 * @param viewId the view id.
 * @param renderKitId the render kit id.
 * @return the view root.
 */
@Override
public UIViewRoot restoreView(FacesContext context, String viewId, String renderKitId) {
    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.log(Level.FINEST, "FaceletFullStateManagementStrategy.restoreView", new Object[] { viewId, renderKitId });
    }
    UIViewRoot result = null;
    ResponseStateManager rsm = RenderKitUtils.getResponseStateManager(context, renderKitId);
    Object[] state = (Object[]) rsm.getState(context, viewId);
    if (state != null && state.length >= 2) {
        /*
             * Restore the component tree.
             */
        if (state[0] != null) {
            result = restoreTree(context, renderKitId, ((Object[]) state[0]).clone());
            context.setViewRoot(result);
        }
        if (result != null) {
            StateContext stateContext = StateContext.getStateContext(context);
            stateContext.startTrackViewModifications(context, result);
            stateContext.setTrackViewModifications(false);
            try {
                @SuppressWarnings("unchecked") HashMap<String, Object> stateMap = (HashMap<String, Object>) state[1];
                if (stateMap != null) {
                    /*
                         * Restore the component state.
                         */
                    restoreComponentState(context, stateMap);
                    /*
                         * Restore the dynamic actions.
                         */
                    restoreDynamicActions(context, stateContext, stateMap);
                }
            } finally {
                stateContext.setTrackViewModifications(true);
            }
        }
    }
    /*
         * Make sure the library contracts get setup as well.
         */
    ViewHandler viewHandler = context.getApplication().getViewHandler();
    ViewDeclarationLanguage vdl = viewHandler.getViewDeclarationLanguage(context, viewId);
    context.setResourceLibraryContracts(vdl.calculateResourceLibraryContracts(context, viewId));
    return result;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ResponseStateManager(jakarta.faces.render.ResponseStateManager) ViewHandler(jakarta.faces.application.ViewHandler) StateContext(com.sun.faces.context.StateContext) ViewDeclarationLanguage(jakarta.faces.view.ViewDeclarationLanguage) UIViewRoot(jakarta.faces.component.UIViewRoot)

Example 9 with StateContext

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

the class FaceletPartialStateManagementStrategy method restoreDynamicRemove.

/**
 * Method that takes care of restoring a dynamic remove.
 *
 * @param context the Faces context.
 * @param struct the component struct.
 */
private void restoreDynamicRemove(FacesContext context, ComponentStruct struct) {
    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.finest("FaceletPartialStateManagementStrategy.restoreDynamicRemove");
    }
    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(com.sun.faces.context.StateContext)

Example 10 with StateContext

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

the class FaceletPartialStateManagementStrategy method restoreDynamicAdd.

/**
 * Method that takes care of restoring a dynamic add.
 *
 * @param context the Faces context.
 * @param state the state.
 * @param struct the component struct.
 */
private void restoreDynamicAdd(FacesContext context, Map<String, Object> state, ComponentStruct struct) {
    if (LOGGER.isLoggable(FINEST)) {
        LOGGER.finest("FaceletPartialStateManagementStrategy.restoreDynamicAdd");
    }
    UIComponent parent = locateComponentByClientId(context, context.getViewRoot(), struct.getParentClientId());
    if (parent != null) {
        UIComponent child = locateComponentByClientId(context, parent, struct.getClientId());
        /*
             * If Facelets engine restored the child before us we are going to use it, but we need to remove it before we can add it
             * in the correct place.
             */
        if (child != null) {
            if (struct.getFacetName() == null) {
                parent.getChildren().remove(child);
            } else {
                parent.getFacets().remove(struct.getFacetName());
            }
        }
        /*
             * The child was not build previously, so we are going to check if the component was saved in the state.
             */
        if (child == null) {
            StateHolderSaver saver = (StateHolderSaver) state.get(struct.getClientId());
            if (saver != null) {
                child = (UIComponent) saver.restore(context);
            }
        }
        /*
             * Are we adding =BACK= in a component that was not in the state, because it was added by the initial buildView and
             * removed by another dynamic action?
             */
        StateContext stateContext = StateContext.getStateContext(context);
        if (child == null) {
            child = stateContext.getDynamicComponents().get(struct.getClientId());
        }
        /*
             * Now if we have the child we are going to add it back in.
             */
        if (child != null) {
            if (struct.getFacetName() != null) {
                parent.getFacets().put(struct.getFacetName(), child);
            } 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(com.sun.faces.context.StateContext) VisitHint(jakarta.faces.component.visit.VisitHint)

Aggregations

StateContext (com.sun.faces.context.StateContext)15 UIComponent (jakarta.faces.component.UIComponent)7 UIViewRoot (jakarta.faces.component.UIViewRoot)6 VisitHint (jakarta.faces.component.visit.VisitHint)6 StateContext.getStateContext (com.sun.faces.context.StateContext.getStateContext)4 VisitContext (jakarta.faces.component.visit.VisitContext)4 VisitResult (jakarta.faces.component.visit.VisitResult)4 HashMap (java.util.HashMap)4 FacesException (jakarta.faces.FacesException)2 FacesContext (jakarta.faces.context.FacesContext)2 ResponseStateManager (jakarta.faces.render.ResponseStateManager)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 XMLFrontMatterSaver (com.sun.faces.facelets.impl.XMLFrontMatterSaver)1 ComponentStruct (com.sun.faces.util.ComponentStruct)1 ViewHandler (jakarta.faces.application.ViewHandler)1 Doctype (jakarta.faces.component.Doctype)1 ViewDeclarationLanguage (jakarta.faces.view.ViewDeclarationLanguage)1 Facelet (jakarta.faces.view.facelets.Facelet)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1