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: * SheetNameFunction.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.function.sys;
030:
031: import org.jfree.report.Band;
032: import org.jfree.report.event.ReportEvent;
033: import org.jfree.report.function.AbstractElementFormatFunction;
034: import org.jfree.report.function.StructureFunction;
035: import org.jfree.report.states.LayoutProcess;
036: import org.jfree.report.style.BandStyleKeys;
037: import org.jfree.util.Log;
038:
039: /**
040: * This function is used to generate sheet names into table exports. Sheet names are generated on page breaks and have
041: * different representations depending the export type.<br/>
042: * To use this functionnality report configuration must set the property {@link #DECALRED_SHEETNAME_FUNCTION_KEY} to
043: * point to an existing function or property accessible within the report.
044: * <p/>
045: * As for example using simple report definition:<br/>
046: * <pre>
047: * <report>
048: * <configuration>
049: * <!-- where sheetNameExpression is pointing to a valid function declared in this report -->
050: * <property name="org.jfree.report.targets.table.TableWriter.SheetNameFunction">sheetNameExpression</property>
051: * </configuration>
052: * ...
053: * </report>
054: * </pre>
055: * This way of defining a sheetname for elements is deprecated. Sheetnames should be declared or computed directly
056: * on the bands by specifiing a sheetname using the "computed-sheetname" style-property.
057: *
058: * @author Cedric Pronzato
059: */
060: public class SheetNameFunction extends AbstractElementFormatFunction
061: implements StructureFunction {
062: /**
063: * The configuration property declaring the function name to call in order to generate sheet names.<br/>
064: */
065: private static final String DECALRED_SHEETNAME_FUNCTION_KEY = "org.jfree.report.targets.table.TableWriter.SheetNameFunction";
066:
067: /** A property that holds the last computed value of the sheetname function. */
068: private transient String lastValue;
069: /** A property that holds the name of the column from where to receive the sheetname. */
070: private transient String functionToCall;
071:
072: /**
073: * Default constructor.
074: */
075: public SheetNameFunction() {
076: }
077:
078: public void reportInitialized(final ReportEvent event) {
079: super .reportInitialized(event);
080: functionToCall = this .getReportConfiguration()
081: .getConfigProperty(DECALRED_SHEETNAME_FUNCTION_KEY);
082: }
083:
084: /**
085: * Overrides the dependency level to only execute this function on the pagination and content-generation level.
086: * @return LayoutProcess.LEVEL_PAGINATE.
087: */
088: public int getDependencyLevel() {
089: return LayoutProcess.LEVEL_PAGINATE;
090: }
091:
092: /**
093: * Sets the sheet name value to the current <code>Band</code>
094: * {@link org.jfree.report.style.BandStyleKeys#COMPUTED_SHEETNAME} style key.
095: *
096: * @param b The current band element.
097: */
098: protected void processRootBand(final Band b) {
099: lastValue = null;
100: // if exporting to a table/* export
101: if (getRuntime().getExportDescriptor().startsWith("table/")) {
102: if (functionToCall != null) {
103: // evaluating the declared sheetname function
104: final Object value = this .getDataRow().get(
105: functionToCall);
106: if (value == null) {
107: Log
108: .debug("Cannot find the sheetname function/property referenced by '"
109: + functionToCall + '\'');
110: } else {
111: // setting the value as a style property
112: lastValue = value.toString();
113: b.getStyle()
114: .setStyleProperty(
115: BandStyleKeys.COMPUTED_SHEETNAME,
116: lastValue);
117: }
118: }
119: }
120: }
121:
122: /**
123: * Structure functions do not care of the result so this method should never be called.
124: *
125: * @return <code>null</code>
126: */
127: public Object getValue() {
128: return lastValue;
129: }
130: }
|