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);
}
}
}
}
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;
}
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);
}
}
}
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;
}
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);
}
}
Aggregations