001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.renderkit.dom_html_basic;
035:
036: import com.icesoft.faces.context.DOMContext;
037: import org.w3c.dom.Element;
038:
039: import javax.faces.component.UIComponent;
040: import javax.faces.component.html.HtmlPanelGrid;
041: import javax.faces.context.FacesContext;
042: import java.io.IOException;
043: import java.util.Iterator;
044:
045: public class GridRenderer extends DomBasicRenderer {
046:
047: public boolean getRendersChildren() {
048: return true;
049: }
050:
051: public void encodeBegin(FacesContext facesContext,
052: UIComponent uiComponent) throws IOException {
053: validateParameters(facesContext, uiComponent, null);
054: DOMContext domContext = DOMContext.attachDOMContext(
055: facesContext, uiComponent);
056: if (!domContext.isInitialized()) {
057: Element root = domContext.createElement("table");
058: domContext.setRootNode(root);
059: setRootElementId(facesContext, root, uiComponent);
060: PassThruAttributeRenderer.renderAttributes(facesContext,
061: uiComponent, null);
062: }
063: Element root = (Element) domContext.getRootNode();
064: String styleClass = ((HtmlPanelGrid) uiComponent)
065: .getStyleClass();
066: if (styleClass != null) {
067: root.setAttribute("class", styleClass);
068: }
069: }
070:
071: private void renderHeaderFacet(FacesContext facesContext,
072: UIComponent uiComponent, DOMContext domContext)
073: throws IOException {
074: Element root = (Element) domContext.getRootNode();
075: DOMContext.removeChildrenByTagName(root, "thead");
076: UIComponent headerFacet = getFacetByName(uiComponent, "header");
077: if (headerFacet != null && headerFacet.isRendered()) {
078: Element thead = domContext.createElement("thead");
079: Element tr = domContext.createElement("tr");
080: Element th = domContext.createElement("th");
081: root.appendChild(thead);
082: thead.appendChild(tr);
083: tr.appendChild(th);
084: String headerClassAttribute = ((HtmlPanelGrid) uiComponent)
085: .getHeaderClass();
086: if (headerClassAttribute != null) {
087: th.setAttribute("class", headerClassAttribute);
088: }
089: th.setAttribute("scope", "colgroup");
090: th.setAttribute("colspan", String
091: .valueOf(getConvertedColumnAttribute(uiComponent)));
092: domContext.setCursorParent(th);
093: domContext.streamWrite(facesContext, uiComponent,
094: domContext.getRootNode(), th);
095: encodeParentAndChildren(facesContext, headerFacet);
096: }
097: }
098:
099: private void renderFooterFacet(FacesContext facesContext,
100: UIComponent uiComponent, DOMContext domContext)
101: throws IOException {
102: Element root = (Element) domContext.getRootNode();
103: DOMContext.removeChildrenByTagName(root, "tfoot");
104: UIComponent footerFacet = getFacetByName(uiComponent, "footer");
105: if (footerFacet != null && footerFacet.isRendered()) {
106: Element tfoot = domContext.createElement("tfoot");
107: Element tr = domContext.createElement("tr");
108: Element td = domContext.createElement("td");
109: root.appendChild(tfoot);
110: tfoot.appendChild(tr);
111: tr.appendChild(td);
112: String footerClassAttribute = ((HtmlPanelGrid) uiComponent)
113: .getFooterClass();
114: if (footerClassAttribute != null) {
115: td.setAttribute("class", footerClassAttribute);
116: }
117: td.setAttribute("colspan", String
118: .valueOf(getConvertedColumnAttribute(uiComponent)));
119: domContext.setCursorParent(td);
120: domContext.streamWrite(facesContext, uiComponent,
121: domContext.getRootNode(), td);
122: encodeParentAndChildren(facesContext, footerFacet);
123: }
124: }
125:
126: public void encodeChildren(FacesContext facesContext,
127: UIComponent uiComponent) throws IOException {
128: validateParameters(facesContext, uiComponent, null);
129: DOMContext domContext = DOMContext.getDOMContext(facesContext,
130: uiComponent);
131: renderHeaderFacet(facesContext, uiComponent, domContext);
132: // remove previous children
133: Element root = (Element) domContext.getRootNode();
134: DOMContext.removeChildrenByTagName(root, "tbody");
135: Element tbody = domContext.createElement("tbody");
136: root.appendChild(tbody);
137: Element tr = null;
138:
139: // Render children inside the tbody element.
140: // Based on the value of the "columns" attribute, create a new row every
141: // time a columns-worth of children has been rendered.
142: // Children with attribute "rendered" == false are not rendered and do
143: // not occupy a table cell.
144: Iterator children = uiComponent.getChildren().iterator();
145: if (children != null) {
146: int numberOfColumns = getConvertedColumnAttribute(uiComponent);
147: int rowIndex = -1;// this initial value ensures zero-based indexing in ids
148: int columnIndex = numberOfColumns; // this initial value invoked initialization of first row
149: String columnStyleClasses[] = getColumnStyleClasses(uiComponent);
150: String rowStyleClasses[] = getRowStyles(uiComponent);
151: int columnStyleIndex = 0;
152: int rowStyleIndex = 0;
153: int numberOfColumnStyles = columnStyleClasses.length - 1;
154: int numberOfRowStyles = rowStyleClasses.length;
155: UIComponent facet = null;
156: while (children.hasNext()) {
157: UIComponent nextChild = (UIComponent) children.next();
158: if (!nextChild.isRendered()) {
159: continue;
160: }
161: // detect whether a new row is needed
162: if (columnIndex >= numberOfColumns) {
163: tr = domContext.createElement("tr");
164: tbody.appendChild(tr);
165: if (numberOfRowStyles > 0) {
166: tr.setAttribute("class",
167: rowStyleClasses[rowStyleIndex++]);
168: if (rowStyleIndex >= numberOfRowStyles) {
169: rowStyleIndex = 0;
170: }
171: }
172: columnStyleIndex = 0;
173: columnIndex = 0;
174: rowIndex++;
175: }
176: // create the td for this child
177: Element td = domContext.createElement("td");
178: tr.appendChild(td);
179: td.setAttribute("id", getIndexedClientId(facesContext,
180: uiComponent, columnIndex, rowIndex));
181:
182: writeColStyles(columnStyleClasses,
183: numberOfColumnStyles, columnStyleIndex, td,
184: columnIndex + 1, uiComponent);
185: if (++columnStyleIndex > numberOfColumnStyles) {
186: columnStyleIndex = 0;
187: }
188:
189: domContext.setCursorParent(td);
190: domContext.streamWrite(facesContext, uiComponent,
191: domContext.getRootNode(), td);
192: encodeParentAndChildren(facesContext, nextChild);
193: columnIndex++;
194: }
195: }
196: renderFooterFacet(facesContext, uiComponent, domContext);
197: domContext.stepOver();
198: domContext.streamWrite(facesContext, uiComponent);
199: }
200:
201: // this method is overridden in the subclass
202: public String[] getRowStyles(UIComponent uiComponent) {
203: return getRowStyleClasses(uiComponent);
204: }
205:
206: private String getIndexedClientId(FacesContext facesContext,
207: UIComponent uiComponent, int columnIndex, int rowIndex) {
208: return uiComponent.getClientId(facesContext) + "-" + rowIndex
209: + "-" + columnIndex;
210: }
211:
212: private int getConvertedColumnAttribute(UIComponent uiComponent) {
213: int convertedColumnAttribute = 1; // default
214: Object columnAttribute = uiComponent.getAttributes().get(
215: "columns");
216: int value;
217: if (columnAttribute != null
218: && columnAttribute instanceof Integer
219: && (value = ((Integer) columnAttribute).intValue()) > 0) {
220: convertedColumnAttribute = value;
221: }
222: return (convertedColumnAttribute);
223: }
224:
225: public void encodeEnd(FacesContext facesContext,
226: UIComponent uiComponent) throws IOException {
227: validateParameters(facesContext, uiComponent, null);
228: }
229:
230: // this method is overridden in the subclass
231: public void writeColStyles(String[] columnStyles,
232: int columnStylesMaxIndex, int columnStyleIndex, Element td,
233: int colNumber, UIComponent uiComponent) {
234: if (columnStyles.length > 0) {
235: if (columnStylesMaxIndex >= 0) {
236: td
237: .setAttribute("class",
238: columnStyles[columnStyleIndex]);
239: if (++columnStyleIndex > columnStylesMaxIndex) {
240: columnStyleIndex = 0;
241: }
242: }
243: }
244: }
245:
246: protected String getRowStyle(UIComponent uiComponent, String style) {
247: return style;
248: }
249:
250: }
|