use of org.apache.myfaces.view.facelets.tag.faces.FaceletState in project myfaces by apache.
the class ForEachHandler method applyOnRefresh.
private void applyOnRefresh(FaceletContext ctx, FaceletCompositionContext fcc, PageContext pctx, UIComponent parent, String uniqueId, Object src, ValueExpression srcVE, IterationState restoredSavedOption) throws IOException {
int s = this.getBegin(ctx);
int e = this.getEnd(ctx);
int m = this.getStep(ctx);
Integer sO = this.begin != null ? Integer.valueOf(s) : null;
Integer eO = this.end != null ? Integer.valueOf(e) : null;
Integer mO = this.step != null ? Integer.valueOf(m) : null;
boolean t = this.getTransient(ctx);
// Refresh, evaluate and synchronize state
Iterator<?> itr = this.toIterator(src);
IterationState iterationState = new IterationState();
iterationState.setCounter(restoredSavedOption.getCounter());
if (itr != null) {
int i = 0;
// move to start
while (i < s && itr.hasNext()) {
itr.next();
i++;
}
String v = this.getVarName(ctx);
String vs = this.getVarStatusName(ctx);
ValueExpression vO = this.capture(v, pctx);
ValueExpression vsO = this.capture(vs, pctx);
int mi = 0;
Object value = null;
int stateIndex = 0;
try {
boolean first = true;
while (i <= e && itr.hasNext()) {
value = itr.next();
Object[] stateValue = null;
/*restoredSavedOption.getValueList().get(stateIndex);*/
String base = null;
boolean found = false;
// The important thing here is use the same base for the generated component ids
// for each element in the iteration that was used on the restore. To do that
//
int stateIndexCheck = stateIndex;
for (; stateIndexCheck < restoredSavedOption.getValueList().size(); stateIndexCheck++) {
stateValue = restoredSavedOption.getValueList().get(stateIndexCheck);
if (value.equals(stateValue[1])) {
found = true;
break;
}
}
if (found) {
stateIndex = stateIndexCheck;
base = (String) stateValue[0];
stateIndex++;
} else {
// No state, added item, create new count
Integer count = iterationState.getCounter();
base = count.toString();
iterationState.setCounter(count + 1);
stateValue = null;
}
if (value instanceof Serializable) {
iterationState.getValueList().add(new Object[] { base, value, i });
}
try {
fcc.startComponentUniqueIdSection(base);
setVar(ctx, parent, uniqueId, base, t, src, srcVE, value, v, i);
boolean last = !itr.hasNext();
// set the varStatus
if (vs != null) {
IterationStatus itrS = new IterationStatus(first, last, i, sO, eO, mO, value);
ValueExpression ve;
if (t || srcVE == null) {
if (srcVE == null) {
ve = null;
} else {
ve = ctx.getExpressionFactory().createValueExpression(itrS, Object.class);
}
} else {
ve = new IterationStatusExpression(itrS);
}
setVar(ctx, parent, uniqueId, base + "_vs", vs, ve, srcVE);
}
// setVarStatus(ctx, pctx, t, sO, eO, mO, srcVE, value, vs, first, !itr.hasNext(), i);
// execute body
// !restoredSavedOption.equals(i)
boolean markInitialState = (stateValue == null);
boolean oldMarkInitialState = false;
Boolean isBuildingInitialState = null;
try {
if (markInitialState && fcc.isUsingPSSOnThisView()) {
// set markInitialState flag
oldMarkInitialState = fcc.isMarkInitialState();
fcc.setMarkInitialState(true);
isBuildingInitialState = (Boolean) ctx.getFacesContext().getAttributes().put(StateManager.IS_BUILDING_INITIAL_STATE, Boolean.TRUE);
}
this.nextHandler.apply(ctx, parent);
} finally {
if (markInitialState && fcc.isUsingPSSOnThisView()) {
// unset markInitialState flag
if (isBuildingInitialState == null) {
ctx.getFacesContext().getAttributes().remove(StateManager.IS_BUILDING_INITIAL_STATE);
} else {
ctx.getFacesContext().getAttributes().put(StateManager.IS_BUILDING_INITIAL_STATE, isBuildingInitialState);
}
fcc.setMarkInitialState(oldMarkInitialState);
}
}
} finally {
fcc.endComponentUniqueIdSection(base);
}
// increment steps
mi = 1;
while (mi < m && itr.hasNext()) {
itr.next();
mi++;
i++;
}
i++;
first = false;
}
} finally {
removeVarAndVarStatus(pctx, v, vO, vs, vsO);
}
}
FaceletState faceletState = ComponentSupport.getFaceletState(ctx, parent, true);
faceletState.putState(uniqueId, iterationState);
}
use of org.apache.myfaces.view.facelets.tag.faces.FaceletState in project myfaces by apache.
the class ForEachHandler method apply.
@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, ELException {
int e = this.getEnd(ctx);
Object src = null;
ValueExpression srcVE = null;
if (this.items != null) {
srcVE = this.items.getValueExpression(ctx, Object.class);
src = srcVE.getValue(ctx);
} else {
byte[] b = new byte[e + 1];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) i;
}
src = b;
}
FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
// Just increment one number to ensure the prefix doesn't conflict later if two
// c:forEach are close between each other. Note c:forEach is different from
// c:if tag and doesn't require a section because c:forEach requires to provide
// multiple sections starting with a specified "base" related to the element
// position and value in the collection.
fcc.incrementUniqueComponentId();
String uniqueId = actx.generateUniqueFaceletTagId(fcc.generateUniqueId(), tagId);
if (src != null) {
PageContext pctx = actx.getPageContext();
// c:forEach is special because it requires FaceletState even if no pss is used.
FaceletState restoredFaceletState = ComponentSupport.getFaceletState(ctx, parent, false);
IterationState restoredSavedOption = (restoredFaceletState == null) ? null : (IterationState) restoredFaceletState.getState(uniqueId);
if (restoredSavedOption != null) {
if (!PhaseId.RESTORE_VIEW.equals(ctx.getFacesContext().getCurrentPhaseId())) {
// Refresh, evaluate and synchronize state
applyOnRefresh(ctx, fcc, pctx, parent, uniqueId, src, srcVE, restoredSavedOption);
} else {
// restore view, don't record iteration, use the saved value
applyOnRestore(ctx, fcc, pctx, parent, uniqueId, src, srcVE, restoredSavedOption);
}
} else {
// First time
applyFirstTime(ctx, fcc, pctx, parent, uniqueId, src, srcVE);
}
}
if (fcc.isUsingPSSOnThisView() && fcc.isRefreshTransientBuildOnPSS() && !fcc.isRefreshingTransientBuild()) {
// Mark the parent component to be saved and restored fully.
ComponentSupport.markComponentToRestoreFully(ctx.getFacesContext(), parent);
}
if (fcc.isDynamicComponentSection()) {
ComponentSupport.markComponentToRefreshDynamically(ctx.getFacesContext(), parent);
}
}
use of org.apache.myfaces.view.facelets.tag.faces.FaceletState in project myfaces by apache.
the class ForEachHandler method setVar.
private void setVar(FaceletContext ctx, UIComponent parent, String uniqueId, String base, String v, ValueExpression ve, ValueExpression srcVE) {
AbstractFaceletContext actx = ((AbstractFaceletContext) ctx);
PageContext pctx = actx.getPageContext();
if (srcVE != null) {
FaceletState faceletState = ComponentSupport.getFaceletState(ctx, parent, true);
faceletState.putBinding(uniqueId, base, ve);
// Put the indirect EL into context
ValueExpression fve = new FaceletStateValueExpression(uniqueId, base);
pctx.getAttributes().put(v, fve);
} else {
pctx.getAttributes().put(v, ve);
}
}
use of org.apache.myfaces.view.facelets.tag.faces.FaceletState in project myfaces by apache.
the class ViewPoolMyFacesRequestTestCase method testPartialPage1_1.
@Test
public void testPartialPage1_1() throws Exception {
Locale locale = null;
startViewRequest("/partialPage1.xhtml");
processLifecycleExecute();
locale = facesContext.getViewRoot().getLocale();
executeBuildViewCycle(facesContext);
// Now let's try to remove some component programatically
// that invalidates the view to be reused without a refresh,
// so in the pool it should be marked as REFRESH_REQUIRED
UIPanel panel = (UIPanel) facesContext.getViewRoot().findComponent("mainForm:panel1");
panel.getParent().getChildren().remove(panel);
UICommand submitButton = (UICommand) facesContext.getViewRoot().findComponent("mainForm:submit");
executeViewHandlerRender(facesContext);
executeAfterRender(facesContext);
client.submit(submitButton);
processLifecycleExecute();
FaceletState faceletState = (FaceletState) facesContext.getViewRoot().getAttributes().get(ComponentSupport.FACELET_STATE_INSTANCE);
UIViewRoot root = new UIViewRoot();
root.setLocale(locale);
root.setRenderKitId("HTML_BASIC");
root.setViewId("/partialPage1.xhtml");
ViewPoolProcessor processor = ViewPoolProcessor.getInstance(facesContext);
ViewPool viewPool = processor.getViewPool(facesContext, root);
// Check the view was used
ViewEntry entry2 = viewPool.popStaticOrPartialStructureView(facesContext, root);
Assert.assertNull(entry2);
ViewEntry entry3 = viewPool.popDynamicStructureView(facesContext, root, faceletState);
Assert.assertNull(entry3);
}
use of org.apache.myfaces.view.facelets.tag.faces.FaceletState in project myfaces by apache.
the class ViewPoolMyFacesRequestTestCase method testPartialPage1_2.
@Test
public void testPartialPage1_2() throws Exception {
Locale locale = null;
startViewRequest("/partialPage1.xhtml");
processLifecycleExecute();
locale = facesContext.getViewRoot().getLocale();
executeBuildViewCycle(facesContext);
// Now let's try to remove some component programatically
// that invalidates the view to be reused without a refresh,
// so in the pool it should be marked as REFRESH_REQUIRED
UIPanel panel = (UIPanel) facesContext.getViewRoot().findComponent("mainForm:panel1");
panel.getParent().getChildren().remove(panel);
facesContext.getViewRoot().getViewMap().put("someKey", "someValue");
UICommand submitButton = (UICommand) facesContext.getViewRoot().findComponent("mainForm:submit");
executeViewHandlerRender(facesContext);
executeAfterRender(facesContext);
client.submit(submitButton);
processLifecycleExecute();
Assert.assertEquals("someValue", facesContext.getViewRoot().getViewMap().get("someKey"));
FaceletState faceletState = (FaceletState) facesContext.getViewRoot().getAttributes().get(ComponentSupport.FACELET_STATE_INSTANCE);
UIViewRoot root = new UIViewRoot();
root.setLocale(locale);
root.setRenderKitId("HTML_BASIC");
root.setViewId("/partialPage1.xhtml");
ViewPoolProcessor processor = ViewPoolProcessor.getInstance(facesContext);
ViewPool viewPool = processor.getViewPool(facesContext, root);
// Check the view was used
ViewEntry entry2 = viewPool.popStaticOrPartialStructureView(facesContext, root);
Assert.assertNull(entry2);
ViewEntry entry3 = viewPool.popDynamicStructureView(facesContext, root, faceletState);
Assert.assertNull(entry3);
}
Aggregations