001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/jsf/tags/sakai_2-4-1/widgets/src/java/org/sakaiproject/jsf/renderer/DynaRendererBase.java $
003: * $Id: DynaRendererBase.java 9278 2006-05-10 23:29:21Z ray@media.berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.jsf.renderer;
021:
022: import java.io.IOException;
023: import java.util.Iterator;
024: import javax.faces.component.UIColumn;
025: import javax.faces.component.UIComponent;
026: import javax.faces.component.UIData;
027: import javax.faces.context.FacesContext;
028: import javax.faces.context.ResponseWriter;
029: import javax.faces.render.Renderer;
030:
031: import org.sakaiproject.jsf.component.MultiColumnComponent;
032: import org.sakaiproject.jsf.util.RendererUtil;
033:
034: public abstract class DynaRendererBase extends Renderer {
035:
036: public DynaRendererBase() {
037: super ();
038: }
039:
040: abstract public void encodeBegin(FacesContext context,
041: UIComponent component) throws IOException;
042:
043: abstract public void encodeEnd(FacesContext context,
044: UIComponent component) throws IOException;
045:
046: /**
047: * <p>Faces render output method .</p>
048: * <p>Encode children column and multicolumn</p>
049: *
050: * @param context <code>FacesContext</code> for the current request
051: * @param component <code>UIComponent</code> being rendered
052: *
053: * @throws IOException if an input/output error occurs
054: */
055: public void encodeChildren(FacesContext context,
056: UIComponent component) throws IOException {
057: if (!component.isRendered()) {
058: return;
059: }
060:
061: renderData(context, component);
062: }
063:
064: /**
065: * This component renders its children
066: * @return true
067: */
068: public boolean getRendersChildren() {
069: return true;
070: }
071:
072: /**
073: * This is an UIData type component.
074: * @param component
075: * @return true if UIData
076: */
077: public boolean supportsComponentType(UIComponent component) {
078: return (component instanceof UIData);
079: }
080:
081: /**
082: * Core workhouse method of the dynamic renderers.
083: * @param context FacesContext
084: * @param component UIComponent
085: * @throws IOException
086: */
087: protected void renderData(FacesContext context,
088: UIComponent component) throws IOException {
089: boolean multiColumn = component instanceof MultiColumnComponent;
090: ResponseWriter writer = context.getResponseWriter();
091:
092: UIData data = (UIData) component;
093:
094: int first = data.getFirst();
095: int rows = data.getRows();
096:
097: for (int i = first, n = 0; n < rows; i++, n++) {
098: data.setRowIndex(i);
099: if (!data.isRowAvailable()) {
100: break;
101: }
102: ////////////////////////////////////
103: // TR
104: ////////////////////////////////////
105: writer.startElement("tr", data);
106:
107: Iterator iter = data.getChildren().iterator();
108: while (iter.hasNext()) {
109: UIComponent child = (UIComponent) iter.next();
110: System.out.println("child.getClass().getName()="
111: + child.getClass().getName());
112:
113: // we skip if children are not of the type we want, we can add tests as needed
114: // if (!(child instanceof UIColumn) && !(child instanceof MultiColumnComponent))
115: // {
116: // continue;
117: // }
118:
119: if (child instanceof UIColumn) {
120: writer.startElement("td", child);
121: writer.write("debug UIColumn");
122: RendererUtil.encodeRecursive(context, child);
123: writer.endElement("td");
124: } else if (child instanceof UIData) {
125: writer.write("debug UIData");
126: child.encodeBegin(context);
127: child.encodeChildren(context);
128: child.encodeEnd(context);
129:
130: // RendererUtil.encodeRecursive(context, child);
131:
132: // UIData multiData = (UIData) child;
133: // int mFirst = multiData.getFirst();
134: // int mRows = multiData.getRows();
135: // String value = RendererUtil.getAttribute(context, )
136: // mFirst=0; mRows=2;
137: // mFirst=0; mRows=0;
138: // System.out.println("mFirst="+mFirst);
139: // System.out.println("mRows="+mRows);
140: // for (int j = mFirst, m = 0; m < mRows; j++, m++)
141: // {
142: // System.out.println("j="+j);
143: // System.out.println("m="+m);
144: // System.out.println("data.isRowAvailable()="+data.isRowAvailable());
145: // data.setRowIndex(j);
146: // if (!data.isRowAvailable())
147: // {
148: // break;
149: // }
150: // Iterator multIter = multiData.getChildren().iterator();
151: // while (multIter.hasNext()) {
152: // UIComponent multiChild = (UIComponent) multIter.next();
153: //
154: // if (multiChild instanceof UIColumn)
155: // {
156: // writer.startElement("td", multiChild);
157: // RendererUtil.encodeRecursive(context, multiChild);
158: // writer.endElement("td");
159: // }
160: // }
161: // }
162: }
163:
164: // ////////////////////////////////////
165: // // TD
166: // ////////////////////////////////////
167: // if (child instanceof UIColumn) writer.startElement("td", child);
168: // RendererUtil.encodeRecursive(context, child);
169: // ////////////////////////////////////
170: // // /TD
171: // ////////////////////////////////////
172: // if (child instanceof UIColumn) writer.endElement("td");
173:
174: // if (child instanceof UIColumn)
175: // {
176: // ////////////////////////////////////
177: // // TD
178: // ////////////////////////////////////
179: // writer.startElement("td", child);
180: // RendererUtil.encodeRecursive(context, child);
181: // ////////////////////////////////////
182: // // /TD
183: // ////////////////////////////////////
184: // writer.endElement("td");
185: // }
186: // else if (child instanceof MultiColumnComponent
187: // && !multiColumn) // cannot nest MultiColumnComponents!
188: // {
189: // renderData(context, child);
190: // }
191: }
192: ////////////////////////////////////
193: // /TR
194: ////////////////////////////////////
195: writer.endElement("tr");
196: writer.write("\n");
197: }
198: }
199:
200: }
|