📄 htmltablerendererbase.java
字号:
/* * Copyright 2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * 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.apache.myfaces.renderkit.html;import org.apache.myfaces.renderkit.JSFAttr;import org.apache.myfaces.renderkit.RendererUtils;import org.apache.myfaces.util.ArrayUtils;import org.apache.myfaces.util.StringUtils;import org.apache.myfaces.component.UIColumns;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import javax.faces.component.UIColumn;import javax.faces.component.UIComponent;import javax.faces.component.UIData;import javax.faces.component.html.HtmlDataTable;import javax.faces.context.FacesContext;import javax.faces.context.ResponseWriter;import java.io.IOException;import java.util.Iterator;import java.util.List;/** * @author Thomas Spiegl (latest modification by $Author: matzew $) * @version $Revision: 1.9 $ $Date: 2005/03/29 11:40:50 $ * * * $Log: HtmlTableRendererBase.java,v $ * Revision 1.9 2005/03/29 11:40:50 matzew * added new crosstable component (x:columns). Contributed by Mathias Broekelmann * * Revision 1.8 2005/02/11 16:03:00 mmarinschek * solve bug in tabbed panel when datatable was displayed not on tab, but at the bottom of the datatable... * * Revision 1.7 2005/01/19 11:49:20 matzew * MYFACES-83. Refactored HtmlTableRendererBase supported by "power-user" Heath Borders-Wing * * Revision 1.6 2004/12/23 13:03:09 mmarinschek * id's not rendered (or not conditionally rendered); changes in jslistener to support both ie and firefox now * * Revision 1.5 2004/11/26 12:14:10 oros * MYFACES-8: applied tree table patch by David Le Strat * * */public class HtmlTableRendererBase extends HtmlRenderer{ /** Header facet name. */ protected static final String HEADER_FACET_NAME = "header"; /** Footer facet name. */ protected static final String FOOTER_FACET_NAME = "footer"; /** The logger. */ private static final Log log = LogFactory.getLog(HtmlTableRendererBase.class); /** * @see javax.faces.render.Renderer#getRendersChildren() */ public boolean getRendersChildren() { return true; } /** * @see javax.faces.render.Renderer#encodeBegin(FacesContext, UIComponent) */ public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException { RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class); ResponseWriter writer = facesContext.getResponseWriter(); beforeTable(facesContext, (UIData) uiComponent); HtmlRendererUtils.writePrettyLineSeparator(facesContext); writer.startElement(HTML.TABLE_ELEM, uiComponent); HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext); HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.TABLE_PASSTHROUGH_ATTRIBUTES); renderFacet(facesContext, writer, (UIData) uiComponent, true); } /** * @see javax.faces.render.Renderer#encodeChildren(FacesContext, UIComponent) */ public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException { RendererUtils.checkParamValidity(facesContext, component, UIData.class); UIData uiData = (UIData) component; ResponseWriter writer = facesContext.getResponseWriter(); HtmlRendererUtils.writePrettyLineSeparator(facesContext); writer.startElement(HTML.TBODY_ELEM, component); String rowClasses; String columnClasses; if (component instanceof HtmlDataTable) { rowClasses = ((HtmlDataTable) component).getRowClasses(); columnClasses = ((HtmlDataTable) component).getColumnClasses(); } else { rowClasses = (String) component.getAttributes().get(JSFAttr.ROW_CLASSES_ATTR); columnClasses = (String) component.getAttributes().get(JSFAttr.COLUMN_CLASSES_ATTR); } Styles styles = new Styles(rowClasses, columnClasses); int first = uiData.getFirst(); int rows = uiData.getRows(); int rowCount = uiData.getRowCount(); if (rows <= 0) { rows = rowCount - first; } int last = first + rows; if (last > rowCount) last = rowCount; for (int i = first; i < last; i++) { uiData.setRowIndex(i); if (!uiData.isRowAvailable()) { log.error("Row is not available. Rowindex = " + i); return; } beforeRow(facesContext, uiData); HtmlRendererUtils.writePrettyLineSeparator(facesContext); renderRowStart(facesContext, writer, uiData, styles.getRowStyle(i)); List children = component.getChildren(); for (int j = 0, size = component.getChildCount(); j < size; j++) { UIComponent child = (UIComponent) children.get(j); if(child.isRendered()) { if (child instanceof UIColumn) { String columnStyle = styles.getColumnStyle(j); renderColumnBody(facesContext, writer, uiData, child, columnStyle); } else if (child instanceof UIColumns) { UIColumns columns = (UIColumns) child; for (int k = 0, colSize = columns.getRowCount(); k < colSize; k++) { columns.setRowIndex(k); String columnStyle = styles.getColumnStyle(j); renderColumnBody(facesContext, writer, uiData, child, columnStyle); } columns.setRowIndex(-1); } } } renderRowEnd(facesContext, writer, uiData); afterRow(facesContext, uiData); } writer.endElement(HTML.TBODY_ELEM); } /** * Renders the body of a given <code>UIColumn</code> (everything but the header and footer facets). * @param facesContext the <code>FacesContext</code>. * @param writer the <code>ResponseWriter</code>. * @param uiData the <code>UIData</code> being rendered. * @param component the <code>UIComponent</code> to render. * @param columnStyleClass the styleClass of the <code>UIColumn</code> or <code>null</code> if * there is none. * @throws IOException if an exception occurs. */ protected void renderColumnBody( FacesContext facesContext, ResponseWriter writer, UIData uiData, UIComponent component, String columnStyleClass) throws IOException { writer.startElement(HTML.TD_ELEM, uiData); if (columnStyleClass != null) { writer.writeAttribute(HTML.CLASS_ATTR, columnStyleClass, null); } RendererUtils.renderChild(facesContext, component); writer.endElement(HTML.TD_ELEM); } /** * Renders the start of a new row of body content. * @param facesContext the <code>FacesContext</code>. * @param writer the <code>ResponseWriter</code>. * @param uiData the <code>UIData</code> being rendered. * @param rowStyleClass te styleClass of the row or <code>null</code> if there is none. * @throws IOException if an exceptoin occurs. */ protected void renderRowStart( FacesContext facesContext, ResponseWriter writer, UIData uiData, String rowStyleClass) throws IOException { writer.startElement(HTML.TR_ELEM, uiData); if (rowStyleClass != null) { writer.writeAttribute(HTML.CLASS_ATTR, rowStyleClass, null); } } /** * Renders the end of a row of body content. * @param facesContext the <code>FacesContext</code>. * @param writer the <code>ResponseWriter</code>. * @param uiData the <code>UIData</code> being rendered. * @throws IOException if an exceptoin occurs. */ protected void renderRowEnd( FacesContext facesContext, ResponseWriter writer, UIData uiData) throws IOException { writer.endElement(HTML.TR_ELEM); } /** * Convenient method for derived table renderers. * @param facesContext the <code>FacesContext</code>. * @param uiData the <code>UIData</code> being rendered. */ protected void beforeTable(FacesContext facesContext, UIData uiData) throws IOException { } /** * Convenient method for derived table renderers. * @param facesContext the <code>FacesContext</code>. * @param uiData the <code>UIData</code> being rendered. */ protected void beforeRow(FacesContext facesContext, UIData uiData) throws IOException { } /** * Convenient method for derived table renderers. * @param facesContext the <code>FacesContext</code>. * @param uiData the <code>UIData</code> being rendered. */ protected void afterRow(FacesContext facesContext, UIData uiData) throws IOException { } /** * Convenient method for derived table renderers. * @param facesContext the <code>FacesContext</code>. * @param uiData the <code>UIData</code> being rendered. */ protected void afterTable(FacesContext facesContext, UIData uiData) throws IOException { } /** * @see javax.faces.render.Renderer#encodeEnd(FacesContext, UIComponent) */ public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException { RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class); ResponseWriter writer = facesContext.getResponseWriter(); renderFacet(facesContext, writer, (UIData) uiComponent, false); writer.endElement(HTML.TABLE_ELEM); HtmlRendererUtils.writePrettyLineSeparator(facesContext); afterTable(facesContext, (UIData) uiComponent); } /** * Renders either the header or the footer facets. * @param facesContext the <code>FacesContext</code>. * @param writer the <code>ResponseWriter</code>. * @param component the parent <code>UIComponent</code> containing the facets. * @param header whether this is the header facet (if not, then the footer facet). * @throws IOException if an exception occurs. */ protected void renderFacet(FacesContext facesContext, ResponseWriter writer, UIComponent component, boolean header) throws IOException { int colspan = 0; boolean hasColumnFacet = false; for (Iterator it = component.getChildren().iterator(); it.hasNext();) { UIComponent uiComponent = (UIComponent) it.next(); if(uiComponent.isRendered()) { if (uiComponent instanceof UIColumn) { colspan++; if (!hasColumnFacet) { hasColumnFacet = header ? ((UIColumn) uiComponent).getHeader() != null : ((UIColumn) uiComponent) .getFooter() != null; } } else if (uiComponent instanceof UIColumns) { UIColumns columns = (UIColumns) uiComponent; colspan += columns.getRowCount(); if (!hasColumnFacet) { hasColumnFacet = header ? columns.getHeader() != null : columns.getFooter() != null; } } } } UIComponent facet = header ? (UIComponent) component.getFacets().get(HEADER_FACET_NAME) : (UIComponent) component.getFacets().get(FOOTER_FACET_NAME); if (facet != null || hasColumnFacet) { // Header or Footer present String elemName = header ? HTML.THEAD_ELEM : HTML.TFOOT_ELEM; HtmlRendererUtils.writePrettyLineSeparator(facesContext);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -