001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, 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: * BubbleXYItemLabelGenerator.java
029: * -------------------------------
030: * (C) Copyright 2005, 2006, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: BubbleXYItemLabelGenerator.java,v 1.1.2.1 2006/01/27 12:51:21 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 13-Dec-2005 : Version 1, based on StandardXYZToolTipGenerator (DG);
040: * 26-Jan-2006 : Renamed StandardXYZItemLabelGenerator
041: * --> BubbleXYItemLabelGenerator (DG);
042: *
043: */
044:
045: package org.jfree.chart.labels;
046:
047: import java.io.Serializable;
048: import java.text.DateFormat;
049: import java.text.MessageFormat;
050: import java.text.NumberFormat;
051:
052: import org.jfree.chart.renderer.xy.XYBubbleRenderer;
053: import org.jfree.data.xy.XYDataset;
054: import org.jfree.data.xy.XYZDataset;
055: import org.jfree.util.ObjectUtilities;
056:
057: /**
058: * An item label generator defined for use with the {@link XYBubbleRenderer}
059: * class, or any other class that uses an {@link XYZDataset}.
060: *
061: * @since 1.0.1
062: */
063: public class BubbleXYItemLabelGenerator extends
064: AbstractXYItemLabelGenerator implements XYItemLabelGenerator,
065: Serializable {
066:
067: static final long serialVersionUID = -8458568928021240922L;
068:
069: /** The default item label format. */
070: public static final String DEFAULT_FORMAT_STRING = "{3}";
071:
072: /**
073: * A number formatter for the z value - if this is <code>null</code>, then
074: * zDateFormat must be non-null.
075: */
076: private NumberFormat zFormat;
077:
078: /**
079: * A date formatter for the z-value - if this is null, then zFormat must be
080: * non-null.
081: */
082: private DateFormat zDateFormat;
083:
084: /**
085: * Creates a new tool tip generator using default number formatters for the
086: * x, y and z-values.
087: */
088: public BubbleXYItemLabelGenerator() {
089: this (DEFAULT_FORMAT_STRING, NumberFormat.getNumberInstance(),
090: NumberFormat.getNumberInstance(), NumberFormat
091: .getNumberInstance());
092: }
093:
094: /**
095: * Constructs a new tool tip generator using the specified number
096: * formatters.
097: *
098: * @param formatString the format string.
099: * @param xFormat the format object for the x values (<code>null</code>
100: * not permitted).
101: * @param yFormat the format object for the y values (<code>null</code>
102: * not permitted).
103: * @param zFormat the format object for the z values (<code>null</code>
104: * not permitted).
105: */
106: public BubbleXYItemLabelGenerator(String formatString,
107: NumberFormat xFormat, NumberFormat yFormat,
108: NumberFormat zFormat) {
109: super (formatString, xFormat, yFormat);
110: if (zFormat == null) {
111: throw new IllegalArgumentException(
112: "Null 'zFormat' argument.");
113: }
114: this .zFormat = zFormat;
115: }
116:
117: /**
118: * Constructs a new item label generator using the specified date
119: * formatters.
120: *
121: * @param formatString the format string.
122: * @param xFormat the format object for the x values (<code>null</code>
123: * not permitted).
124: * @param yFormat the format object for the y values (<code>null</code>
125: * not permitted).
126: * @param zFormat the format object for the z values (<code>null</code>
127: * not permitted).
128: */
129: public BubbleXYItemLabelGenerator(String formatString,
130: DateFormat xFormat, DateFormat yFormat, DateFormat zFormat) {
131: super (formatString, xFormat, yFormat);
132: if (zFormat == null) {
133: throw new IllegalArgumentException(
134: "Null 'zFormat' argument.");
135: }
136: this .zDateFormat = zFormat;
137: }
138:
139: /**
140: * Returns the number formatter for the z-values.
141: *
142: * @return The number formatter (possibly <code>null</code>).
143: */
144: public NumberFormat getZFormat() {
145: return this .zFormat;
146: }
147:
148: /**
149: * Returns the date formatter for the z-values.
150: *
151: * @return The date formatter (possibly <code>null</code>).
152: */
153: public DateFormat getZDateFormat() {
154: return this .zDateFormat;
155: }
156:
157: /**
158: * Generates an item label for a particular item within a series.
159: *
160: * @param dataset the dataset (<code>null</code> not permitted).
161: * @param series the series index (zero-based).
162: * @param item the item index (zero-based).
163: *
164: * @return The item label (possibly <code>null</code>).
165: */
166: public String generateLabel(XYDataset dataset, int series, int item) {
167: return generateLabelString(dataset, series, item);
168: }
169:
170: /**
171: * Generates a label string for an item in the dataset.
172: *
173: * @param dataset the dataset (<code>null</code> not permitted).
174: * @param series the series (zero-based index).
175: * @param item the item (zero-based index).
176: *
177: * @return The label (possibly <code>null</code>).
178: */
179: public String generateLabelString(XYDataset dataset, int series,
180: int item) {
181: String result = null;
182: Object[] items = null;
183: if (dataset instanceof XYZDataset) {
184: items = createItemArray((XYZDataset) dataset, series, item);
185: } else {
186: items = createItemArray(dataset, series, item);
187: }
188: result = MessageFormat.format(getFormatString(), items);
189: return result;
190: }
191:
192: /**
193: * Creates the array of items that can be passed to the
194: * {@link MessageFormat} class for creating labels.
195: *
196: * @param dataset the dataset (<code>null</code> not permitted).
197: * @param series the series (zero-based index).
198: * @param item the item (zero-based index).
199: *
200: * @return The items (never <code>null</code>).
201: */
202: protected Object[] createItemArray(XYZDataset dataset, int series,
203: int item) {
204:
205: Object[] result = new Object[4];
206: result[0] = dataset.getSeriesKey(series).toString();
207:
208: Number x = dataset.getX(series, item);
209: DateFormat xf = getXDateFormat();
210: if (xf != null) {
211: result[1] = xf.format(x);
212: } else {
213: result[1] = getXFormat().format(x);
214: }
215:
216: Number y = dataset.getY(series, item);
217: DateFormat yf = getYDateFormat();
218: if (yf != null) {
219: result[2] = yf.format(y);
220: } else {
221: result[2] = getYFormat().format(y);
222: }
223:
224: Number z = dataset.getZ(series, item);
225: if (this .zDateFormat != null) {
226: result[3] = this .zDateFormat.format(z);
227: } else {
228: result[3] = this .zFormat.format(z);
229: }
230:
231: return result;
232:
233: }
234:
235: /**
236: * Tests this object for equality with an arbitrary object.
237: *
238: * @param obj the other object (<code>null</code> permitted).
239: *
240: * @return A boolean.
241: */
242: public boolean equals(Object obj) {
243: if (obj == this ) {
244: return true;
245: }
246: if (!(obj instanceof BubbleXYItemLabelGenerator)) {
247: return false;
248: }
249: if (!super .equals(obj)) {
250: return false;
251: }
252: BubbleXYItemLabelGenerator that = (BubbleXYItemLabelGenerator) obj;
253: if (!ObjectUtilities.equal(this .zFormat, that.zFormat)) {
254: return false;
255: }
256: if (!ObjectUtilities.equal(this .zDateFormat, that.zDateFormat)) {
257: return false;
258: }
259: return true;
260: }
261:
262: }
|