📄 simplepagelayouter.java
字号:
/**
* ========================================
* 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.
*
* -----------------------
* SimplePageLayouter.java
* -----------------------
* (C)opyright 2002, 2003, by Thomas Morgner and Contributors.
*
* Original Author: Thomas Morgner;
* Contributor(s): David Gilbert (for Simba Management Limited);
*
* $Id: SimplePageLayouter.java,v 1.55.2.2 2003/08/24 14:18:13 taqua Exp $
*
* Changes
* -------
* 05-Dec-2002 : Updated Javadocs (DG);
* 07-Dec-2002 : Removed manual-pagebreak flag, was a left-over. PageFinished
* did not query the DISPLAY_ON_FIRSTPAGE flag.
* 12-Dec-2002 : Layouting Fix I: Layouting failed for non-absolute band elements if no
* absolute positioned elements were defined.
* 18-Dec-2002 : Layouting Fix II: PageFooter on own page failed
* 07-Jan-2003 : Layouting Fix III: Limited max band height to the available space of a single page
* 08-Jan-2003 : Layouting Fix IIa: PageFooter fix fix
* 27-Jan-2003 : BugFix: printing empty bands caused a commit spooled operations
* 01-Feb-2003 : Layouting moved into BandLayoutManagerUtil (Common code)
*/
package com.jrefinery.report.targets.pageable.pagelayout;
import java.awt.geom.Rectangle2D;
import com.jrefinery.report.Band;
import com.jrefinery.report.Group;
import com.jrefinery.report.JFreeReportConstants;
import com.jrefinery.report.ReportProcessingException;
import com.jrefinery.report.states.DataRowConnector;
import com.jrefinery.report.states.ReportDefinitionImpl;
import com.jrefinery.report.event.PrepareEventListener;
import com.jrefinery.report.event.ReportEvent;
import com.jrefinery.report.function.Expression;
import com.jrefinery.report.function.FunctionProcessingException;
import com.jrefinery.report.states.ReportState;
import com.jrefinery.report.targets.base.bandlayout.BandLayoutManagerUtil;
import com.jrefinery.report.targets.pageable.LogicalPage;
import com.jrefinery.report.targets.pageable.OutputTargetException;
import com.jrefinery.report.targets.pageable.Spool;
import com.jrefinery.report.targets.style.BandStyleSheet;
/**
* A simple page layouter. This class replicates the 'old' behaviour of JFreeReport,
* simple and straightforward.
* <p>
* Layout Constraints used:
* <ul>
* <li>PageHeader, PageFooter: BandStyleSheet.DISPLAY_ON_FIRST_PAGE
* <p>Defines whether a PageHeader or ~footer should be printed on the first page.</p>
* <li>PageHeader, PageFooter: BandStyleSheet.DISPLAY_ON_LAST_PAGE
* <p>Defines whether a PageHeader or ~footer should be printed on the last page.
* A warning: For the PageHeader this works only if the ReportFooter has a pagebreak
* before printing.
* </p>
* <li>GroupHeader: BandStyleSheet.REPEAT_HEADER
* <p>Defines whether this GroupHeader should be repeated on every page as long as this
* group is active</p>
* <li>All Bands: BandStyleSheet.PAGEBREAK_BEFORE, BandStyleSheet.PAGEBREAK_AFTER
* <p>defines whether to start a new page before the band is printed or after the
* band is printed. This request is ignored, if the current page is empty (not counting
* the PageHeader and the repeating GroupHeader).</p>
* </ul>
*
* @author Thomas Morgner
*/
public class SimplePageLayouter extends PageLayouter implements PrepareEventListener
{
/**
* Represents the current state of the page layouter.
*/
protected static class SimpleLayoutManagerState extends PageLayouter.LayoutManagerState
{
/** The band. */
private Band band;
/**
* Creates a new state. The band can be <code>null</code> if there is no band to be printed
* on the next page.
*
* @param band the band.
*/
public SimpleLayoutManagerState(final Band band)
{
this.band = band;
}
/**
* Returns the band.
*
* @return the band.
*/
public Band getBand()
{
return band;
}
/**
* Returns a string describing the object.
*
* @return The string.
*/
public String toString()
{
return "State={" + band + "}";
}
}
/** small carrier class to transport the maximum page number for this report. */
private static class PageCarrier
{
/** stores the last page number of the report processing. */
public int maxPages;
}
/** the page carrier for this pagelayouter contains the number of the last page. */
private PageCarrier pageCarrier;
/** A useful constant. */
private static final boolean ENDPAGE_FORCED = true;
/** A useful constant. */
private static final boolean ENDPAGE_REQUESTED = false;
/** A flag that indicates that the current pagebreak will be the last one. */
private boolean isLastPageBreak;
/**
* A flag which indicates that a new page should be started before the next band
* is printed. Bool-or this flag with PageBreakBefore ...
*/
private boolean startNewPage;
/** The cursor used by the layouter. */
private SimplePageLayoutCursor cursor;
/** The current state. */
private SimpleLayoutManagerState state;
/** The spool. */
private Spool spooledBand;
/** The current state for repeating group headers. */
private int currentEffectiveGroupIndex;
/**
* Creates a new page layouter.
*/
public SimplePageLayouter()
{
setName("Layout");
currentEffectiveGroupIndex = -1;
pageCarrier = new PageCarrier();
}
/**
* Return a completly separated copy of this function. The copy does no
* longer share any changeable objects with the original function.
*
* @return a copy of this function.
*/
public Expression getInstance()
{
final SimplePageLayouter pl = (SimplePageLayouter) super.getInstance();
pl.pageCarrier = new PageCarrier();
return pl;
}
/**
* Returns the highest pagenumber found during the repagination process.
*
* @return the highest page number.
*/
public int getMaxPage()
{
return pageCarrier.maxPages;
}
/**
* Defines the highest pagenumber found during the repagination process.
*
* @param maxPage the highest page number.
*/
protected void setMaxPage(final int maxPage)
{
pageCarrier.maxPages = maxPage;
}
/**
* Receives notification that the report has started. Also invokes the
* start of the first page ...
* <P>
* Layout and draw the report header after the PageStartEvent was fired.
*
* @param event the event.
*/
public void reportStarted(final ReportEvent event)
{
// activating this state after the page has ended is invalid.
if (isPageEnded())
{
throw new IllegalStateException();
}
setCurrentEvent(event);
if (getCurrentEvent() == null)
{
throw new NullPointerException("getCurrentEvent() returned null");
}
try
{
currentEffectiveGroupIndex = -1;
printBand(getReport().getReportHeader());
}
catch (FunctionProcessingException fe)
{
throw fe;
}
catch (Exception e)
{
throw new FunctionProcessingException("ReportStarted failed", e);
}
finally
{
clearCurrentEvent();
}
}
/**
* Receives notification that a group of item bands has been completed.
* <P>
* The itemBand is finished, the report starts to close open groups.
*
* @param event The event.
*/
public void itemsFinished(final ReportEvent event)
{
// activating this state after the page has ended is invalid.
if (isPageEnded())
{
throw new IllegalStateException();
}
currentEffectiveGroupIndex -= 1;
}
/**
* Receives notification that report generation has completed, the report footer was printed,
* no more output is done. This is a helper event to shut down the output service.
*
* @param event The event.
*/
public void reportDone(final ReportEvent event)
{
// activating this state after the page has ended is invalid.
if (isPageEnded())
{
throw new IllegalStateException();
}
try
{
setCurrentEvent(event);
restartPage();
createSaveState(null);
saveCurrentState();
endPage(ENDPAGE_FORCED);
}
catch (Exception e)
{
throw new FunctionProcessingException("ReportDone", e);
}
finally
{
clearCurrentEvent();
}
}
/**
* Receives notification that a group of item bands is about to be processed.
* <P>
* The next events will be itemsAdvanced events until the itemsFinished event is raised.
*
* @param event The event.
*/
public void itemsStarted(final ReportEvent event)
{
// activating this state after the page has ended is invalid.
if (isPageEnded())
{
throw new IllegalStateException();
}
currentEffectiveGroupIndex += 1;
}
/**
* Receives notification that a page has started.
* <P>
* This prints the PageHeader. If this is the first page, the header is not
* printed if the pageheader style-flag DISPLAY_ON_FIRSTPAGE is set to false.
* If this event is known to be the last pageStarted event, the DISPLAY_ON_LASTPAGE
* is evaluated and the header is printed only if this flag is set to TRUE.
* <p>
* If there is an active repeating GroupHeader, print the last one. The GroupHeader
* is searched for the current group and all parent groups, starting at the
* current group and ascending to the parents. The first goupheader that has the
* StyleFlag REPEAT_HEADER set to TRUE is printed.
* <p>
* The PageHeader and the repeating GroupHeader are spooled until the first real
* content is printed. This way, the LogicalPage remains empty until an other band
* is printed.
*
* @param event Information about the event.
*/
public void pageStarted(final ReportEvent event)
{
// activating this state after the page has ended is invalid.
if (isPageEnded())
{
throw new IllegalStateException();
}
setCurrentEvent(event);
try
{
// a new page has started, so reset the cursor ...
setCursor(new SimplePageLayoutCursor(getLogicalPage().getHeight()));
final Band b = getReport().getPageHeader();
if (event.getState().getCurrentPage() == 1)
{
if (b.getStyle().getBooleanStyleProperty(BandStyleSheet.DISPLAY_ON_FIRSTPAGE) == true)
{
print(b, true);
}
}
else if (event.getState().getCurrentPage() == getMaxPage())
{
if (b.getStyle().getBooleanStyleProperty(BandStyleSheet.DISPLAY_ON_LASTPAGE) == true)
{
print(b, true);
}
}
else if (isLastPageBreak)
{
if (b.getStyle().getBooleanStyleProperty(BandStyleSheet.DISPLAY_ON_LASTPAGE) == true)
{
print(b, true);
}
}
else
{
print(b, true);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -