Search in sources :

Example 1 with StateContext

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

the class ComponentTagHandlerDelegateImpl method adjustIndexOfDynamicChildren.

private void adjustIndexOfDynamicChildren(FacesContext context, UIComponent parent) {
    StateContext stateContext = StateContext.getStateContext(context);
    if (!stateContext.hasOneOrMoreDynamicChild(parent)) {
        return;
    }
    List<UIComponent> children = parent.getChildren();
    List<UIComponent> dynamicChildren = Collections.emptyList();
    for (UIComponent cur : children) {
        if (stateContext.componentAddedDynamically(cur)) {
            if (dynamicChildren.isEmpty()) {
                dynamicChildren = new ArrayList<>(children.size());
            }
            dynamicChildren.add(cur);
        }
    }
    // their original position
    for (UIComponent cur : dynamicChildren) {
        int i = stateContext.getIndexOfDynamicallyAddedChildInParent(cur);
        if (-1 != i) {
            children.remove(cur);
        }
    }
    // back in.
    for (UIComponent cur : dynamicChildren) {
        int i = stateContext.getIndexOfDynamicallyAddedChildInParent(cur);
        if (-1 != i) {
            if (i < children.size()) {
                children.add(i, cur);
            } else {
                children.add(cur);
            }
        }
    }
}
Also used : StateContext(com.sun.faces.context.StateContext) UIComponent(jakarta.faces.component.UIComponent)

Example 2 with StateContext

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

the class FaceletFullStateManagementStrategy method saveView.

/**
 * Save the view.
 *
 * @param context the Faces context.
 * @return the saved view.
 */
@Override
public Object saveView(FacesContext context) {
    if (LOGGER.isLoggable(FINEST)) {
        LOGGER.finest("FaceletFullStateManagementStrategy.saveView");
    }
    Object[] result;
    UIViewRoot viewRoot = context.getViewRoot();
    /*
         * Check uniqueness.
         */
    Util.checkIdUniqueness(context, viewRoot, new HashSet<>(viewRoot.getChildCount() << 1));
    /**
     * Save the dynamic actions.
     */
    StateContext stateContext = StateContext.getStateContext(context);
    saveDynamicActions(context, stateContext, viewRoot);
    /*
         * Save the component state.
         */
    Object state = saveComponentState(context);
    /*
         * Save the tree structure.
         */
    List<TreeNode> treeList = new ArrayList<>(32);
    captureChild(treeList, 0, viewRoot);
    Object[] tree = treeList.toArray();
    result = new Object[] { tree, state };
    StateContext.release(context);
    return result;
}
Also used : StateContext(com.sun.faces.context.StateContext) ArrayList(java.util.ArrayList) UIViewRoot(jakarta.faces.component.UIViewRoot)

Example 3 with StateContext

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

the class FaceletFullStateManagementStrategy 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("FaceletFullStateManagementStrategy.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)

Example 4 with StateContext

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

the class FaceletFullStateManagementStrategy method saveComponentState.

/**
 * Save the component state.
 *
 * @param context the Faces context.
 * @return the saved state.
 */
private Object saveComponentState(FacesContext context) {
    final HashMap<String, Object> stateMap = new HashMap<>();
    final StateContext stateContext = StateContext.getStateContext(context);
    final UIViewRoot viewRoot = context.getViewRoot();
    final FacesContext finalContext = context;
    context.getAttributes().put(SKIP_ITERATION_HINT, true);
    Set<VisitHint> hints = EnumSet.of(SKIP_ITERATION);
    VisitContext visitContext = VisitContext.createVisitContext(context, null, hints);
    try {
        viewRoot.visitTree(visitContext, (context1, component) -> {
            VisitResult result = ACCEPT;
            Object stateObj;
            if (!component.isTransient()) {
                if (stateContext.componentAddedDynamically(component)) {
                    component.getAttributes().put(DYNAMIC_COMPONENT, new Integer(getProperChildIndex(component)));
                    stateObj = new StateHolderSaver(finalContext, component);
                } else {
                    stateObj = component.saveState(finalContext);
                }
                if (stateObj != null) {
                    stateMap.put(component.getClientId(finalContext), stateObj);
                }
            } else {
                result = REJECT;
            }
            return result;
        });
    } finally {
        context.getAttributes().remove(SKIP_ITERATION_HINT);
    }
    return stateMap;
}
Also used : FacesContext(jakarta.faces.context.FacesContext) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) VisitHint(jakarta.faces.component.visit.VisitHint) VisitContext(jakarta.faces.component.visit.VisitContext) StateContext(com.sun.faces.context.StateContext) VisitResult(jakarta.faces.component.visit.VisitResult) UIViewRoot(jakarta.faces.component.UIViewRoot)

Example 5 with StateContext

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

the class FaceletFullStateManagementStrategy method restoreComponentState.

/**
 * Restore the component state.
 *
 * @param context the Faces context.
 * @param state the component state.
 */
private void restoreComponentState(final FacesContext context, final HashMap<String, Object> state) {
    final StateContext stateContext = StateContext.getStateContext(context);
    final UIViewRoot viewRoot = context.getViewRoot();
    try {
        context.getAttributes().put(SKIP_ITERATION_HINT, true);
        Set<VisitHint> hints = EnumSet.of(SKIP_ITERATION);
        VisitContext visitContext = VisitContext.createVisitContext(context, null, hints);
        viewRoot.visitTree(visitContext, (visitContext1, component) -> {
            VisitResult result = ACCEPT;
            String clientId = component.getClientId(context);
            Object stateObj = state.get(clientId);
            if (stateObj != null && !stateContext.componentAddedDynamically(component)) {
                boolean restoreStateNow = true;
                if (stateObj instanceof StateHolderSaver) {
                    restoreStateNow = !((StateHolderSaver) stateObj).componentAddedDynamically();
                }
                if (restoreStateNow) {
                    try {
                        component.restoreState(context, stateObj);
                    } catch (Exception e) {
                        throw new FacesException(e);
                    }
                }
            }
            return result;
        });
    } finally {
        context.getAttributes().remove(SKIP_ITERATION_HINT);
    }
}
Also used : VisitHint(jakarta.faces.component.visit.VisitHint) VisitContext(jakarta.faces.component.visit.VisitContext) StateContext(com.sun.faces.context.StateContext) VisitResult(jakarta.faces.component.visit.VisitResult) UIViewRoot(jakarta.faces.component.UIViewRoot) FacesException(jakarta.faces.FacesException) IOException(java.io.IOException) FacesException(jakarta.faces.FacesException)

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