001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ---------------------------------
028: * AbstractXYItemLabelGenerator.java
029: * ---------------------------------
030: * (C) Copyright 2004-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: AbstractXYItemLabelGenerator.java,v 1.9.2.4 2007/01/25 11:54:23 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 27-Feb-2004 : Version 1 (DG);
040: * 12-May-2004 : Moved default tool tip format to
041: * StandardXYToolTipGenerator (DG);
042: * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
043: * getYValue() (DG);
044: * 08-Oct-2004 : Modified createItemArray() method to handle null values (DG);
045: * 10-Jan-2005 : Updated createItemArray() to use x, y primitives if
046: * possible (DG);
047: * ------------- JFREECHART 1.0.x --------------------------------------------
048: * 26-Jan-2006 : Minor API doc update (DG);
049: * 25-Jan-2007 : Added new constructor and fixed bug in clone() method (DG);
050: *
051: */
052:
053: package org.jfree.chart.labels;
054:
055: import java.io.Serializable;
056: import java.text.DateFormat;
057: import java.text.MessageFormat;
058: import java.text.NumberFormat;
059: import java.util.Date;
060:
061: import org.jfree.data.xy.XYDataset;
062: import org.jfree.util.ObjectUtilities;
063:
064: /**
065: * A base class for creating item label generators.
066: */
067: public class AbstractXYItemLabelGenerator implements Cloneable,
068: Serializable {
069:
070: /** For serialization. */
071: private static final long serialVersionUID = 5869744396278660636L;
072:
073: /** The item label format string. */
074: private String formatString;
075:
076: /** A number formatter for the x value. */
077: private NumberFormat xFormat;
078:
079: /** A date formatter for the x value. */
080: private DateFormat xDateFormat;
081:
082: /** A formatter for the y value. */
083: private NumberFormat yFormat;
084:
085: /** A date formatter for the y value. */
086: private DateFormat yDateFormat;
087:
088: /** The string used to represent 'null' for the x-value. */
089: private String nullXString = "null";
090:
091: /** The string used to represent 'null' for the y-value. */
092: private String nullYString = "null";
093:
094: /**
095: * Creates an item label generator using default number formatters.
096: */
097: protected AbstractXYItemLabelGenerator() {
098: this ("{2}", NumberFormat.getNumberInstance(), NumberFormat
099: .getNumberInstance());
100: }
101:
102: /**
103: * Creates an item label generator using the specified number formatters.
104: *
105: * @param formatString the item label format string (<code>null</code>
106: * not permitted).
107: * @param xFormat the format object for the x values (<code>null</code>
108: * not permitted).
109: * @param yFormat the format object for the y values (<code>null</code>
110: * not permitted).
111: */
112: protected AbstractXYItemLabelGenerator(String formatString,
113: NumberFormat xFormat, NumberFormat yFormat) {
114:
115: if (formatString == null) {
116: throw new IllegalArgumentException(
117: "Null 'formatString' argument.");
118: }
119: if (xFormat == null) {
120: throw new IllegalArgumentException(
121: "Null 'xFormat' argument.");
122: }
123: if (yFormat == null) {
124: throw new IllegalArgumentException(
125: "Null 'yFormat' argument.");
126: }
127: this .formatString = formatString;
128: this .xFormat = xFormat;
129: this .yFormat = yFormat;
130:
131: }
132:
133: /**
134: * Creates an item label generator using the specified number formatters.
135: *
136: * @param formatString the item label format string (<code>null</code>
137: * not permitted).
138: * @param xFormat the format object for the x values (<code>null</code>
139: * permitted).
140: * @param yFormat the format object for the y values (<code>null</code>
141: * not permitted).
142: */
143: protected AbstractXYItemLabelGenerator(String formatString,
144: DateFormat xFormat, NumberFormat yFormat) {
145:
146: this (formatString, NumberFormat.getInstance(), yFormat);
147: this .xDateFormat = xFormat;
148:
149: }
150:
151: /**
152: * Creates an item label generator using the specified formatters (a
153: * number formatter for the x-values and a date formatter for the
154: * y-values).
155: *
156: * @param formatString the item label format string (<code>null</code>
157: * not permitted).
158: * @param xFormat the format object for the x values (<code>null</code>
159: * permitted).
160: * @param yFormat the format object for the y values (<code>null</code>
161: * not permitted).
162: *
163: * @since 1.0.4
164: */
165: protected AbstractXYItemLabelGenerator(String formatString,
166: NumberFormat xFormat, DateFormat yFormat) {
167:
168: this (formatString, xFormat, NumberFormat.getInstance());
169: this .yDateFormat = yFormat;
170: }
171:
172: /**
173: * Creates an item label generator using the specified number formatters.
174: *
175: * @param formatString the item label format string (<code>null</code>
176: * not permitted).
177: * @param xFormat the format object for the x values (<code>null</code>
178: * permitted).
179: * @param yFormat the format object for the y values (<code>null</code>
180: * not permitted).
181: */
182: protected AbstractXYItemLabelGenerator(String formatString,
183: DateFormat xFormat, DateFormat yFormat) {
184:
185: this (formatString, NumberFormat.getInstance(), NumberFormat
186: .getInstance());
187: this .xDateFormat = xFormat;
188: this .yDateFormat = yFormat;
189:
190: }
191:
192: /**
193: * Returns the format string (this controls the overall structure of the
194: * label).
195: *
196: * @return The format string (never <code>null</code>).
197: */
198: public String getFormatString() {
199: return this .formatString;
200: }
201:
202: /**
203: * Returns the number formatter for the x-values.
204: *
205: * @return The number formatter (possibly <code>null</code>).
206: */
207: public NumberFormat getXFormat() {
208: return this .xFormat;
209: }
210:
211: /**
212: * Returns the date formatter for the x-values.
213: *
214: * @return The date formatter (possibly <code>null</code>).
215: */
216: public DateFormat getXDateFormat() {
217: return this .xDateFormat;
218: }
219:
220: /**
221: * Returns the number formatter for the y-values.
222: *
223: * @return The number formatter (possibly <code>null</code>).
224: */
225: public NumberFormat getYFormat() {
226: return this .yFormat;
227: }
228:
229: /**
230: * Returns the date formatter for the y-values.
231: *
232: * @return The date formatter (possibly <code>null</code>).
233: */
234: public DateFormat getYDateFormat() {
235: return this .yDateFormat;
236: }
237:
238: /**
239: * Generates a label string for an item in the dataset.
240: *
241: * @param dataset the dataset (<code>null</code> not permitted).
242: * @param series the series (zero-based index).
243: * @param item the item (zero-based index).
244: *
245: * @return The label (possibly <code>null</code>).
246: */
247: public String generateLabelString(XYDataset dataset, int series,
248: int item) {
249: String result = null;
250: Object[] items = createItemArray(dataset, series, item);
251: result = MessageFormat.format(this .formatString, items);
252: return result;
253: }
254:
255: /**
256: * Creates the array of items that can be passed to the
257: * {@link MessageFormat} class for creating labels.
258: *
259: * @param dataset the dataset (<code>null</code> not permitted).
260: * @param series the series (zero-based index).
261: * @param item the item (zero-based index).
262: *
263: * @return An array of three items from the dataset formatted as
264: * <code>String</code> objects (never <code>null</code>).
265: */
266: protected Object[] createItemArray(XYDataset dataset, int series,
267: int item) {
268: Object[] result = new Object[3];
269: result[0] = dataset.getSeriesKey(series).toString();
270:
271: double x = dataset.getXValue(series, item);
272: if (Double.isNaN(x) && dataset.getX(series, item) == null) {
273: result[1] = this .nullXString;
274: } else {
275: if (this .xDateFormat != null) {
276: result[1] = this .xDateFormat.format(new Date((long) x));
277: } else {
278: result[1] = this .xFormat.format(x);
279: }
280: }
281:
282: double y = dataset.getYValue(series, item);
283: if (Double.isNaN(y) && dataset.getY(series, item) == null) {
284: result[2] = this .nullYString;
285: } else {
286: if (this .yDateFormat != null) {
287: result[2] = this .yDateFormat.format(new Date((long) y));
288: } else {
289: result[2] = this .yFormat.format(y);
290: }
291: }
292: return result;
293: }
294:
295: /**
296: * Tests this object for equality with an arbitrary object.
297: *
298: * @param obj the other object (<code>null</code> permitted).
299: *
300: * @return A boolean.
301: */
302: public boolean equals(Object obj) {
303: if (obj == this ) {
304: return true;
305: }
306: if (!(obj instanceof AbstractXYItemLabelGenerator)) {
307: return false;
308: }
309: AbstractXYItemLabelGenerator that = (AbstractXYItemLabelGenerator) obj;
310: if (!this .formatString.equals(that.formatString)) {
311: return false;
312: }
313: if (!ObjectUtilities.equal(this .xFormat, that.xFormat)) {
314: return false;
315: }
316: if (!ObjectUtilities.equal(this .xDateFormat, that.xDateFormat)) {
317: return false;
318: }
319: if (!ObjectUtilities.equal(this .yFormat, that.yFormat)) {
320: return false;
321: }
322: if (!ObjectUtilities.equal(this .yDateFormat, that.yDateFormat)) {
323: return false;
324: }
325: return true;
326: }
327:
328: /**
329: * Returns an independent copy of the generator.
330: *
331: * @return A clone.
332: *
333: * @throws CloneNotSupportedException if cloning is not supported.
334: */
335: public Object clone() throws CloneNotSupportedException {
336: AbstractXYItemLabelGenerator clone = (AbstractXYItemLabelGenerator) super
337: .clone();
338: if (this .xFormat != null) {
339: clone.xFormat = (NumberFormat) this .xFormat.clone();
340: }
341: if (this .yFormat != null) {
342: clone.yFormat = (NumberFormat) this .yFormat.clone();
343: }
344: if (this .xDateFormat != null) {
345: clone.xDateFormat = (DateFormat) this .xDateFormat.clone();
346: }
347: if (this .yDateFormat != null) {
348: clone.yDateFormat = (DateFormat) this.yDateFormat.clone();
349: }
350: return clone;
351: }
352:
353: }
|