001: /**********************************************************************************
002: *
003: * $Id: IteratorComponent.java 10817 2006-06-16 20:52:56Z lance@indiana.edu $
004: *
005: ***********************************************************************************
006: *
007: * Copyright (c) 2005 The Regents of the University of California, The MIT Corporation
008: *
009: * Licensed under the Educational Community License, Version 1.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.opensource.org/licenses/ecl1.php
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: *
021: **********************************************************************************/package org.sakaiproject.tool.gradebook.jsf.iterator;
022:
023: import java.io.IOException;
024: import java.util.Collection;
025: import java.util.Iterator;
026: import java.util.Map;
027:
028: import javax.faces.component.NamingContainer;
029: import javax.faces.component.UIComponent;
030: import javax.faces.component.UIComponentBase;
031: import javax.faces.context.FacesContext;
032: import javax.faces.el.ValueBinding;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: /**
038: * A simple looping component which encodes all its children for every record in
039: * the input collection.
040: *
041: * <gbx:iterator value="#{assignmentDetailsBean.scoreRows}" var="scoreRow" rowIndexVar="scoreRowPos">
042: * ...
043: * </gbx:iterator>
044: */
045:
046: public class IteratorComponent extends UIComponentBase implements
047: NamingContainer {
048: private static final Log log = LogFactory
049: .getLog(IteratorComponent.class);
050:
051: public final static String COMPONENT_TYPE = "org.sakaiproject.tool.gradebook.jsf.iterator";
052: public final static String COMPONENT_FAMILY = "javax.faces.Data";
053:
054: private Object value = null;
055: private String var = null;
056: private String rowIndexVar = null;
057:
058: public void encodeChildren(FacesContext context) throws IOException {
059: if (!isRendered()) {
060: return;
061: }
062:
063: Collection dataModel = getDataModel();
064: if (dataModel != null) {
065: Map requestMap = context.getExternalContext()
066: .getRequestMap();
067: int rowIndex = 0;
068: for (Iterator iter = dataModel.iterator(); iter.hasNext(); rowIndex++) {
069: Object varObject = iter.next();
070: if (var != null) {
071: if (varObject != null) {
072: requestMap.put(var, varObject);
073: } else {
074: requestMap.remove(var);
075: }
076: }
077: if (rowIndexVar != null) {
078: requestMap.put(rowIndexVar, new Integer(rowIndex));
079: }
080: renderRowChildren(context);
081: }
082: if (var != null) {
083: requestMap.remove(var);
084: }
085: if (rowIndexVar != null) {
086: requestMap.remove(rowIndexVar);
087: }
088: }
089: }
090:
091: /**
092:
093: * Subclasses can decorate the children as they see fit.
094: */
095: protected void renderRowChildren(FacesContext context)
096: throws IOException {
097: for (Iterator iter = getChildren().iterator(); iter.hasNext();) {
098: encodeRecursive(context, (UIComponent) iter.next());
099: }
100: }
101:
102: protected void encodeRecursive(FacesContext context,
103: UIComponent component) throws IOException {
104: if (component.isRendered()) {
105: component.encodeBegin(context);
106: if (component.getRendersChildren()) {
107: component.encodeChildren(context);
108: } else {
109: for (Iterator iter = component.getChildren().iterator(); iter
110: .hasNext();) {
111: encodeRecursive(context, (UIComponent) iter.next());
112: }
113: }
114: component.encodeEnd(context);
115: }
116: }
117:
118: private Collection getDataModel() {
119: Collection dataModel = null;
120: Object val = getValue();
121: if (val != null) {
122: if (val instanceof Collection) {
123: dataModel = (Collection) val;
124: } else {
125: if (log.isDebugEnabled())
126: log.debug("value is not a Collection: " + val);
127: }
128: }
129: return dataModel;
130: }
131:
132: public void encodeBegin(FacesContext context) throws IOException {
133: }
134:
135: public void encodeEnd(FacesContext context) throws IOException {
136: return;
137: }
138:
139: public void decode(FacesContext context) {
140: return;
141: }
142:
143: public boolean getRendersChildren() {
144: return true;
145: }
146:
147: public void setValueBinding(String name, ValueBinding binding) {
148: if ("var".equals(name) || "rowIndexVar".equals(name)) {
149: throw new IllegalArgumentException();
150: }
151: super .setValueBinding(name, binding);
152: }
153:
154: protected Object getFieldOrBinding(Object field, String bindingName) {
155: Object retVal = null;
156: if (field != null) {
157: retVal = field;
158: } else {
159: ValueBinding binding = getValueBinding(bindingName);
160: if (binding != null) {
161: retVal = binding.getValue(getFacesContext());
162: }
163: }
164: return retVal;
165: }
166:
167: public Object getValue() {
168: return getFieldOrBinding(value, "value");
169: }
170:
171: public void setValue(Object value) {
172: this .value = value;
173: }
174:
175: public String getVar() {
176: return var;
177: }
178:
179: public void setVar(String var) {
180: this .var = var;
181: }
182:
183: public void setRowIndexVar(String rowIndexVar) {
184: this .rowIndexVar = rowIndexVar;
185: }
186:
187: public Object saveState(FacesContext context) {
188: Object values[] = new Object[4];
189: values[0] = super .saveState(context);
190: values[1] = value;
191: values[2] = var;
192: values[3] = rowIndexVar;
193: return values;
194: }
195:
196: public void restoreState(FacesContext context, Object state) {
197: Object values[] = (Object[]) state;
198: super .restoreState(context, values[0]);
199: value = values[1];
200: var = (String) values[2];
201: rowIndexVar = (String) values[3];
202: }
203:
204: public String getFamily() {
205: return COMPONENT_FAMILY;
206: }
207:
208: }
|