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: * EndGroupHandler.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.states.process;
030:
031: import org.jfree.report.Group;
032: import org.jfree.report.ReportProcessingException;
033: import org.jfree.report.RootLevelBand;
034: import org.jfree.report.event.ReportEvent;
035: import org.jfree.report.states.datarow.DefaultFlowController;
036:
037: /**
038: * Creation-Date: 03.07.2007, 13:57:49
039: *
040: * @author Thomas Morgner
041: */
042: public class EndGroupHandler implements AdvanceHandler {
043: public static final AdvanceHandler HANDLER = new EndGroupHandler();
044:
045: public EndGroupHandler() {
046: }
047:
048: public ProcessState commit(final ProcessState next)
049: throws ReportProcessingException {
050:
051: final RootLevelBand rootLevelBand = next.getReport().getGroup(
052: next.getCurrentGroupIndex()).getHeader();
053: if (rootLevelBand.getSubReportCount() > 0) {
054: next.setAdvanceHandler(JoinEndGroupHandler.HANDLER);
055: return new ProcessState(rootLevelBand.getSubReports(), 0,
056: next);
057: }
058:
059: next.setCurrentGroupIndex(next.getCurrentGroupIndex() - 1);
060: final DefaultFlowController fc = next.getFlowController();
061: final boolean advanceRequested = fc.isAdvanceRequested();
062: final boolean advanceable = fc.getMasterRow().isAdvanceable();
063: if (isRootGroup(next)) {
064: // there is no parent group. So if there is more data, print the next header for this group,
065: // else print the report-footer and finish the report processing.
066: if (advanceRequested && advanceable) {
067: final DefaultFlowController cfc = fc.performCommit();
068: next.setFlowController(cfc);
069: next.setAdvanceHandler(BeginGroupHandler.HANDLER);
070: return next;
071: } else {
072: next.setAdvanceHandler(ReportFooterHandler.HANDLER);
073: return next;
074: }
075: }
076:
077: if (advanceRequested == false || advanceable == false) {
078: // This happens for empty - reports. Empty-Reports are never advanceable, therefore we can
079: // reach an non-advance state where inner group-footers are printed.
080: next.setAdvanceHandler(EndGroupHandler.HANDLER);
081: return next;
082: }
083:
084: // This group is not the outer-most group ..
085: final Group group = next.getReport().getGroup(
086: next.getCurrentGroupIndex());
087: final DefaultFlowController cfc = fc.performCommit();
088: if (ProcessState.isLastItemInGroup(group, fc.getMasterRow(),
089: cfc.getMasterRow())) {
090: // continue with an other EndGroup-State ...
091: next.setAdvanceHandler(EndGroupHandler.HANDLER);
092: return next;
093: } else {
094: // The parent group is not finished, so finalize the createRollbackInformation.
095: // more data in parent group, print the next header
096: next.setFlowController(cfc);
097: next.setAdvanceHandler(BeginGroupHandler.HANDLER);
098: return next;
099: }
100: }
101:
102: public ProcessState advance(final ProcessState state)
103: throws ReportProcessingException {
104: final ProcessState next = state.deriveForAdvance();
105: next.firePrepareEvent();
106: next.fireReportEvent();
107: return next;
108: }
109:
110: /**
111: * Checks, whether there are more groups active.
112: *
113: * @return true if this is the last (outer-most) group.
114: */
115: private boolean isRootGroup(final ProcessState state) {
116: return state.getCurrentGroupIndex() == ProcessState.BEFORE_FIRST_GROUP;
117: }
118:
119: public int getEventCode() {
120: return ReportEvent.GROUP_FINISHED;
121: }
122:
123: public boolean isFinish() {
124: return false;
125: }
126: }
|