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: * PageTotalFunction.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.function;
030:
031: import java.util.HashMap;
032:
033: import org.jfree.report.event.ReportEvent;
034: import org.jfree.report.states.ReportStateKey;
035:
036: /**
037: * Prints the total number of pages of an report. If a group is specified, this function expects the group to have the
038: * manual pagebreak enabled.
039: * <p/>
040: * This function will only work as expected in group mode if the named group has pagebreak-before set to true on the
041: * header for pagebreak-after set to true on the group's footer.
042: *
043: * @author Thomas Morgner
044: */
045: public class PageTotalFunction extends PageFunction {
046: /**
047: * A map of results, keyed by the process-key.
048: */
049: private HashMap groupPages;
050:
051: /**
052: * The state key is used to store the result for the report.
053: */
054: private transient ReportStateKey currentStateKey;
055: /**
056: * An internal state flag.
057: */
058: private boolean firstGroupSeen;
059:
060: /**
061: * Creates a new page total function.
062: */
063: public PageTotalFunction() {
064: this .groupPages = new HashMap();
065: setDependencyLevel(0);
066: }
067:
068: /**
069: * Receives notification that the report has started.
070: *
071: * @param event the event.
072: */
073: public void reportInitialized(final ReportEvent event) {
074: super .reportInitialized(event);
075: if (event.isDeepTraversing()) {
076: return;
077: }
078:
079: firstGroupSeen = false;
080: currentStateKey = event.getState().getProcessKey();
081: }
082:
083: public void groupStarted(final ReportEvent event) {
084: super .groupStarted(event);
085: if (event.isDeepTraversing()) {
086: return;
087: }
088:
089: if (getGroup() == null) {
090: return;
091: }
092:
093: if (FunctionUtilities.isDefinedGroup(getGroup(), event)) {
094: if (firstGroupSeen == false) {
095: firstGroupSeen = true;
096: return;
097: }
098:
099: currentStateKey = event.getState().getProcessKey();
100: }
101: }
102:
103: public void groupFinished(final ReportEvent event) {
104: super .groupFinished(event);
105: if (event.isDeepTraversing()) {
106: return;
107: }
108:
109: if (getGroup() == null) {
110: return;
111: }
112:
113: if (FunctionUtilities.isDefinedGroup(getGroup(), event)) {
114: if (event.getState().isPrepareRun()) {
115: groupPages.put(currentStateKey, super .getValue());
116: }
117: }
118: }
119:
120: public void pageFinished(final ReportEvent event) {
121: if (event.getState().isPrepareRun()) {
122: groupPages.put(currentStateKey, super .getValue());
123: }
124: }
125:
126: public void reportDone(final ReportEvent event) {
127: if (event.getState().isPrepareRun()) {
128: groupPages.put(currentStateKey, super .getValue());
129: }
130: }
131:
132: /**
133: * Return a completly separated copy of this function. The copy does no longer share any changeable objects with the
134: * original function.
135: *
136: * @return a copy of this function.
137: */
138: public Expression getInstance() {
139: final PageTotalFunction function = (PageTotalFunction) super
140: .getInstance();
141: function.groupPages = new HashMap();
142: return function;
143: }
144:
145: public Object getValue() {
146: // Log.debug ("Returning " + currentStateKey);
147: return groupPages.get(currentStateKey);
148: }
149:
150: }
|