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: * StateUtilities.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.states;
030:
031: import java.util.Arrays;
032: import java.util.Comparator;
033: import java.util.HashSet;
034: import java.io.Serializable;
035:
036: import org.jfree.report.Group;
037: import org.jfree.report.JFreeReport;
038: import org.jfree.report.RootLevelBand;
039: import org.jfree.report.SubReport;
040: import org.jfree.report.function.Expression;
041: import org.jfree.report.function.StructureFunction;
042: import org.jfree.report.util.IntegerCache;
043:
044: /**
045: * Creation-Date: Dec 14, 2006, 7:59:39 PM
046: *
047: * @author Thomas Morgner
048: */
049: public class StateUtilities {
050: /**
051: * A comparator for levels in descending order.
052: */
053: private static final class DescendingComparator implements
054: Comparator, Serializable {
055: /**
056: * Default constructor.
057: */
058: private DescendingComparator() {
059: }
060:
061: /**
062: * Compares its two arguments for order. Returns a negative integer, zero, or a
063: * positive integer as the first argument is less than, equal to, or greater than the
064: * second.<p>
065: *
066: * @param o1 the first object to be compared.
067: * @param o2 the second object to be compared.
068: * @return a negative integer, zero, or a positive integer as the first argument is
069: * less than, equal to, or greater than the second.
070: *
071: * @throws ClassCastException if the arguments' types prevent them from being compared
072: * by this Comparator.
073: */
074: public int compare(final Object o1, final Object o2) {
075: if ((o1 instanceof Comparable) == false) {
076: throw new ClassCastException("Need comparable Elements");
077: }
078: if ((o2 instanceof Comparable) == false) {
079: throw new ClassCastException("Need comparable Elements");
080: }
081: final Comparable c1 = (Comparable) o1;
082: final Comparable c2 = (Comparable) o2;
083: return -1 * c1.compareTo(c2);
084: }
085: }
086:
087: private StateUtilities() {
088: }
089:
090: public static int[] computeLevels(final JFreeReport report,
091: final LayoutProcess lp) {
092: final HashSet levels = new HashSet();
093: // levels.add(IntegerCache.getInteger(0)); // A report without expressions needs no function evaluation run ..
094:
095: final StructureFunction[] collectionFunctions = lp
096: .getCollectionFunctions();
097: for (int i = 0; i < collectionFunctions.length; i++) {
098: final StructureFunction function = collectionFunctions[i];
099: levels.add(IntegerCache.getInteger(function
100: .getDependencyLevel()));
101: }
102: levels.add(IntegerCache
103: .getInteger(LayoutProcess.LEVEL_PAGINATE));
104:
105: collectLevels(report, levels);
106: final Integer[] levelList = (Integer[]) levels
107: .toArray(new Integer[levels.size()]);
108: Arrays.sort(levelList, new DescendingComparator());
109: final int[] retval = new int[levelList.length];
110: for (int i = 0; i < retval.length; i++) {
111: retval[i] = levelList[i].intValue();
112: }
113: return retval;
114: }
115:
116: private static void collectLevels(final JFreeReport report,
117: final HashSet levels) {
118: final Expression[] expressions = report.getExpressions()
119: .getExpressions();
120:
121: for (int i = 0; i < expressions.length; i++) {
122: final Expression expression = expressions[i];
123: levels.add(IntegerCache.getInteger(expression
124: .getDependencyLevel()));
125: }
126:
127: collectLevels(report.getItemBand(), levels);
128: collectLevels(report.getReportFooter(), levels);
129: collectLevels(report.getReportHeader(), levels);
130:
131: final int groupCount = report.getGroupCount();
132: for (int i = 0; i < groupCount; i++) {
133: final Group g = report.getGroup(i);
134: collectLevels(g.getFooter(), levels);
135: collectLevels(g.getHeader(), levels);
136: }
137: }
138:
139: private static void collectLevels(final SubReport report,
140: final HashSet levels) {
141: final Expression[] expressions = report.getExpressions()
142: .getExpressions();
143:
144: for (int i = 0; i < expressions.length; i++) {
145: final Expression expression = expressions[i];
146: levels.add(IntegerCache.getInteger(expression
147: .getDependencyLevel()));
148: }
149:
150: collectLevels(report.getItemBand(), levels);
151: collectLevels(report.getReportFooter(), levels);
152: collectLevels(report.getReportHeader(), levels);
153:
154: final int groupCount = report.getGroupCount();
155: for (int i = 0; i < groupCount; i++) {
156: final Group g = report.getGroup(i);
157: collectLevels(g.getFooter(), levels);
158: collectLevels(g.getHeader(), levels);
159: }
160: }
161:
162: private static void collectLevels(
163: final RootLevelBand rootLevelBand, final HashSet levels) {
164: final int sc = rootLevelBand.getSubReportCount();
165: for (int i = 0; i < sc; i++) {
166: final SubReport sr = rootLevelBand.getSubReport(i);
167: collectLevels(sr, levels);
168: }
169: }
170: }
|