001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * AbstractOutputProcessor.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.layout.output;
030:
031: import java.util.ArrayList;
032: import java.util.Collections;
033: import java.util.List;
034:
035: import org.jfree.report.layout.model.LogicalPageBox;
036: import org.jfree.report.layout.model.PageGrid;
037:
038: /**
039: * Creation-Date: 09.04.2007, 10:51:30
040: *
041: * @author Thomas Morgner
042: */
043: public abstract class AbstractOutputProcessor implements
044: OutputProcessor {
045: protected static final int PROCESSING_PAGES = 0;
046: protected static final int PROCESSING_CONTENT = 2;
047: private int processingState;
048:
049: private List logicalPages;
050: private int pageCursor;
051:
052: protected AbstractOutputProcessor() {
053: logicalPages = new ArrayList();
054: }
055:
056: public final int getLogicalPageCount() {
057: return logicalPages.size();
058: }
059:
060: public final LogicalPageKey getLogicalPage(final int page) {
061: if (isPaginationFinished() == false) {
062: throw new IllegalStateException();
063: }
064:
065: return (LogicalPageKey) logicalPages.get(page);
066: }
067:
068: /**
069: * Checks, whether the 'processingFinished' event had been received at least once.
070: *
071: * @return
072: */
073: public final boolean isPaginationFinished() {
074: return processingState == PROCESSING_CONTENT;
075: }
076:
077: /**
078: * Notifies the output processor, that the processing has been finished and that the input-feed received the last
079: * event.
080: */
081: public final void processingFinished() {
082: pageCursor = 0;
083: if (processingState == PROCESSING_PAGES) {
084: // the pagination is complete. So, now we can produce real content.
085: processingPagesFinished();
086: processingState = PROCESSING_CONTENT;
087: } else {
088: processingContentFinished();
089: }
090: }
091:
092: protected void processingContentFinished() {
093:
094: }
095:
096: protected void processingPagesFinished() {
097: logicalPages = Collections.unmodifiableList(logicalPages);
098: }
099:
100: protected LogicalPageKey createLogicalPage(final int width,
101: final int height) {
102: return new LogicalPageKey(logicalPages.size(), width, height);
103: }
104:
105: public final int getPageCursor() {
106: return pageCursor;
107: }
108:
109: public final void setPageCursor(final int pageCursor) {
110: this .pageCursor = pageCursor;
111: }
112:
113: /**
114: * This flag indicates, whether the output processor has collected enough information to start the content
115: * generation.
116: *
117: * @return
118: */
119: protected boolean isContentGeneratable() {
120: return processingState == PROCESSING_CONTENT;
121: }
122:
123: public void processRecomputedContent(final LogicalPageBox pageBox)
124: throws ContentProcessingException {
125: setPageCursor(pageCursor + 1);
126: }
127:
128: public final void processContent(final LogicalPageBox logicalPage)
129: throws ContentProcessingException {
130: if (isContentGeneratable() == false) {
131: // This is the pagination stage ..
132:
133: // This is just an assertation ...
134: // Only if pagination is active ..
135: final PageGrid pageGrid = logicalPage.getPageGrid();
136: final int rowCount = pageGrid.getRowCount();
137: final int colCount = pageGrid.getColumnCount();
138:
139: final LogicalPageKey key = createLogicalPage(colCount,
140: rowCount);
141: logicalPages.add(key);
142: final int pageCursor = getPageCursor();
143: if (key.getPosition() != pageCursor) {
144: throw new IllegalStateException("Expected position "
145: + pageCursor + " is not the key's position "
146: + key.getPosition());
147: }
148:
149: processPaginationContent(key, logicalPage);
150: setPageCursor(pageCursor + 1);
151:
152: } else // if (isContentGeneratable())
153: {
154: // This is the content generation stage ..
155:
156: final int pageCursor = getPageCursor();
157: final LogicalPageKey logicalPageKey = getLogicalPage(pageCursor);
158: processPageContent(logicalPageKey, logicalPage);
159: setPageCursor(pageCursor + 1);
160: }
161: }
162:
163: public boolean isNeedAlignedPage() {
164: return isContentGeneratable();
165: }
166:
167: protected void processPaginationContent(
168: final LogicalPageKey logicalPageKey,
169: final LogicalPageBox logicalPage)
170: throws ContentProcessingException {
171: }
172:
173: protected abstract void processPageContent(
174: final LogicalPageKey logicalPageKey,
175: final LogicalPageBox logicalPage)
176: throws ContentProcessingException;
177:
178: public int getPhysicalPageCount() {
179: // By default, we assume a one-to-one mapping between logical and physical pages. Only the pageable target
180: // will implement a different mapping ..
181: return getLogicalPageCount();
182: }
183: }
|