⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tablewritertemplate.java

📁 一个比较不错的java分页标签,有源代码,开发者 可以学习学习
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * Licensed under the Artistic License; you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://displaytag.sourceforge.net/license.html
 *
 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */
package org.displaytag.render;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.JspException;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.displaytag.decorator.TableDecorator;
import org.displaytag.model.Column;
import org.displaytag.model.ColumnIterator;
import org.displaytag.model.HeaderCell;
import org.displaytag.model.Row;
import org.displaytag.model.RowIterator;
import org.displaytag.model.TableModel;
import org.displaytag.properties.TableProperties;
import org.displaytag.util.TagConstants;


/**
 * A template that encapsulates and drives the construction of a table based on a given table model and configuration.
 * This class is meant to be extended by classes that build tables sharing the same structure, sorting, and grouping,
 * but that write them in different formats and to various destinations. Subclasses must provide the format- and
 * destination-specific implementations of the abstract methods this class calls to build a table. (Background: This
 * class came about because our users wanted to export tables to Excel and PDF just as they were presented in HTML. It
 * originates with the TableTagData.writeHTMLData method, factoring its logic so that it can be re-used by classes that
 * write the tables as PDF, Excel, RTF and other formats. TableTagData.writeHTMLData now calls an HTML extension of this
 * class to write tables in HTML format to a JSP page.)
 * @author Jorge L. Barroso
 * @version $Id$
 */
public abstract class TableWriterTemplate
{

    public static final short GROUP_START = -2;

    public static final short GROUP_END = 5;

    public static final short GROUP_START_AND_END = 3;

    public static final short GROUP_NO_CHANGE = 0;

    /**
     * logger.
     */
    private static Log log = LogFactory.getLog(TableWriterTemplate.class);

    /**
     * Table unique id.
     */
    private String id;

    /**
     * Given a table model, this method creates a table, sorting and grouping it per its configuration, while delegating
     * where and how it writes the table to subclass objects. (Background: This method refactors
     * TableTagData.writeHTMLData method. See above.)
     * @param model The table model used to build the table.
     * @param id This table's page id.
     * @throws JspException if any exception thrown while constructing the tablei, it is caught and rethrown as a
     * JspException. Extension classes may throw all sorts of exceptions, depending on their respective formats and
     * destinations.
     */
    public void writeTable(TableModel model, String id) throws JspException
    {
        try
        {
            // table id used for logging
            this.id = id;

            TableProperties properties = model.getProperties();

            if (log.isDebugEnabled())
            {
                log.debug("[" + this.id + "] writeTable called for table [" + this.id + "]");
            }

            // Handle empty table
            boolean noItems = model.getRowListPage().size() == 0;
            if (noItems && !properties.getEmptyListShowTable())
            {
                writeEmptyListMessage(properties.getEmptyListMessage());
                return;
            }

            // Put the page stuff there if it needs to be there...
            if (properties.getAddPagingBannerTop())
            {
                // search result and navigation bar
                writeTopBanner(model);
            }

            // open table
            writeTableOpener(model);

            // render caption
            if (model.getCaption() != null)
            {
                writeCaption(model);
            }

            // render headers
            if (model.getProperties().getShowHeader())
            {
                writeTableHeader(model);
            }

            // render footer prior to body
            if (model.getFooter() != null)
            {
                writePreBodyFooter(model);
            }

            // open table body
            writeTableBodyOpener(model);

            // render table body
            writeTableBody(model);

            // close table body
            writeTableBodyCloser(model);

            // render footer after body
            if (model.getFooter() != null)
            {
                writePostBodyFooter(model);
            }

            // close table
            writeTableCloser(model);

            if (model.getTableDecorator() != null)
            {
                writeDecoratedTableFinish(model);
            }

            writeBottomBanner(model);

            if (log.isDebugEnabled())
            {
                log.debug("[" + this.id + "] writeTable end");
            }
        }
        catch (Exception e)
        {
            throw new JspException(e);
        }
    }

    /**
     * Called by writeTable to write a message explaining that the table model contains no data.
     * @param emptyListMessage A message explaining that the table model contains no data.
     * @throws Exception if it encounters an error while writing.
     */
    protected abstract void writeEmptyListMessage(String emptyListMessage) throws Exception;

    /**
     * Called by writeTable to write a summary of the search result this table reports and the table's pagination
     * interface.
     * @param model The table model for which the banner is written.
     * @throws Exception if it encounters an error while writing.
     */
    protected abstract void writeTopBanner(TableModel model) throws Exception;

    /**
     * Called by writeTable to write the start of the table structure.
     * @param model The table model for which the content is written.
     * @throws Exception if it encounters an error while writing.
     */
    protected abstract void writeTableOpener(TableModel model) throws Exception;

    /**
     * Called by writeTable to write the table's caption.
     * @param model The table model for which the content is written.
     * @throws Exception if it encounters an error while writing.
     */
    protected abstract void writeCaption(TableModel model) throws Exception;

    /**
     * Called by writeTable to write the table's header columns.
     * @param model The table model for which the content is written.
     * @throws Exception if it encounters an error while writing.
     */
    protected abstract void writeTableHeader(TableModel model) throws Exception;

    /**
     * Called by writeTable to write table footer before table body.
     * @param model The table model for which the content is written.
     * @throws Exception if it encounters an error while writing.
     */
    protected abstract void writePreBodyFooter(TableModel model) throws Exception;

    /**
     * Called by writeTable to write the start of the table's body.
     * @param model The table model for which the content is written.
     * @throws Exception if it encounters an error while writing.
     */
    protected abstract void writeTableBodyOpener(TableModel model) throws Exception;

    // protected abstract void writeTableBody(TableModel model);

    /**
     * Called by writeTable to write the end of the table's body.
     * @param model The table model for which the content is written.
     * @throws Exception if it encounters an error while writing.
     */
    protected abstract void writeTableBodyCloser(TableModel model) throws Exception;

    /**
     * Called by writeTable to write table footer after table body.
     * @param model The table model for which the content is written.
     * @throws Exception if it encounters an error while writing.
     */
    protected abstract void writePostBodyFooter(TableModel model) throws Exception;

    /**
     * Called by writeTable to write the end of the table's structure.
     * @param model The table model for which the content is written.
     * @throws Exception if it encounters an error while writing.
     */
    protected abstract void writeTableCloser(TableModel model) throws Exception;

    /**
     * Called by writeTable to decorate the table.
     * @param model The table model for which the content is written.
     * @throws Exception if it encounters an error while writing.
     */
    protected abstract void writeDecoratedTableFinish(TableModel model) throws Exception;

    /**
     * Called by writeTable to write the table's footer.
     * @param model The table model for which the content is written.
     * @throws Exception if it encounters an error while writing.
     */
    protected abstract void writeBottomBanner(TableModel model) throws Exception;

    /**
     * Given a table model, writes the table body content, sorting and grouping it per its configuration, while
     * delegating where and how it writes to subclass objects. (Background: This method refactors
     * TableTagData.writeTableBody method. See above.)
     * @param model The table model used to build the table body.
     * @throws Exception if an error is encountered while writing the table body.
     */
    private void writeTableBody(TableModel model) throws Exception
    {
        // Ok, start bouncing through our list (only the visible part)
        RowIterator rowIterator = model.getRowIterator(false);

        // iterator on rows
        TableDecorator tableDecorator = model.getTableDecorator();
        Row previousRow = null;
        Row currentRow = null;
        Row nextRow = null;
        Map previousRowValues = new HashMap(10);
        Map currentRowValues = new HashMap(10);
        Map nextRowValues = new HashMap(10);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -