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: * ItemCountFunction.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.function;
030:
031: import org.jfree.report.Group;
032: import org.jfree.report.event.ReportEvent;
033: import org.jfree.report.states.ReportState;
034: import org.jfree.report.util.IntegerCache;
035:
036: /**
037: * A report function that counts items in a report. If the "group" property is set, the item count is reset to zero
038: * whenever the group changes.
039: *
040: * @author Thomas Morgner
041: */
042: public class ItemCountFunction extends AbstractFunction {
043: /**
044: * The item count.
045: */
046: private transient int count;
047:
048: /**
049: * The name of the group on which to reset the count. This can be set to null to compute the count for the whole
050: * report.
051: */
052: private String group;
053:
054: /**
055: * Constructs an unnamed function. <P> This constructor is intended for use by the SAX handler class only.
056: */
057: public ItemCountFunction() {
058: }
059:
060: /**
061: * Constructs an item count report function.
062: *
063: * @param name The name of the function.
064: * @throws NullPointerException if the name is null
065: */
066: public ItemCountFunction(final String name) {
067: setName(name);
068: }
069:
070: /**
071: * Returns the current count value.
072: *
073: * @return the current count value.
074: */
075: protected int getCount() {
076: return count;
077: }
078:
079: /**
080: * Defines the current count value.
081: *
082: * @param count the current count value.
083: */
084: protected void setCount(final int count) {
085: this .count = count;
086: }
087:
088: /**
089: * Receives notification that a new report is about to start. The item count is set to zero.
090: *
091: * @param event the event.
092: */
093: public void reportInitialized(final ReportEvent event) {
094: setCount(0);
095: }
096:
097: /**
098: * Returns the name of the group (possibly null) for this function. The item count is reset to zero at the start of
099: * each instance of this group.
100: *
101: * @return the group name.
102: */
103: public String getGroup() {
104: return group;
105: }
106:
107: /**
108: * Setss the name of the group for this function. The item count is reset to zero at the start of each instance of
109: * this group. If the name is null, all items in the report are counted.
110: *
111: * @param group The group name.
112: */
113: public void setGroup(final String group) {
114: this .group = group;
115: }
116:
117: /**
118: * Receives notification that a new group is about to start. Checks to see if the group that is starting is the same
119: * as the group defined for this function...if so, the item count is reset to zero.
120: *
121: * @param event Information about the event.
122: */
123: public void groupStarted(final ReportEvent event) {
124: if (getGroup() == null) {
125: return;
126: }
127:
128: final ReportState state = event.getState();
129: final Group group = event.getReport().getGroup(
130: state.getCurrentGroupIndex());
131: if (getGroup().equals(group.getName())) {
132: setCount(0);
133: }
134: }
135:
136: /**
137: * Received notification of a move to the next row of data. Increments the item count.
138: *
139: * @param event Information about the event.
140: */
141: public void itemsAdvanced(final ReportEvent event) {
142: setCount(getCount() + 1);
143: }
144:
145: /**
146: * Returns the number of items counted (so far) by the function. This is either the number of items in the report, or
147: * the group (if a group has been defined for the function).
148: *
149: * @return The item count.
150: */
151: public Object getValue() {
152: return IntegerCache.getInteger(getCount());
153: }
154: }
|