001: /**
002: * ========================================
003: * JFreeReport : a free Java report library
004: * ========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2000-2007, by Object Refinery Limited, 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: * $Id: BufferingLayoutController.java 3048 2007-07-28 18:02:42Z tmorgner $
027: * ------------
028: * (C) Copyright 2000-2005, by Object Refinery Limited.
029: * (C) Copyright 2005-2007, by Pentaho Corporation.
030: */package org.jfree.report.flow.layoutprocessor;
031:
032: import org.jfree.report.DataSourceException;
033: import org.jfree.report.ReportDataFactoryException;
034: import org.jfree.report.ReportProcessingException;
035: import org.jfree.report.flow.FlowController;
036: import org.jfree.report.flow.ReportTarget;
037:
038: /**
039: * Todo: Document me!
040: *
041: * @author Thomas Morgner
042: * @since 05.03.2007
043: */
044: public abstract class BufferingLayoutController extends
045: AbstractLayoutController {
046: private BufferedReportTarget reportTarget;
047: private LayoutController delegate;
048: private boolean finished;
049:
050: protected BufferingLayoutController() {
051: reportTarget = new BufferedReportTarget();
052: }
053:
054: /**
055: * Advances the processing position.
056: *
057: * @param target the report target that receives generated events.
058: * @return the new layout controller instance representing the new state.
059: *
060: * @throws DataSourceException if there was a problem reading data from
061: * the datasource.
062: * @throws ReportProcessingException if there was a general problem during
063: * the report processing.
064: * @throws ReportDataFactoryException if a query failed.
065: */
066: public LayoutController advance(final ReportTarget target)
067: throws DataSourceException, ReportDataFactoryException,
068: ReportProcessingException {
069: reportTarget.setTarget(target);
070: if (delegate != null) {
071: try {
072: final BufferingLayoutController bc = (BufferingLayoutController) clone();
073: bc.delegate = delegate.advance(reportTarget);
074: return bc;
075: } finally {
076: reportTarget.setTarget(null);
077: }
078: }
079:
080: // write all buffered changes to the real report target.
081: reportTarget.close(target);
082: if (getParent() == null) {
083: final BufferingLayoutController bc = (BufferingLayoutController) clone();
084: bc.finished = true;
085: return bc;
086: }
087:
088: return joinWithParent();
089: }
090:
091: /**
092: * Joins the layout controller with the parent. This simply calls
093: * {@link #join(org.jfree.report.flow.FlowController)} on the parent. A join
094: * operation is necessary to propagate changes in the flow-controller to the
095: * parent for further processing.
096: *
097: * @return the joined parent.
098: * @throws IllegalStateException if this layout controller has no parent.
099: * @throws org.jfree.report.ReportProcessingException
100: * @throws org.jfree.report.ReportDataFactoryException
101: * @throws org.jfree.report.DataSourceException
102: */
103: protected LayoutController joinWithParent()
104: throws ReportProcessingException,
105: ReportDataFactoryException, DataSourceException {
106: final LayoutController parent = getParent();
107: if (parent == null) {
108: // skip to the next step ..
109: throw new IllegalStateException(
110: "There is no parent to join with. "
111: + "This should not happen in a sane environment!");
112: }
113:
114: return parent.join(getFlowController());
115: }
116:
117: /**
118: * Initializes the layout controller. This method is called exactly once. It
119: * is the creators responsibility to call this method.
120: * <p/>
121: * Calling initialize after the first advance must result in a
122: * IllegalStateException.
123: *
124: * @param node the currently processed object or layout node.
125: * @param flowController the current flow controller.
126: * @param parent the parent layout controller that was responsible for
127: * instantiating this controller.
128: * @throws DataSourceException if there was a problem reading data from
129: * the datasource.
130: * @throws ReportProcessingException if there was a general problem during
131: * the report processing.
132: * @throws ReportDataFactoryException if a query failed.
133: */
134: public void initialize(final Object node,
135: final FlowController flowController,
136: final LayoutController parent) throws DataSourceException,
137: ReportDataFactoryException, ReportProcessingException {
138: super .initialize(node, flowController, parent);
139: delegate = getInitialDelegate();
140: }
141:
142: protected abstract LayoutController getInitialDelegate();
143:
144: public boolean isAdvanceable() {
145: if (delegate == null) {
146: return finished == false;
147: }
148: return delegate.isAdvanceable();
149: }
150:
151: /**
152: * Joins with a delegated process flow. This is generally called from a child
153: * flow and should *not* (I mean it!) be called from outside. If you do,
154: * you'll suffer.
155: *
156: * @param flowController the flow controller of the parent.
157: * @return the joined layout controller that incorperates all changes from
158: * the delegate.
159: */
160: public LayoutController join(final FlowController flowController)
161: throws ReportProcessingException, DataSourceException {
162: // the delegation is finished, the element has returned.
163: final BufferingLayoutController bc = (BufferingLayoutController) clone();
164: bc.delegate = null;
165: return bc;
166: }
167:
168: public Object clone() {
169: final BufferingLayoutController o = (BufferingLayoutController) super
170: .clone();
171: o.reportTarget = (BufferedReportTarget) reportTarget.clone();
172: return o;
173: }
174: }
|