📄 htmlnewspapertablerenderer.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.custom.newspaper;import javax.faces.component.UIComponent;import javax.faces.context.FacesContext;import javax.faces.component.html.HtmlDataTable;import javax.faces.context.ResponseWriter;import javax.faces.component.UIColumn;import javax.faces.component.UIData;import org.apache.myfaces.renderkit.JSFAttr;import org.apache.myfaces.renderkit.RendererUtils;import org.apache.myfaces.renderkit.html.HtmlRendererUtils;import org.apache.myfaces.renderkit.html.HTML;import org.apache.myfaces.renderkit.html.HtmlTableRendererBase;import org.apache.myfaces.util.ArrayUtils;import org.apache.myfaces.util.StringUtils;import java.io.IOException;import java.util.Iterator;import java.util.List;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * Renderer for a table in multiple balanced columns. * * @author <a href="mailto:jesse@odel.on.ca">Jesse Wilson</a> */public class HtmlNewspaperTableRenderer extends HtmlTableRendererBase{ private static final Log log = LogFactory.getLog(HtmlNewspaperTableRenderer.class); public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException { RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class); ResponseWriter writer = facesContext.getResponseWriter(); HtmlNewspaperTable newspaperTable = (HtmlNewspaperTable)uiComponent; // write out the table start tag HtmlRendererUtils.writePrettyLineSeparator(facesContext); writer.startElement(HTML.TABLE_ELEM, newspaperTable); writer.writeAttribute(HTML.ID_ATTR, newspaperTable.getClientId(facesContext), null); HtmlRendererUtils.renderHTMLAttributes(writer, newspaperTable, HTML.TABLE_PASSTHROUGH_ATTRIBUTES); // render the header renderFacet(facesContext, writer, newspaperTable, true); } public void encodeChildren(FacesContext facesContext, UIComponent uiComponent) throws IOException { RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class); ResponseWriter writer = facesContext.getResponseWriter(); HtmlNewspaperTable newspaperTable = (HtmlNewspaperTable)uiComponent; // begin the table HtmlRendererUtils.writePrettyLineSeparator(facesContext); writer.startElement(HTML.TBODY_ELEM, newspaperTable); // get the CSS styles Styles styles = getStyles(newspaperTable); // count the number of actual rows int first = newspaperTable.getFirst(); int rows = newspaperTable.getRows(); int rowCount = newspaperTable.getRowCount(); if(rows <= 0) { rows = rowCount - first; } int last = first + rows; if(last > rowCount) last = rowCount; // the number of slices to break the table up into */ int newspaperColumns = newspaperTable.getNewspaperColumns(); int newspaperRows; if((last - first) % newspaperColumns == 0) newspaperRows = (last - first) / newspaperColumns; else newspaperRows = ((last - first) / newspaperColumns) + 1; // walk through the newspaper rows for(int nr = 0; nr < newspaperRows; nr++) { // write the row start HtmlRendererUtils.writePrettyLineSeparator(facesContext); writer.startElement(HTML.TR_ELEM, newspaperTable); if(styles.hasRowStyle()) { String rowStyle = styles.getRowStyle(nr); writer.writeAttribute(HTML.CLASS_ATTR, rowStyle, null); } // walk through the newspaper columns for(int nc = 0; nc < newspaperColumns; nc++) { // the current row in the 'real' table int currentRow = nc * newspaperRows + nr + first; // if this row is not to be rendered if(currentRow >= last) continue; // bail if any row does not exist newspaperTable.setRowIndex(currentRow); if(!newspaperTable.isRowAvailable()) { log.error("Row is not available. Rowindex = " + currentRow); return; } // write each cell List children = newspaperTable.getChildren(); for(int j = 0; j < newspaperTable.getChildCount(); j++) { // skip this child if its not a rendered column UIComponent child = (UIComponent)children.get(j); if(!(child instanceof UIColumn)) continue; UIColumn column = (UIColumn)child; if(!child.isRendered()) continue; // draw the element's cell writer.startElement(HTML.TD_ELEM, newspaperTable); if(styles.hasColumnStyle()) writer.writeAttribute(HTML.CLASS_ATTR, styles.getColumnStyle(j), null); RendererUtils.renderChild(facesContext, child); writer.endElement(HTML.TD_ELEM); } // draw the spacer facet if(nc < newspaperColumns - 1) renderSpacerCell(facesContext, writer, newspaperTable); } // write the row end writer.endElement(HTML.TR_ELEM); } // write the end of the table's body writer.endElement(HTML.TBODY_ELEM); } /** * Fetch the number of columns to divide the table into. */ private int getNewspaperColumns() { return 3; } public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException { RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class); ResponseWriter writer = facesContext.getResponseWriter(); HtmlNewspaperTable newspaperTable = (HtmlNewspaperTable)uiComponent; // render the footer renderFacet(facesContext, writer, newspaperTable, false); // write out the table end tag writer.endElement(HTML.TABLE_ELEM); HtmlRendererUtils.writePrettyLineSeparator(facesContext); } /** * Count the number of columns in the speicifed Newspaper table.. */ private int countColumns(HtmlNewspaperTable newspaperTable) { int columnCount = 0; for(Iterator it = newspaperTable.getChildren().iterator(); it.hasNext(); ) { UIComponent uiComponent = (UIComponent)it.next(); if (uiComponent instanceof UIColumn && ((UIColumn)uiComponent).isRendered()) { columnCount++; } } return columnCount; } /** * Tests if the specified facet exists for the specified newspaper table. */ private boolean hasFacet(HtmlNewspaperTable newspaperTable, boolean header) { for(Iterator it = newspaperTable.getChildren().iterator(); it.hasNext(); ) { // get the column UIComponent uiComponent = (UIComponent)it.next(); if(!(uiComponent instanceof UIColumn)) continue; UIColumn column = (UIColumn)uiComponent; if(!column.isRendered()) continue; // test the facet if(header && ((UIColumn)uiComponent).getHeader() != null) return true; if(!header && ((UIColumn)uiComponent).getFooter() != null) return true; } return false; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -