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: * NegativeNumberPaintChangeFunction.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.awt.Color;
032:
033: import org.jfree.report.Band;
034: import org.jfree.report.Element;
035: import org.jfree.report.style.ElementStyleKeys;
036:
037: /**
038: * This function changes the color of the named elements according to the current value of a numeric field. If the value
039: * of the field is not numeric (or null), the positive color is set.
040: *
041: * @author Thomas Morgner
042: * @deprecated The same thing can be achieved using a simple StyleExpression on the element's PAINT stylekey.
043: */
044: public class NegativeNumberPaintChangeFunction extends
045: AbstractElementFormatFunction {
046: /**
047: * The name of the data-row column from where to read the numeric value.
048: */
049: private String field;
050: /**
051: * The color that is used for positive values.
052: */
053: private Color positiveColor;
054: /**
055: * The color that is used for negative values.
056: */
057: private Color negativeColor;
058: /**
059: * The color that is used for zero values.
060: */
061: private Color zeroColor;
062:
063: /**
064: * Default Constructor.
065: */
066: public NegativeNumberPaintChangeFunction() {
067: }
068:
069: /**
070: * Applies the computed color to all elements with the defined name.
071: *
072: * @param b the band to which the color is applied.
073: */
074: protected void processRootBand(final Band b) {
075: final Element[] elements = FunctionUtilities.findAllElements(b,
076: getElement());
077: if (elements.length == 0) {
078: return;
079: }
080:
081: final Color color = computeColor();
082: for (int i = 0; i < elements.length; i++) {
083: elements[i].getStyle().setStyleProperty(
084: ElementStyleKeys.PAINT, color);
085: }
086: }
087:
088: /**
089: * Computes the color that is applied to the elements.
090: *
091: * @return the computed color.
092: */
093: protected Color computeColor() {
094: final Object o = getDataRow().get(getField());
095: if (o instanceof Number == false) {
096: return getPositiveColor();
097: }
098: final Number n = (Number) o;
099: final double d = n.doubleValue();
100: if (d < 0) {
101: return getNegativeColor();
102: }
103: if (d > 0) {
104: return getPositiveColor();
105: }
106: final Color zeroColor = getZeroColor();
107: if (zeroColor == null) {
108: return getPositiveColor();
109: }
110: return zeroColor;
111: }
112:
113: /**
114: * Returns the color that is used if the number read from the field is positive.
115: *
116: * @return the color for positive values.
117: */
118: public Color getPositiveColor() {
119: return positiveColor;
120: }
121:
122: /**
123: * Defines the color that is used if the number read from the field is positive.
124: *
125: * @param positiveColor the color for positive values.
126: */
127: public void setPositiveColor(final Color positiveColor) {
128: this .positiveColor = positiveColor;
129: }
130:
131: /**
132: * Returns the color that is used if the number read from the field is negative.
133: *
134: * @return the color for negative values.
135: */
136: public Color getNegativeColor() {
137: return negativeColor;
138: }
139:
140: /**
141: * Defines the color that is used if the number read from the field is negative.
142: *
143: * @param negativeColor the color for negative values.
144: */
145: public void setNegativeColor(final Color negativeColor) {
146: this .negativeColor = negativeColor;
147: }
148:
149: /**
150: * Returns the color that is used if the number read from the field is zero.
151: *
152: * @return the color for zero values.
153: */
154: public Color getZeroColor() {
155: return zeroColor;
156: }
157:
158: /**
159: * Defines the color that is used if the number read from the field is zero.
160: *
161: * @param zeroColor the color for zero values.
162: */
163: public void setZeroColor(final Color zeroColor) {
164: this .zeroColor = zeroColor;
165: }
166:
167: /**
168: * Returns the name of the data-row column from where to read the numeric value.
169: *
170: * @return the field name.
171: */
172: public String getField() {
173: return field;
174: }
175:
176: /**
177: * Defines the name of the data-row column from where to read the numeric value.
178: *
179: * @param field the field name.
180: */
181: public void setField(final String field) {
182: this.field = field;
183: }
184: }
|