001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2005 Danet GmbH (www.danet.de), BU BTS.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: HtmlPanelGridRenderer.java,v 1.3 2007/03/27 21:59:43 mlipp Exp $
021: *
022: * $Log: HtmlPanelGridRenderer.java,v $
023: * Revision 1.3 2007/03/27 21:59:43 mlipp
024: * Fixed lots of checkstyle warnings.
025: *
026: * Revision 1.2 2006/09/29 12:32:09 drmlipp
027: * Consistently using WfMOpen as projct name now.
028: *
029: * Revision 1.1 2006/09/14 22:06:30 mlipp
030: * Implemented extended panel grid.
031: *
032: */
033: package de.danet.an.util.jsf.taglib;
034:
035: import java.io.IOException;
036: import java.util.Iterator;
037:
038: import javax.faces.component.UIComponent;
039: import javax.faces.context.FacesContext;
040: import javax.faces.context.ResponseWriter;
041:
042: import org.apache.myfaces.renderkit.html.ext.HtmlGridRenderer;
043: import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
044: import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
045: import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
046: import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
047: import org.apache.myfaces.shared_tomahawk.util.ArrayUtils;
048: import org.apache.myfaces.shared_tomahawk.util.StringUtils;
049:
050: /**
051: * This class provides the renderer for HtmlPanelGrid.
052: *
053: * @author Michael Lipp
054: *
055: */
056: public class HtmlPanelGridRenderer extends HtmlGridRenderer {
057:
058: private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
059: .getLog(HtmlPanelGridRenderer.class);
060:
061: /**
062: * Render the children.
063: * @param context the faces context
064: * @param writer the response writer
065: * @param component the UI component
066: * @param columns the columns
067: * @throws IOException if an I/O error occurs
068: */
069: protected void renderChildren(FacesContext context,
070: ResponseWriter writer, UIComponent component, int columns)
071: throws IOException {
072: writer.startElement(HTML.TBODY_ELEM, component);
073:
074: String columnClasses;
075: String rowClasses;
076: String cellAlign = null;
077: String cellVAlign = null;
078: if (component instanceof HtmlPanelGrid) {
079: columnClasses = ((HtmlPanelGrid) component)
080: .getColumnClasses();
081: rowClasses = ((HtmlPanelGrid) component).getRowClasses();
082: cellAlign = ((HtmlPanelGrid) component).getCellAlign();
083: cellVAlign = ((HtmlPanelGrid) component).getCellVAlign();
084: } else {
085: columnClasses = (String) component
086: .getAttributes()
087: .get(
088: org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr.COLUMN_CLASSES_ATTR);
089: rowClasses = (String) component.getAttributes().get(
090: JSFAttr.ROW_CLASSES_ATTR);
091: }
092: if (cellAlign != null) {
093: writer.writeAttribute("align", cellAlign, null);
094: }
095: if (cellVAlign != null) {
096: writer.writeAttribute("valign", cellVAlign, null);
097: }
098:
099: String[] columnClassesArray = (columnClasses == null) ? ArrayUtils.EMPTY_STRING_ARRAY
100: : StringUtils.trim(StringUtils.splitShortString(
101: columnClasses, ','));
102: int columnClassesCount = columnClassesArray.length;
103:
104: String[] rowClassesArray = (rowClasses == null) ? org.apache.myfaces.shared_tomahawk.util.ArrayUtils.EMPTY_STRING_ARRAY
105: : StringUtils.trim(StringUtils.splitShortString(
106: rowClasses, ','));
107: int rowClassesCount = rowClassesArray.length;
108:
109: int childCount = getChildCount(component);
110: if (childCount > 0) {
111: int columnIndex = 0;
112: int rowClassIndex = 0;
113: boolean rowStarted = false;
114: for (Iterator it = getChildren(component).iterator(); it
115: .hasNext();) {
116: UIComponent child = (UIComponent) it.next();
117: if (child.isRendered()) {
118: if (columnIndex == 0) {
119: // start of new/next row
120: if (rowStarted) {
121: // do we have to close the last row?
122: writer.endElement(HTML.TR_ELEM);
123: HtmlRendererUtils
124: .writePrettyLineSeparator(context);
125: }
126: writer.startElement(HTML.TR_ELEM, component);
127: if (rowClassIndex < rowClassesCount) {
128: writer.writeAttribute(HTML.CLASS_ATTR,
129: rowClassesArray[rowClassIndex],
130: null);
131: }
132: rowStarted = true;
133: rowClassIndex++;
134: if (rowClassIndex == rowClassesCount) {
135: rowClassIndex = 0;
136: }
137: }
138:
139: writer.startElement(HTML.TD_ELEM, component);
140: if (columnIndex < columnClassesCount) {
141: writer.writeAttribute(HTML.CLASS_ATTR,
142: columnClassesArray[columnIndex], null);
143: }
144: RendererUtils.renderChild(context, child);
145: writer.endElement(HTML.TD_ELEM);
146:
147: columnIndex++;
148: if (columnIndex >= columns) {
149: columnIndex = 0;
150: }
151: }
152: }
153:
154: if (rowStarted) {
155: if (columnIndex > 0) {
156: if (logger.isWarnEnabled()) {
157: logger
158: .warn("PanelGrid "
159: + component
160: .getClientId(context)
161: + " has not enough children. Child count should "
162: + "be a multiple of the columns attribute.");
163: }
164: //Render empty columns, so that table is correct
165: for (; columnIndex < columns; columnIndex++) {
166: writer.startElement(HTML.TD_ELEM, component);
167: if (columnIndex < columnClassesCount) {
168: writer.writeAttribute(HTML.CLASS_ATTR,
169: columnClassesArray[columnIndex],
170: null);
171: }
172: writer.endElement(HTML.TD_ELEM);
173: }
174: }
175: writer.endElement(HTML.TR_ELEM);
176: HtmlRendererUtils.writePrettyLineSeparator(context);
177: }
178: }
179:
180: writer.endElement(HTML.TBODY_ELEM);
181: }
182:
183: }
|