Author: andrei_exadel
Date: 2008-10-01 09:23:54 -0400 (Wed, 01 Oct 2008)
New Revision: 10641
Modified:
trunk/ui/columns/src/main/java/org/richfaces/taglib/ColumnsHandler.java
Log:
RF-4544
Modified: trunk/ui/columns/src/main/java/org/richfaces/taglib/ColumnsHandler.java
===================================================================
--- trunk/ui/columns/src/main/java/org/richfaces/taglib/ColumnsHandler.java 2008-10-01
12:48:33 UTC (rev 10640)
+++ trunk/ui/columns/src/main/java/org/richfaces/taglib/ColumnsHandler.java 2008-10-01
13:23:54 UTC (rev 10641)
@@ -24,7 +24,6 @@
import org.richfaces.iterator.SimpleForEachIterator;
import com.sun.facelets.FaceletContext;
-import com.sun.facelets.tag.MetaRuleset;
import com.sun.facelets.tag.MetaTagHandler;
import com.sun.facelets.tag.TagAttribute;
import com.sun.facelets.tag.jsf.ComponentConfig;
@@ -59,33 +58,36 @@
/** end attribute */
private TagAttribute end;
- /** Iterator for columns's tag value attribute */
- protected ForEachIterator items; // our 'digested' items
+ class IterationContext {
+
+ /** Iterator for columns's tag value attribute */
+ public ForEachIterator items; // our 'digested' items
+
+ /** Value attribute value */
+ public Object rawItems; // our 'raw' items
+
+ /** Var attr - defines page variable for current item */
+ public String _indexId;
+
+ /** Integer value begin attr */
+ public Integer _begin;
+
+ /** Integer value end attr */
+ public Integer _end;
+
+ /** Integer value of end attr. */
+ public Integer _columns;
+
+ /** String value of var attr */
+ public String _itemId = null;
+
+ /** Current column counter */
+ public Integer _index = 0;
+
+ /** Expression for var item */
+ public IteratedExpression iteratedExpression;
+ };
- /** Value attribute value */
- protected Object rawItems; // our 'raw' items
-
- /** Var attr - defines page variable for current item */
- private String _indexId;
-
- /** Integer value begin attr */
- private Integer _begin;
-
- /** Integer value end attr */
- private Integer _end;
-
- /** Integer value of end attr. */
- private Integer _columns;
-
- /** String value of var attr */
- private String _itemId = null;
-
- /** Current column counter */
- private Integer _index = 0;
-
- /** Expression for var item */
- private IteratedExpression iteratedExpression;
-
/**
* TODO Description goes here.
*
@@ -99,12 +101,12 @@
/**
* Extracts tags attributes values
*/
- private void initVariables(FaceletContext ctx) {
- initColumnsCount(ctx);
- initIndex(ctx);
- initVar(ctx);
- initBegin(ctx);
- initEnd(ctx);
+ private void initVariables(FaceletContext ctx, IterationContext itContext) {
+ initColumnsCount(ctx, itContext);
+ initIndex(ctx, itContext);
+ initVar(ctx, itContext);
+ initBegin(ctx, itContext);
+ initEnd(ctx, itContext);
}
/**
@@ -112,9 +114,9 @@
*
* @throws JspTagException
*/
- private void prepare(FaceletContext ctx) {
+ private void prepare(FaceletContext ctx, IterationContext itContext) {
- initVariables(ctx);
+ initVariables(ctx, itContext);
try {
@@ -125,34 +127,34 @@
// If this is a deferred expression, make a note and get
// the 'items' instance.
- rawItems = value.getObject(ctx);
+ itContext.rawItems = value.getObject(ctx);
// extract an iterator over the 'items' we've got
- items = SimpleForEachIterator
- .supportedTypeForEachIterator(rawItems);
+ itContext.items = SimpleForEachIterator
+ .supportedTypeForEachIterator(itContext.rawItems);
} else {
// no 'items', so use 'begin' and 'end'
- items = SimpleForEachIterator
- .beginEndForEachIterator(_columns - 1);
+ itContext.items = SimpleForEachIterator
+ .beginEndForEachIterator(itContext._columns - 1);
}
} catch (Exception e) {
// TODO: handle exception
}
- correctFirst(ctx);
+ correctFirst(ctx, itContext);
}
/**
* Inits first iteration item
*/
- private void correctFirst(FaceletContext ctx) {
- if (items != null) {
- if (_begin > 0 && (_index < _begin)) {
- while ((_index < _begin && hasNext())) {
- next(ctx);
+ private void correctFirst(FaceletContext ctx, IterationContext itContext) {
+ if (itContext.items != null) {
+ if (itContext._begin > 0 && (itContext._index < itContext._begin)) {
+ while ((itContext._index < itContext._begin && hasNext(itContext))) {
+ next(ctx, itContext);
}
- if (!hasNext()) {
- _index = 0;
+ if (!hasNext(itContext)) {
+ itContext._index = 0;
}
}
}
@@ -164,12 +166,12 @@
* @return
* @throws JspTagException
*/
- private boolean hasNext() {
+ private boolean hasNext(IterationContext itContext) {
try {
- if (_end != 0) {
- return (_index < _end) ? items.hasNext() : false;
+ if (itContext._end != 0) {
+ return (itContext._index < itContext._end) ? itContext.items.hasNext() : false;
} else {
- return items.hasNext();
+ return itContext.items.hasNext();
}
} catch (Exception e) {
return false;
@@ -183,10 +185,10 @@
* @return
* @throws JspTagException
*/
- private Object next(FaceletContext ctx) {
+ private Object next(FaceletContext ctx, IterationContext itContext) {
try {
- Object o = items.next();
- _index++;
+ Object o = itContext.items.next();
+ itContext._index++;
return o;
} catch (Exception e) {
return null;
@@ -196,81 +198,81 @@
/**
* Extracts integer value from end attr
*/
- private void initColumnsCount(FaceletContext ctx) {
+ private void initColumnsCount(FaceletContext ctx, IterationContext itContext) {
this.columns = getAttribute("columns");
if (columns != null) {
try {
- _columns = Integer.parseInt((String) columns.getObject(ctx));
- if (_columns < 0) {
- _columns = 0; // If end is negative set up zero
+ itContext._columns = Integer.parseInt((String) columns.getObject(ctx));
+ if (itContext._columns < 0) {
+ itContext._columns = 0; // If end is negative set up zero
}
} catch (Exception e) {
- _columns = 0;
+ itContext._columns = 0;
}
} else {
- _columns = 0;
+ itContext._columns = 0;
}
}
/**
* Extracts integer value from begin attr
*/
- private void initBegin(FaceletContext ctx) {
+ private void initBegin(FaceletContext ctx, IterationContext itContext) {
this.begin = getAttribute("begin");
if (begin != null) {
try {
Object o = begin.getObject(ctx);
if (o instanceof Number) {
- _begin = ((Number)o).intValue();
+ itContext._begin = ((Number)o).intValue();
}else if (o instanceof String) {
- _begin = Integer.parseInt((String) o);
+ itContext._begin = Integer.parseInt((String) o);
}
- _begin--;
- if (_begin < 0) {
- _begin = 0; // If end is negative set up zero
+ itContext._begin--;
+ if (itContext._begin < 0) {
+ itContext._begin = 0; // If end is negative set up zero
}
} catch (Exception e) {
- _begin = 0;
+ itContext._begin = 0;
}
} else {
- _begin = 0;
+ itContext._begin = 0;
}
}
/**
* Extracts integer value from end attr
*/
- private void initEnd(FaceletContext ctx) {
+ private void initEnd(FaceletContext ctx, IterationContext itContext) {
this.end = getAttribute("end");
if (end != null) {
try {
Object o = end.getObject(ctx);
if (o instanceof Number) {
- _end = ((Number)o).intValue();
+ itContext._end = ((Number)o).intValue();
}else if ( o instanceof String) {
- _end = Integer.parseInt((String) o);
+ itContext._end = Integer.parseInt((String) o);
}
- if (_end < 0) {
- _end = 0; // If end is negative set up zero
+ if (itContext._end < 0) {
+ itContext._end = 0; // If end is negative set up zero
}
} catch (Exception e) {
- _end = 0;
+ itContext._end = 0;
}
} else {
- _end = 0;
+ itContext._end = 0;
}
}
/**
* Extracts string value from var attr
*/
- private void initVar(FaceletContext ctx) {
+ private void initVar(FaceletContext ctx, IterationContext itContext) {
this.var = getAttribute("var");
if (var != null) {
try {
- _itemId = (String) var.getObject(ctx);
+ itContext._itemId = (String) var.getObject(ctx);
} catch (ClassCastException e) {
- _itemId = null;
+ itContext._itemId = null;
}
}
@@ -279,13 +281,13 @@
/**
* Extracts string value from index attr
*/
- private void initIndex(FaceletContext ctx) {
+ private void initIndex(FaceletContext ctx, IterationContext itContext) {
this.index = getAttribute("index");
if (index != null) {
try {
- _indexId = (String) index.getObject(ctx);
+ itContext._indexId = (String) index.getObject(ctx);
} catch (ClassCastException e) {
- _indexId = null;
+ itContext._indexId = null;
}
}
@@ -297,21 +299,22 @@
//@Override
public void apply(FaceletContext ctx, UIComponent parent)
throws IOException, FacesException, ELException {
-
- prepare(ctx); // prepare data
+ IterationContext iterationContext = new IterationContext();
+
+ prepare(ctx, iterationContext); // prepare data
try {
- while (hasNext()) { // for each
- exposeVariables(ctx, _index);
+ while (hasNext(iterationContext)) { // for each
+ exposeVariables(ctx, iterationContext);
//super.apply(ctx, parent);
handler.apply(ctx, parent);
- next(ctx);
+ next(ctx, iterationContext);
}
} catch (Exception e) {
// TODO: handle exception
} finally {
- release();
- unExposeVariables(ctx);
+ release(iterationContext);
+ unExposeVariables(ctx, iterationContext);
}
}
@@ -329,17 +332,18 @@
*
* @throws JspTagException
*/
- private void exposeVariables(FaceletContext ctx, int k) {
+ private void exposeVariables(FaceletContext ctx, IterationContext itContext) {
VariableMapper vm = ctx.getVariableMapper();
+ int k = itContext._index;
- if (_itemId != null) {
+ if (itContext._itemId != null) {
if (vm != null) {
if (value != null) {
ValueExpression srcVE = value.getValueExpression(ctx,
Object.class);
- ValueExpression ve = getVarExpression(ctx, srcVE, k);
- vm.setVariable(_itemId, ve);
+ ValueExpression ve = getVarExpression(ctx, srcVE, itContext);
+ vm.setVariable(itContext._itemId, ve);
}
}
@@ -347,10 +351,10 @@
// Set up index variable
- if (_indexId != null) {
+ if (itContext._indexId != null) {
if (vm != null) {
ValueExpression ve = new IteratedIndexExpression(k);
- vm.setVariable(_indexId, ve);
+ vm.setVariable(itContext._indexId, ve);
}
}
@@ -361,16 +365,16 @@
* Removes page attributes that we have exposed and, if applicable, restores
* them to their prior values (and scopes).
*/
- private void unExposeVariables(FaceletContext ctx) {
+ private void unExposeVariables(FaceletContext ctx, IterationContext itContext) {
VariableMapper vm = ctx.getVariableMapper();
// "nested" variables are now simply removed
- if (_itemId != null) {
+ if (itContext._itemId != null) {
if (vm != null)
- vm.setVariable(_itemId, null);
+ vm.setVariable(itContext._itemId, null);
}
- if (_indexId != null) {
+ if (itContext._indexId != null) {
if (vm != null)
- vm.setVariable(_indexId, null);
+ vm.setVariable(itContext._indexId, null);
}
}
@@ -381,8 +385,9 @@
* @return
*/
private ValueExpression getVarExpression(FaceletContext ctx,
- ValueExpression expr, int k) {
+ ValueExpression expr, IterationContext itContext) {
Object o = expr.getValue(ctx.getFacesContext().getELContext());
+ int k = itContext._index;
if (o.getClass().isArray() || o instanceof List) {
return new IndexedValueExpression(expr, k);
}
@@ -391,10 +396,10 @@
|| o instanceof Enumeration || o instanceof Map
|| o instanceof String) {
- if (iteratedExpression == null) {
- iteratedExpression = new IteratedExpression(expr, ",");
+ if (itContext.iteratedExpression == null) {
+ itContext.iteratedExpression = new IteratedExpression(expr, ",");
}
- return new IteratedValueExpression(iteratedExpression, k);
+ return new IteratedValueExpression(itContext.iteratedExpression, k);
}
throw new ELException("FOREACH_BAD_ITEMS");
@@ -403,9 +408,9 @@
/**
* Release iteration variables
*/
- private void release() {
- this.items = null;
- this._index = 0;
+ private void release(IterationContext itContext) {
+ itContext.items = null;
+ itContext._index = 0;
}
}