use of com.sun.faces.context.StateContext in project mojarra by eclipse-ee4j.
the class FaceletPartialStateManagementStrategy method saveView.
/**
* Save the view.
*
* @param context the Faces context.
* @return the saved view.
*/
@Override
public Object saveView(FacesContext context) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest("FaceletPartialStateManagementStrategy.saveView");
}
if (context == null) {
return null;
}
UIViewRoot viewRoot = context.getViewRoot();
if (viewRoot.isTransient()) {
return null;
}
Util.checkIdUniqueness(context, viewRoot, new HashSet<>(viewRoot.getChildCount() << 1));
final Map<String, Object> stateMap = new HashMap<>();
final StateContext stateContext = StateContext.getStateContext(context);
context.getAttributes().put(SKIP_ITERATION_HINT, true);
Set<VisitHint> hints = EnumSet.of(VisitHint.SKIP_ITERATION);
VisitContext visitContext = VisitContext.createVisitContext(context, null, hints);
final FacesContext finalContext = context;
try {
viewRoot.visitTree(visitContext, (context1, target) -> {
VisitResult result = VisitResult.ACCEPT;
Object stateObj;
if (!target.isTransient()) {
if (stateContext.componentAddedDynamically(target)) {
target.getAttributes().put(DYNAMIC_COMPONENT, target.getParent().getChildren().indexOf(target));
stateObj = new StateHolderSaver(finalContext, target);
} else {
stateObj = target.saveState(context1.getFacesContext());
}
if (stateObj != null) {
stateMap.put(target.getClientId(context1.getFacesContext()), stateObj);
}
} else {
return VisitResult.REJECT;
}
return result;
});
} finally {
context.getAttributes().remove(SKIP_ITERATION_HINT);
}
saveDynamicActions(context, stateContext, stateMap);
StateContext.release(context);
return new Object[] { null, stateMap };
}
use of com.sun.faces.context.StateContext in project mojarra by eclipse-ee4j.
the class FaceletPartialStateManagementStrategy 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, "FaceletPartialStateManagementStrategy.restoreView", new Object[] { viewId, renderKitId });
}
ResponseStateManager rsm = RenderKitUtils.getResponseStateManager(context, renderKitId);
boolean processingEvents = context.isProcessingEvents();
UIViewRoot viewRoot = context.getViewRoot();
Object[] rawState = (Object[]) rsm.getState(context, viewId);
if (rawState == null) {
return null;
}
@SuppressWarnings("unchecked") final Map<String, Object> state = (Map<String, Object>) rawState[1];
final StateContext stateContext = StateContext.getStateContext(context);
if (state != null) {
try {
stateContext.setTrackViewModifications(false);
context.getAttributes().put(SKIP_ITERATION_HINT, true);
Set<VisitHint> hints = EnumSet.of(VisitHint.SKIP_ITERATION, VisitHint.EXECUTE_LIFECYCLE);
VisitContext visitContext = VisitContext.createVisitContext(context, null, hints);
viewRoot.visitTree(visitContext, (context1, target) -> {
VisitResult result = VisitResult.ACCEPT;
String cid = target.getClientId(context1.getFacesContext());
Object stateObj = state.get(cid);
if (stateObj != null && !stateContext.componentAddedDynamically(target)) {
boolean restoreStateNow = true;
if (stateObj instanceof StateHolderSaver) {
restoreStateNow = !((StateHolderSaver) stateObj).componentAddedDynamically();
}
if (restoreStateNow) {
try {
target.restoreState(context1.getFacesContext(), stateObj);
} catch (Exception e) {
String msg = MessageUtils.getExceptionMessageString(MessageUtils.PARTIAL_STATE_ERROR_RESTORING_ID, cid, e.toString());
throw new FacesException(msg, e);
}
}
}
return result;
});
restoreDynamicActions(context, stateContext, state);
} finally {
stateContext.setTrackViewModifications(true);
context.getAttributes().remove(SKIP_ITERATION_HINT);
}
} else {
viewRoot = null;
}
context.setProcessingEvents(processingEvents);
return viewRoot;
}
use of com.sun.faces.context.StateContext 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);
}
use of com.sun.faces.context.StateContext 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);
}
}
}
}
use of com.sun.faces.context.StateContext 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);
}
}
}
Aggregations