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: * FunctionUtilities.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.ArrayList;
032:
033: import org.jfree.report.Band;
034: import org.jfree.report.Element;
035: import org.jfree.report.Group;
036: import org.jfree.report.event.ReportEvent;
037:
038: /**
039: * A collection of utility methods relating to functions.
040: *
041: * @author Thomas Morgner.
042: */
043: public final class FunctionUtilities {
044:
045: /**
046: * Default Constructor.
047: */
048: private FunctionUtilities() {
049: }
050:
051: /**
052: * Try to find the defined element in the last active root-band.
053: *
054: * @param band the band that is suspected to contain the element.
055: * @param element the element name.
056: * @return the found element or null, if no element could be found.
057: */
058: public static Element findElement(final Band band,
059: final String element) {
060: if (element == null) {
061: throw new NullPointerException(
062: "Element name must not be null");
063: }
064:
065: final Element[] elements = band.getElementArray();
066: if (band.getName().equals(element)) {
067: return band;
068: }
069:
070: for (int i = 0; i < elements.length; i++) {
071: final Element e = elements[i];
072: if (e.getName().equals(element)) {
073: return e;
074: }
075: if (e instanceof Band) {
076: final Element retval = findElement((Band) e, element);
077: if (retval != null) {
078: return retval;
079: }
080: }
081: }
082: return null;
083: }
084:
085: /**
086: * Try to find the defined element in the last active root-band.
087: *
088: * @param band the band that is suspected to contain the element.
089: * @param element the element name.
090: * @return the found element or null, if no element could be found.
091: */
092: public static Element[] findAllElements(final Band band,
093: final String element) {
094: if (element == null) {
095: throw new NullPointerException(
096: "Element name must not be null");
097: }
098: final ArrayList collector = new ArrayList();
099: if (band.getName().equals(element)) {
100: collector.add(band);
101: }
102: performFindElement(band, element, collector);
103: return (Element[]) collector.toArray(new Element[collector
104: .size()]);
105: }
106:
107: /**
108: * Internal function that collects all elements of a given band with a given name.
109: *
110: * @param band the band from which elements should be collected.
111: * @param element the name of the element to collect.
112: * @param collector the list of results.
113: */
114: private static void performFindElement(final Band band,
115: final String element, final ArrayList collector) {
116: final Element[] elements = band.getElementArray();
117: for (int i = 0; i < elements.length; i++) {
118: final Element e = elements[i];
119: if (e.getName().equals(element)) {
120: collector.add(e);
121: }
122: if (e instanceof Band) {
123: performFindElement((Band) e, element, collector);
124: }
125: }
126: }
127:
128: /**
129: * Returns true if the events current groupname is equal to the group name.
130: *
131: * @param groupName the group name.
132: * @param event the report event.
133: * @return A boolean.
134: */
135: public static boolean isDefinedGroup(final String groupName,
136: final ReportEvent event) {
137: if (groupName == null) {
138: return false;
139: }
140:
141: final Group group = event.getReport().getGroup(
142: event.getState().getCurrentGroupIndex());
143: return groupName.equals(group.getName());
144: }
145:
146: /**
147: * Returns true, if the current run level is defined for the given function and this is
148: * a prepare run. The prepare run is used to compute the function values.
149: *
150: * @param f the function.
151: * @param event the event.
152: * @return A boolean.
153: */
154: public static boolean isDefinedPrepareRunLevel(final Function f,
155: final ReportEvent event) {
156: if (f == null) {
157: throw new NullPointerException("Function is null");
158: }
159:
160: if (event == null) {
161: throw new NullPointerException("ReportEvent is null");
162: }
163:
164: if (event.getState().isPrepareRun() == false) {
165: return false;
166: }
167: return (event.getState().getLevel() == f.getDependencyLevel());
168: }
169:
170: /**
171: * Returns true or false.
172: *
173: * @param event the report event.
174: * @return A boolean.
175: */
176: public static boolean isLayoutLevel(final ReportEvent event) {
177: if (event == null) {
178: throw new NullPointerException("ReportEvent is null");
179: }
180: return (event.getState().getLevel() < 0);
181: }
182:
183: /**
184: * Returns the current group instance, based on the given report event.
185: *
186: * @param event the event which is base for the action.
187: * @return the current group of the event, never null.
188: */
189: public static Group getCurrentGroup(final ReportEvent event) {
190: if (event == null) {
191: throw new NullPointerException("ReportEvent is null");
192: }
193: return event.getReport().getGroup(
194: event.getState().getCurrentGroupIndex());
195: }
196: }
|