Author: andrei_exadel
Date: 2008-03-11 10:03:41 -0400 (Tue, 11 Mar 2008)
New Revision: 6691
Added:
trunk/ui/columns/src/main/java/org/richfaces/taglib/ComponentHandler.java
Removed:
trunk/ui/columns/src/main/java/org/richfaces/taglib/ColumnsTagHandler.java
trunk/ui/columns/src/main/java/org/richfaces/taglib/ComponentHandler.java
Modified:
trunk/ui/columns/src/main/config/component/columns.xml
Log:
RF-2413
Modified: trunk/ui/columns/src/main/config/component/columns.xml
===================================================================
--- trunk/ui/columns/src/main/config/component/columns.xml 2008-03-11 14:02:16 UTC (rev
6690)
+++ trunk/ui/columns/src/main/config/component/columns.xml 2008-03-11 14:03:41 UTC (rev
6691)
@@ -21,7 +21,7 @@
<test/>
</tag>
<taghandler generate="false">
- <classname>org.richfaces.taglib.ColumnsTagHandler</classname>
+ <classname>org.richfaces.taglib.ComponentHandler</classname>
</taghandler>
&ui_data_attributes;
&attributes;
Deleted: trunk/ui/columns/src/main/java/org/richfaces/taglib/ColumnsTagHandler.java
===================================================================
--- trunk/ui/columns/src/main/java/org/richfaces/taglib/ColumnsTagHandler.java 2008-03-11
14:02:16 UTC (rev 6690)
+++ trunk/ui/columns/src/main/java/org/richfaces/taglib/ColumnsTagHandler.java 2008-03-11
14:03:41 UTC (rev 6691)
@@ -1,393 +0,0 @@
-/*
- * ColumnsHandler.java Date created: 07.12.2007
- * Last modified by: $Author$
- * $Revision$ $Date$
- */
-
-package org.richfaces.taglib;
-
-import java.io.IOException;
-import java.util.Collection;
-import java.util.Enumeration;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import javax.el.ELException;
-import javax.el.ValueExpression;
-import javax.el.VariableMapper;
-import javax.faces.FacesException;
-import javax.faces.component.UIComponent;
-import javax.servlet.jsp.JspTagException;
-
-import org.richfaces.iterator.ForEachIterator;
-import org.richfaces.iterator.SimpleForEachIterator;
-
-import com.sun.facelets.FaceletContext;
-import com.sun.facelets.tag.TagAttribute;
-import com.sun.facelets.tag.jsf.ComponentConfig;
-
-
-/**
- * TODO Class description goes here.
- *
- * @author "Andrey Markavtsov"
- *
- */
-public class ColumnsTagHandler extends ComponentHandler {
-
- /** value attribute */
- private TagAttribute value;
-
- /** end attribute */
- private TagAttribute columns;
-
- /** begin attribute */
- private TagAttribute begin;
-
- /** var attribute */
- private TagAttribute var;
-
- /** index attribute */
- private TagAttribute index;
-
- /** end attribute */
- private TagAttribute end;
-
- /** Iterator for columns's tag value attribute */
- protected ForEachIterator items; // our 'digested' items
-
- /** 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.
- *
- * @param config
- */
- public ColumnsTagHandler(ComponentConfig config) {
- super(config);
-
- }
-
- /**
- * Extracts tags attributes values
- */
- private void initVariables(FaceletContext ctx) {
- initColumnsCount(ctx);
- initIndex(ctx);
- initVar(ctx);
- initBegin(ctx);
- initEnd(ctx);
- }
-
- /**
- * Method prepares all we need for starting of tag rendering
- *
- * @throws JspTagException
- */
- private void prepare(FaceletContext ctx) {
-
- initVariables(ctx);
-
- try {
-
- this.value = getAttribute("value");
-
- // produce the right sort of ForEachIterator
- if (value != null) {
- // If this is a deferred expression, make a note and get
- // the 'items' instance.
-
- rawItems = value.getObject(ctx);
-
- // extract an iterator over the 'items' we've got
- items = SimpleForEachIterator
- .supportedTypeForEachIterator(rawItems);
- } else {
- // no 'items', so use 'begin' and 'end'
- items = SimpleForEachIterator
- .beginEndForEachIterator(_columns - 1);
- }
- } catch (Exception e) {
- // TODO: handle exception
- }
-
- correctFirst(ctx);
- }
-
- /**
- * Inits first iteration item
- */
- private void correctFirst(FaceletContext ctx) {
- if (items != null) {
- if (_begin > 0 && (_index < _begin)) {
- while ((_index < _begin && hasNext())) {
- next(ctx);
- }
- if (!hasNext()) {
- _index = 0;
- }
- }
- }
- }
-
- /**
- * Return true if we didn't complete column's count
- *
- * @return
- * @throws JspTagException
- */
- private boolean hasNext() {
- try {
- if (_end != 0) {
- return (_index < _end) ? items.hasNext() : false;
- } else {
- return items.hasNext();
- }
- } catch (Exception e) {
- return false;
- }
-
- }
-
- /**
- * Iterate to next column
- *
- * @return
- * @throws JspTagException
- */
- private Object next(FaceletContext ctx) {
- try {
- Object o = items.next();
- _index++;
- return o;
- } catch (Exception e) {
- return null;
- }
- }
-
- /**
- * Extracts integer value from end attr
- */
- private void initColumnsCount(FaceletContext ctx) {
- 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
- }
- } catch (Exception e) {
- _columns = 0;
- }
- } else {
- _columns = 0;
- }
- }
-
- /**
- * Extracts integer value from begin attr
- */
- private void initBegin(FaceletContext ctx) {
- this.begin = getAttribute("begin");
- if (begin != null) {
- try {
- _begin = Integer.parseInt((String) begin.getObject(ctx));
- _begin--;
- if (_begin < 0) {
- _begin = 0; // If end is negative set up zero
- }
- } catch (Exception e) {
- _begin = 0;
- }
- } else {
- _begin = 0;
- }
- }
-
- /**
- * Extracts integer value from end attr
- */
- private void initEnd(FaceletContext ctx) {
- this.end = getAttribute("end");
- if (end != null) {
- try {
- _end = Integer.parseInt((String) end.getObject(ctx));
- if (_end < 0) {
- _end = 0; // If end is negative set up zero
- }
- } catch (Exception e) {
- _end = 0;
- }
- } else {
- _end = 0;
- }
- }
-
- /**
- * Extracts string value from var attr
- */
- private void initVar(FaceletContext ctx) {
- this.var = getAttribute("var");
- if (var != null) {
- try {
- _itemId = (String) var.getObject(ctx);
- } catch (ClassCastException e) {
- _itemId = null;
- }
-
- }
- }
-
- /**
- * Extracts string value from index attr
- */
- private void initIndex(FaceletContext ctx) {
- this.index = getAttribute("index");
- if (index != null) {
- try {
- _indexId = (String) index.getObject(ctx);
- } catch (ClassCastException e) {
- _indexId = null;
- }
-
- }
- }
-
- /* (non-Javadoc)
- * @see org.richfaces.taglib.ComponentHandler#apply(com.sun.facelets.FaceletContext,
javax.faces.component.UIComponent)
- */
- @Override
- public void apply(FaceletContext ctx, UIComponent parent)
- throws IOException, FacesException, ELException {
-
- prepare(ctx); // prepare data
-
- try {
- while (hasNext()) { // for each
- exposeVariables(ctx, _index);
- super.apply(ctx, parent);
- next(ctx);
- }
- } catch (Exception e) {
- // TODO: handle exception
- } finally {
- release();
- unExposeVariables(ctx);
- }
-
- }
-
- protected void applyNextHandler(FaceletContext ctx, UIComponent c)
- throws IOException, FacesException, ELException {
- // TODO Auto-generated method stub
- super.applyNextHandler(ctx, c);
- }
-
-
- /**
- * Sets page request variables
- *
- * @throws JspTagException
- */
- private void exposeVariables(FaceletContext ctx, int k) {
-
- VariableMapper vm = ctx.getVariableMapper();
-
- if (_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);
- }
- }
-
- }
-
- // Set up index variable
-
- if (_indexId != null) {
- if (vm != null) {
- ValueExpression ve = new IteratedIndexExpression(k);
- vm.setVariable(_indexId, ve);
- }
-
- }
-
- }
-
- /**
- * Removes page attributes that we have exposed and, if applicable, restores
- * them to their prior values (and scopes).
- */
- private void unExposeVariables(FaceletContext ctx) {
- VariableMapper vm = ctx.getVariableMapper();
- // "nested" variables are now simply removed
- if (_itemId != null) {
- if (vm != null)
- vm.setVariable(_itemId, null);
- }
- if (_indexId != null) {
- if (vm != null)
- vm.setVariable(_indexId, null);
- }
- }
-
- /**
- * Return expression for page variables
- *
- * @param expr
- * @return
- */
- private ValueExpression getVarExpression(FaceletContext ctx,
- ValueExpression expr, int k) {
- Object o = expr.getValue(ctx.getFacesContext().getELContext());
- if (o.getClass().isArray() || o instanceof List) {
- return new IndexedValueExpression(expr, k);
- }
-
- if (o instanceof Collection || o instanceof Iterator
- || o instanceof Enumeration || o instanceof Map
- || o instanceof String) {
-
- if (iteratedExpression == null) {
- iteratedExpression = new IteratedExpression(expr, ",");
- }
- return new IteratedValueExpression(iteratedExpression, k);
- }
-
- throw new ELException("FOREACH_BAD_ITEMS");
- }
-
- /**
- * Release iteration variables
- */
- private void release() {
- this.items = null;
- this._index = 0;
- }
-}
Deleted: trunk/ui/columns/src/main/java/org/richfaces/taglib/ComponentHandler.java
===================================================================
--- trunk/ui/columns/src/main/java/org/richfaces/taglib/ComponentHandler.java 2008-03-11
14:02:16 UTC (rev 6690)
+++ trunk/ui/columns/src/main/java/org/richfaces/taglib/ComponentHandler.java 2008-03-11
14:03:41 UTC (rev 6691)
@@ -1,284 +0,0 @@
-/**
- * Licensed under the Common Development and Distribution License,
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *
http://www.sun.com/cddl/
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- * implied. See the License for the specific language governing
- * permissions and limitations under the License.
- */
-
-package org.richfaces.taglib;
-
-import java.io.IOException;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import javax.el.ELException;
-import javax.el.MethodExpression;
-import javax.el.ValueExpression;
-import javax.faces.FacesException;
-import javax.faces.application.Application;
-import javax.faces.component.ActionSource;
-import javax.faces.component.ActionSource2;
-import javax.faces.component.EditableValueHolder;
-import javax.faces.component.UIComponent;
-import javax.faces.component.UIViewRoot;
-import javax.faces.component.ValueHolder;
-import javax.faces.context.FacesContext;
-import javax.faces.convert.Converter;
-import javax.faces.el.ValueBinding;
-import javax.faces.event.ActionEvent;
-import javax.faces.event.MethodExpressionActionListener;
-import javax.faces.event.MethodExpressionValueChangeListener;
-import javax.faces.event.ValueChangeEvent;
-import javax.faces.validator.MethodExpressionValidator;
-
-import com.sun.facelets.FaceletContext;
-import com.sun.facelets.el.ELAdaptor;
-import com.sun.facelets.el.LegacyMethodBinding;
-import com.sun.facelets.el.LegacyValueBinding;
-import com.sun.facelets.tag.MetaTagHandler;
-import com.sun.facelets.tag.TagAttribute;
-import com.sun.facelets.tag.Metadata;
-import com.sun.facelets.tag.TagException;
-import com.sun.facelets.tag.TagHandler;
-import com.sun.facelets.tag.MetaRuleset;
-import com.sun.facelets.tag.jsf.ComponentConfig;
-import com.sun.facelets.tag.jsf.ComponentSupport;
-import com.sun.facelets.tag.jsf.EditableValueHolderRule;
-import com.sun.facelets.util.FacesAPI;
-
-/**
- * Implementation of the tag logic used in the JSF specification. This is your
- * golden hammer for wiring UIComponents to Facelets.
- *
- * @author Jacob Hookom
- * @version $Id: ComponentHandler.java,v 1.12 2006/01/11 05:40:56 jhook Exp $
- */
-public class ComponentHandler extends MetaTagHandler {
-
- private final static Logger log = Logger
- .getLogger("facelets.tag.component");
-
- private final TagAttribute binding;
-
- private final String componentType;
-
- private final TagAttribute id;
-
- private final String rendererType;
-
- public ComponentHandler(ComponentConfig config) {
- super(config);
- this.componentType = config.getComponentType();
- this.rendererType = config.getRendererType();
- this.id = this.getAttribute("id");
- this.binding = this.getAttribute("binding");
- }
-
- /**
- * Method handles UIComponent tree creation in accordance with the JSF 1.2
- * spec.
- * <ol>
- * <li>First determines this UIComponent's id by calling
- * {@link #getId(FaceletContext) getId(FaceletContext)}.</li>
- * <li>Search the parent for an existing UIComponent of the id we just
- * grabbed</li>
- * <li>If found, {@link #markForDeletion(UIComponent) mark} its children
- * for deletion.</li>
- * <li>If <i>not</i> found, call
- * {@link #createComponent(FaceletContext) createComponent}.
- * <ol>
- * <li>Only here do we apply
- * {@link ObjectHandler#setAttributes(FaceletContext, Object) attributes}</li>
- * <li>Set the UIComponent's id</li>
- * <li>Set the RendererType of this instance</li>
- * </ol>
- * </li>
- * <li>Now apply the nextHandler, passing the UIComponent we've
- * created/found.</li>
- * <li>Now add the UIComponent to the passed parent</li>
- * <li>Lastly, if the UIComponent already existed (found), then
- * {@link #finalizeForDeletion(UIComponent) finalize} for deletion.</li>
- * </ol>
- *
- * @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext,
- * javax.faces.component.UIComponent)
- *
- * @throws TagException
- * if the UIComponent parent is null
- */
- public void apply(FaceletContext ctx, UIComponent parent)
- throws IOException, FacesException, ELException {
- // make sure our parent is not null
- if (parent == null) {
- throw new TagException(this.tag, "Parent UIComponent was null");
- }
-
- // our id
- String id = ctx.generateUniqueId(this.tagId);
-
- // grab our component
- UIComponent c = ComponentSupport.findChildByTagId(parent, id);
- boolean componentFound = false;
- if (c != null) {
- componentFound = true;
- // mark all children for cleaning
- if (log.isLoggable(Level.FINE)) {
- log.fine(this.tag
- + " Component["+id+"] Found, marking children for
cleanup");
- }
- ComponentSupport.markForDeletion(c);
- } else {
- c = this.createComponent(ctx);
- if (log.isLoggable(Level.FINE)) {
- log.fine(this.tag + " Component["+id+"] Created: "
- + c.getClass().getName());
- }
- this.setAttributes(ctx, c);
-
- // mark it owned by a facelet instance
- c.getAttributes().put(ComponentSupport.MARK_CREATED, id);
-
- // assign our unique id
- if (this.id != null) {
- c.setId(ctx.generateUniqueId(this.id.getValue(ctx)));
- } else {
- UIViewRoot root = ComponentSupport.getViewRoot(ctx, parent);
- if (root != null) {
- c.setId(root.createUniqueId());
- }
- }
-
- if (this.rendererType != null) {
- c.setRendererType(this.rendererType);
- }
-
- // hook method
- this.onComponentCreated(ctx, c, parent);
- }
-
- // first allow c to get populated
- this.nextHandler.apply(ctx, c);
-
- // finish cleaning up orphaned children
- if (componentFound) {
- ComponentSupport.finalizeForDeletion(c);
- parent.getChildren().remove(c);
- }
-
- // add to the tree afterwards
- // this allows children to determine if it's
- // been part of the tree or not yet
- parent.getChildren().add(c);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
com.sun.facelets.tag.jsf.ComponentHandler#applyNextHandler(com.sun.facelets.FaceletContext,
- * javax.faces.component.UIComponent)
- */
-
- protected void applyNextHandler(FaceletContext ctx, UIComponent c)
- throws IOException, FacesException, ELException {
-
- }
-
- /**
- * If the binding attribute was specified, use that in conjuction with our
- * componentType String variable to call createComponent on the Application,
- * otherwise just pass the componentType String.
- * <p />
- * If the binding was used, then set the ValueExpression "binding" on the
- * created UIComponent.
- *
- * @see Application#createComponent(javax.faces.el.ValueBinding,
- * javax.faces.context.FacesContext, java.lang.String)
- * @see Application#createComponent(java.lang.String)
- * @param ctx
- * FaceletContext to use in creating a component
- * @return
- */
- protected UIComponent createComponent(FaceletContext ctx) {
- UIComponent c = null;
- FacesContext faces = ctx.getFacesContext();
- Application app = faces.getApplication();
- if (this.binding != null) {
- ValueExpression ve = this.binding.getValueExpression(ctx,
- Object.class);
- if (FacesAPI.getVersion() >= 12) {
- c = app.createComponent(ve, faces, this.componentType);
- if (c != null) {
- // Make sure the component supports 1.2
- if (FacesAPI.getComponentVersion(c) >= 12) {
- c.setValueExpression("binding", ve);
- } else {
- ValueBinding vb = new LegacyValueBinding(ve);
- c.setValueBinding("binding", vb);
- }
-
- }
- } else {
- ValueBinding vb = new LegacyValueBinding(ve);
- c = app.createComponent(vb, faces, this.componentType);
- if (c != null) {
- c.setValueBinding("binding", vb);
- }
- }
- } else {
- c = app.createComponent(this.componentType);
- }
- return c;
- }
-
- /**
- * If the id TagAttribute was specified, get it's value, otherwise generate
- * a unique id from our tagId.
- *
- * @see TagAttribute#getValue(FaceletContext)
- * @param ctx
- * FaceletContext to use
- * @return what should be a unique Id
- */
- protected String getId(FaceletContext ctx) {
- if (this.id != null) {
- return this.id.getValue(ctx);
- }
- return ctx.generateUniqueId(this.tagId);
- }
-
- /* (non-Javadoc)
- * @see com.sun.facelets.tag.MetaTagHandler#createMetaRuleset(java.lang.Class)
- */
- protected MetaRuleset createMetaRuleset(Class type) {
- MetaRuleset ruleset = super.createMetaRuleset(type);
- ruleset.ignore("var");
- ruleset.ignore("index");
- ruleset.ignore("columns");
- ruleset.ignore("begin");
- ruleset.ignore("end");
- ruleset.ignore("value");
- return ruleset;
-
- }
-
- /**
- * A hook method for allowing developers to do additional processing once Facelets
- * creates the component. The 'setAttributes' method is still perferred, but
this
- * method will provide the parent UIComponent before it's been added to the tree
and
- * before any children have been added to the newly created UIComponent.
- *
- * @param ctx
- * @param c
- * @param parent
- */
- protected void onComponentCreated(FaceletContext ctx, UIComponent c, UIComponent
parent) {
- // do nothing
- }
-}
Added: trunk/ui/columns/src/main/java/org/richfaces/taglib/ComponentHandler.java
===================================================================
--- trunk/ui/columns/src/main/java/org/richfaces/taglib/ComponentHandler.java
(rev 0)
+++ trunk/ui/columns/src/main/java/org/richfaces/taglib/ComponentHandler.java 2008-03-11
14:03:41 UTC (rev 6691)
@@ -0,0 +1,401 @@
+/*
+ * ColumnsHandler.java Date created: 07.12.2007
+ * Last modified by: $Author$
+ * $Revision$ $Date$
+ */
+
+package org.richfaces.taglib;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.el.ELException;
+import javax.el.ValueExpression;
+import javax.el.VariableMapper;
+import javax.faces.FacesException;
+import javax.faces.component.UIComponent;
+import javax.servlet.jsp.JspTagException;
+
+import org.richfaces.iterator.ForEachIterator;
+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;
+
+
+
+/**
+ * TODO Class description goes here.
+ *
+ * @author "Andrey Markavtsov"
+ *
+ */
+public class ComponentHandler extends MetaTagHandler {
+
+ com.sun.facelets.tag.jsf.ComponentHandler handler;
+
+ /** value attribute */
+ private TagAttribute value;
+
+ /** end attribute */
+ private TagAttribute columns;
+
+ /** begin attribute */
+ private TagAttribute begin;
+
+ /** var attribute */
+ private TagAttribute var;
+
+ /** index attribute */
+ private TagAttribute index;
+
+ /** end attribute */
+ private TagAttribute end;
+
+ /** Iterator for columns's tag value attribute */
+ protected ForEachIterator items; // our 'digested' items
+
+ /** 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.
+ *
+ * @param config
+ */
+ public ComponentHandler(ComponentConfig config) {
+ super(config);
+ handler = new com.sun.facelets.tag.jsf.ComponentHandler(config);
+ }
+
+ /**
+ * Extracts tags attributes values
+ */
+ private void initVariables(FaceletContext ctx) {
+ initColumnsCount(ctx);
+ initIndex(ctx);
+ initVar(ctx);
+ initBegin(ctx);
+ initEnd(ctx);
+ }
+
+ /**
+ * Method prepares all we need for starting of tag rendering
+ *
+ * @throws JspTagException
+ */
+ private void prepare(FaceletContext ctx) {
+
+ initVariables(ctx);
+
+ try {
+
+ this.value = getAttribute("value");
+
+ // produce the right sort of ForEachIterator
+ if (value != null) {
+ // If this is a deferred expression, make a note and get
+ // the 'items' instance.
+
+ rawItems = value.getObject(ctx);
+
+ // extract an iterator over the 'items' we've got
+ items = SimpleForEachIterator
+ .supportedTypeForEachIterator(rawItems);
+ } else {
+ // no 'items', so use 'begin' and 'end'
+ items = SimpleForEachIterator
+ .beginEndForEachIterator(_columns - 1);
+ }
+ } catch (Exception e) {
+ // TODO: handle exception
+ }
+
+ correctFirst(ctx);
+ }
+
+ /**
+ * Inits first iteration item
+ */
+ private void correctFirst(FaceletContext ctx) {
+ if (items != null) {
+ if (_begin > 0 && (_index < _begin)) {
+ while ((_index < _begin && hasNext())) {
+ next(ctx);
+ }
+ if (!hasNext()) {
+ _index = 0;
+ }
+ }
+ }
+ }
+
+ /**
+ * Return true if we didn't complete column's count
+ *
+ * @return
+ * @throws JspTagException
+ */
+ private boolean hasNext() {
+ try {
+ if (_end != 0) {
+ return (_index < _end) ? items.hasNext() : false;
+ } else {
+ return items.hasNext();
+ }
+ } catch (Exception e) {
+ return false;
+ }
+
+ }
+
+ /**
+ * Iterate to next column
+ *
+ * @return
+ * @throws JspTagException
+ */
+ private Object next(FaceletContext ctx) {
+ try {
+ Object o = items.next();
+ _index++;
+ return o;
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ /**
+ * Extracts integer value from end attr
+ */
+ private void initColumnsCount(FaceletContext ctx) {
+ 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
+ }
+ } catch (Exception e) {
+ _columns = 0;
+ }
+ } else {
+ _columns = 0;
+ }
+ }
+
+ /**
+ * Extracts integer value from begin attr
+ */
+ private void initBegin(FaceletContext ctx) {
+ this.begin = getAttribute("begin");
+ if (begin != null) {
+ try {
+ _begin = Integer.parseInt((String) begin.getObject(ctx));
+ _begin--;
+ if (_begin < 0) {
+ _begin = 0; // If end is negative set up zero
+ }
+ } catch (Exception e) {
+ _begin = 0;
+ }
+ } else {
+ _begin = 0;
+ }
+ }
+
+ /**
+ * Extracts integer value from end attr
+ */
+ private void initEnd(FaceletContext ctx) {
+ this.end = getAttribute("end");
+ if (end != null) {
+ try {
+ _end = Integer.parseInt((String) end.getObject(ctx));
+ if (_end < 0) {
+ _end = 0; // If end is negative set up zero
+ }
+ } catch (Exception e) {
+ _end = 0;
+ }
+ } else {
+ _end = 0;
+ }
+ }
+
+ /**
+ * Extracts string value from var attr
+ */
+ private void initVar(FaceletContext ctx) {
+ this.var = getAttribute("var");
+ if (var != null) {
+ try {
+ _itemId = (String) var.getObject(ctx);
+ } catch (ClassCastException e) {
+ _itemId = null;
+ }
+
+ }
+ }
+
+ /**
+ * Extracts string value from index attr
+ */
+ private void initIndex(FaceletContext ctx) {
+ this.index = getAttribute("index");
+ if (index != null) {
+ try {
+ _indexId = (String) index.getObject(ctx);
+ } catch (ClassCastException e) {
+ _indexId = null;
+ }
+
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.richfaces.taglib.ComponentHandler#apply(com.sun.facelets.FaceletContext,
javax.faces.component.UIComponent)
+ */
+ //@Override
+ public void apply(FaceletContext ctx, UIComponent parent)
+ throws IOException, FacesException, ELException {
+
+ prepare(ctx); // prepare data
+
+ try {
+ while (hasNext()) { // for each
+ exposeVariables(ctx, _index);
+ //super.apply(ctx, parent);
+ handler.apply(ctx, parent);
+ next(ctx);
+ }
+ } catch (Exception e) {
+ // TODO: handle exception
+ } finally {
+ release();
+ unExposeVariables(ctx);
+ }
+
+ }
+
+ protected void applyNextHandler(FaceletContext ctx, UIComponent c)
+ throws IOException, FacesException, ELException {
+ // TODO Auto-generated method stub
+ //super.applyNextHandler(ctx, c);
+
+ }
+
+
+ /**
+ * Sets page request variables
+ *
+ * @throws JspTagException
+ */
+ private void exposeVariables(FaceletContext ctx, int k) {
+
+ VariableMapper vm = ctx.getVariableMapper();
+
+ if (_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);
+ }
+ }
+
+ }
+
+ // Set up index variable
+
+ if (_indexId != null) {
+ if (vm != null) {
+ ValueExpression ve = new IteratedIndexExpression(k);
+ vm.setVariable(_indexId, ve);
+ }
+
+ }
+
+ }
+
+ /**
+ * Removes page attributes that we have exposed and, if applicable, restores
+ * them to their prior values (and scopes).
+ */
+ private void unExposeVariables(FaceletContext ctx) {
+ VariableMapper vm = ctx.getVariableMapper();
+ // "nested" variables are now simply removed
+ if (_itemId != null) {
+ if (vm != null)
+ vm.setVariable(_itemId, null);
+ }
+ if (_indexId != null) {
+ if (vm != null)
+ vm.setVariable(_indexId, null);
+ }
+ }
+
+ /**
+ * Return expression for page variables
+ *
+ * @param expr
+ * @return
+ */
+ private ValueExpression getVarExpression(FaceletContext ctx,
+ ValueExpression expr, int k) {
+ Object o = expr.getValue(ctx.getFacesContext().getELContext());
+ if (o.getClass().isArray() || o instanceof List) {
+ return new IndexedValueExpression(expr, k);
+ }
+
+ if (o instanceof Collection || o instanceof Iterator
+ || o instanceof Enumeration || o instanceof Map
+ || o instanceof String) {
+
+ if (iteratedExpression == null) {
+ iteratedExpression = new IteratedExpression(expr, ",");
+ }
+ return new IteratedValueExpression(iteratedExpression, k);
+ }
+
+ throw new ELException("FOREACH_BAD_ITEMS");
+ }
+
+ /**
+ * Release iteration variables
+ */
+ private void release() {
+ this.items = null;
+ this._index = 0;
+ }
+
+}