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

📄 tablewriter.java

📁 Java的Web报表库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * ========================================
 * JFreeReport : a free Java report library
 * ========================================
 *
 * Project Info:  http://www.jfree.org/jfreereport/index.html
 * Project Lead:  Thomas Morgner;
 *
 * (C) Copyright 2000-2003, by Simba Management Limited and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * ----------------
 * TableWriter.java
 * ----------------
 * (C)opyright 2003, by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner;
 * Contributor(s):   David Gilbert (for Simba Management Limited);
 *
 * $Id: TableWriter.java,v 1.25.2.4 2003/08/24 14:18:13 taqua Exp $
 *
 * Changes
 * -------
 * 24-Jan-2003 : Initial version
 * 17-Feb-2003 : Documentation
 * 24-Feb-2003 : Fixed Checkstyle issues (DG);
 *
 */
package com.jrefinery.report.targets.table;

import java.awt.geom.Rectangle2D;
import java.util.ResourceBundle;

import com.jrefinery.report.Band;
import com.jrefinery.report.Group;
import com.jrefinery.report.event.PageEventListener;
import com.jrefinery.report.event.ReportEvent;
import com.jrefinery.report.function.AbstractFunction;
import com.jrefinery.report.states.ReportState;
import com.jrefinery.report.targets.base.bandlayout.BandLayoutManagerUtil;
import com.jrefinery.report.targets.base.layout.DefaultLayoutSupport;
import com.jrefinery.report.targets.base.layout.LayoutSupport;
import com.jrefinery.report.targets.style.BandStyleSheet;

/**
 * The TableWriter is the content creation function used to collect the cell data.
 * After the layouting is done, the layouted bands are forwarded to the TableProducer.
 * The virtual page has an unlimited size, only when a manual pagebreak is encountered,
 * a new page is started.
 * <p>
 * This can be used to f.i. to create separate sheets in Excel-Workbooks, the detailed
 * semantics depend on concrete implementation of the TableProducer.
 * <p>
 * This writer is not thread-safe.
 *
 * @author Thomas Morgner
 */
public class TableWriter extends AbstractFunction implements PageEventListener
{
  public static final int OUTPUT_LEVEL = -1;

  /**
   * The SheetName-function property, defines the name of an StringFunction
   * that creates the sheet names.
   */
  public static final String SHEET_NAME_FUNCTION_PROPERTY =
      "com.jrefinery.report.targets.table.TableWriter.SheetNameFunction";

  /** The base class for localised resources. */
  public static final String BASE_RESOURCE_CLASS =
      "com.jrefinery.report.resources.JFreeReportResources";

  /** The current event, stored on every call to one of the ReportListener methods. */
  private ReportEvent currentEvent;

  /** The table producer used to create the layout. */
  private TableProducer producer;

  /** the cursor pointing to the current position on the sheet. */
  private TableWriterCursor cursor;

  /** the maximum width, required for the BandLayout. */
  private float maxWidth;

  /** the dependency level for this function, usually -1. */
  private int depLevel;

  /** A flag indicating whether the writer is currently handling the end of an page. */
  private boolean inEndPage;

  /** A flag indicating whether the current page is empty. */
  private boolean isPageEmpty;

  /** A flag that indicates that the current pagebreak will be the last one. */
  private boolean isLastPageBreak;

  /** Locale-specific resources. */
  private ResourceBundle resources;

  /** The current state for repeating group headers. */
  private int currentEffectiveGroupIndex;

  /**
   * Creates a new TableWriter. The dependency level is set to -1 and the maxwidth
   * is defined to be 1000.
   */
  public TableWriter()
  {
    setDependencyLevel(OUTPUT_LEVEL);
    currentEffectiveGroupIndex = -1;
  }

  /**
   * Gets the name of the SheetName function. The sheetname function defines the
   * names of the generated sheets.
   *
   * @return the name of the sheet name function, or null, if that name is not known yet.
   */
  private String getSheetNameFunction()
  {
    if (getCurrentEvent() == null)
    {
      return null;
    }
    return getCurrentEvent().getReport().getReportConfiguration()
        .getConfigProperty(SHEET_NAME_FUNCTION_PROPERTY);
  }

  /**
   * Returns true, if the tablewriter is currently handling the end of the current page.
   *
   * @return true, if the end of the page is currently handled.
   */
  private boolean isInEndPage()
  {
    return inEndPage;
  }

  /**
   * Gets the cursor for this writer. The cursor marks the current position in the
   * current sheet.
   *
   * @return the cursor.
   */
  private TableWriterCursor getCursor()
  {
    return cursor;
  }

  /**
   * Sets the cursor for the writer.
   * @param cursor the new cursor.
   */
  private void setCursor(final TableWriterCursor cursor)
  {
    this.cursor = cursor;
  }

  /**
   * Creates an output target that mimics a real output target, but produces no output.
   * This is used by the reporting engine when it makes its first pass through the report,
   * calculating page boundaries etc.  The second pass will use a real output target.
   *
   * @return a dummy output target.
   */
  private LayoutSupport getLayoutSupport()
  {
    return DefaultLayoutSupport.getDefaultInstance();
  }

  /**
   * Return the current expression value.
   * <P>
   * The value depends (obviously) on the expression implementation.
   *
   * @return the value of the function.
   */
  public Object getValue()
  {
    return this;
  }

  /**
   * Gets the maximum width available for a root band during the layouting process.
   *
   * @return the maximum width for a root band.
   */
  public float getMaxWidth()
  {
    return maxWidth;
  }

  /**
   * Defines the maximum width available for a root band during the layouting process.
   *
   * @param width the maximum width for a root band.
   */
  public void setMaxWidth(final float width)
  {
    maxWidth = width;
  }

  /**
   * Perform the layout of a band. The height of the band is calculated according to the contents
   * of the band.  The width of the band will always span the complete printable width.
   *
   * @param band  the band.
   *
   * @return the dimensions of the band.
   */
  private Rectangle2D doLayout(final Band band)
  {
    // in this layouter the width of a band is always the full page width.
    // the height is not limited ...
    final float width = getMaxWidth();
    final float height = Short.MAX_VALUE;

    final Rectangle2D bounds = BandLayoutManagerUtil.doLayout(band,
        getLayoutSupport(),
        width,
        height);
    getCurrentEvent().getState().fireLayoutCompleteEvent(band, getCurrentEvent().getType());
    return bounds;
  }

  /**
   * Forwards the given band to the TableProducer. This will create the content
   * and will add the TableCellData object to the grid.
   *
   * @see TableProducer#processBand
   * @param bounds the bounds of the band, defines the position of the printed band within
   * the sheet.
   * @param band the band that should be printed.
   */
  private void doPrint(final Rectangle2D bounds, final Band band)
  {
    final int cellCount = producer.getCellCount();

    // now print the band ...
    producer.processBand(bounds, band);
    getCursor().advance((float) bounds.getHeight());
    if (cellCount < producer.getCellCount())
    {
      // something was printed ...
      isPageEmpty = false;
    }
  }


  /**
   * Ends the current page. Fires the PageFinished event.
   */
  private void endPage()
  {
    if (inEndPage == true)
    {
      throw new IllegalStateException("Already in startPage or endPage");
    }
    inEndPage = true;

    final ReportEvent currentEvent = getCurrentEvent();
    final ReportState cEventState = getCurrentEvent().getState();
    cEventState.firePageFinishedEvent();
    cEventState.nextPage();
    setCurrentEvent(currentEvent);
    inEndPage = false;
  }

  /**
   * Starts a new page. Fires the PageStarted event.
   */
  public void startPage()
  {
    if (inEndPage == true)
    {
      throw new IllegalStateException("Already in startPage or endPage");
    }
    inEndPage = true;

    final ReportEvent currentEvent = getCurrentEvent();
    final ReportState cEventState = currentEvent.getState();
    cEventState.firePageStartedEvent(currentEvent.getType());
    setCurrentEvent(currentEvent);
    inEndPage = false;
    isPageEmpty = true;
  }

  /**
   * Performs the band layout and prints the band.
   *
   * @param b  the band.
   */
  protected void print(final Band b)
  {
    if (!isInEndPage() && (isPageEmpty == false)
        && b.getStyle().getBooleanStyleProperty(BandStyleSheet.PAGEBREAK_BEFORE) == true)
    {
      endPage();
      startPage();

⌨️ 快捷键说明

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