KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > labels > StandardXYZToolTipGenerator


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * --------------------------------
27  * StandardXYZToolTipGenerator.java
28  * --------------------------------
29  * (C) Copyright 2004, 2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: StandardXYZToolTipGenerator.java,v 1.5 2005/05/20 08:20:03 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 11-May-2003 : Version 1, split from StandardXYZItemLabelGenerator (DG);
39  * 15-Jul-2004 : Switched getZ() and getZValue() methods (DG);
40  *
41  */

42
43 package org.jfree.chart.labels;
44
45 import java.io.Serializable JavaDoc;
46 import java.text.DateFormat JavaDoc;
47 import java.text.MessageFormat JavaDoc;
48 import java.text.NumberFormat JavaDoc;
49
50 import org.jfree.data.xy.XYDataset;
51 import org.jfree.data.xy.XYZDataset;
52 import org.jfree.util.ObjectUtilities;
53
54 /**
55  * A standard item label generator for use with {@link XYZDataset} data. Each
56  * value can be formatted as a number or as a date.
57  */

58 public class StandardXYZToolTipGenerator extends StandardXYToolTipGenerator
59                                          implements XYZToolTipGenerator,
60                                                     Serializable JavaDoc {
61
62     /** For serialization. */
63     private static final long serialVersionUID = -2961577421889473503L;
64     
65     /** The default tooltip format. */
66     public static final String JavaDoc DEFAULT_TOOL_TIP_FORMAT = "{0}: ({1}, {2}, {3})";
67
68     /**
69      * A number formatter for the z value - if this is null, then zDateFormat
70      * must be non-null.
71      */

72     private NumberFormat JavaDoc zFormat;
73     
74     /**
75      * A date formatter for the z-value - if this is null, then zFormat must be
76      * non-null.
77      */

78     private DateFormat JavaDoc zDateFormat;
79
80     /**
81      * Creates a new tool tip generator using default number formatters for the
82      * x, y and z-values.
83      */

84     public StandardXYZToolTipGenerator() {
85         this(
86             DEFAULT_TOOL_TIP_FORMAT,
87             NumberFormat.getNumberInstance(),
88             NumberFormat.getNumberInstance(),
89             NumberFormat.getNumberInstance()
90         );
91     }
92
93     /**
94      * Constructs a new tool tip generator using the specified number
95      * formatters.
96      *
97      * @param formatString the format string.
98      * @param xFormat the format object for the x values (<code>null</code>
99      * not permitted).
100      * @param yFormat the format object for the y values (<code>null</code>
101      * not permitted).
102      * @param zFormat the format object for the z values (<code>null</code>
103      * not permitted).
104      */

105     public StandardXYZToolTipGenerator(String JavaDoc formatString,
106                                        NumberFormat JavaDoc xFormat,
107                                        NumberFormat JavaDoc yFormat,
108                                        NumberFormat JavaDoc zFormat) {
109         super(formatString, xFormat, yFormat);
110         if (zFormat == null) {
111             throw new IllegalArgumentException JavaDoc("Null 'zFormat' argument.");
112         }
113         this.zFormat = zFormat;
114     }
115
116     /**
117      * Constructs a new tool tip generator using the specified date formatters.
118      *
119      * @param formatString the format string.
120      * @param xFormat the format object for the x values (<code>null</code>
121      * not permitted).
122      * @param yFormat the format object for the y values (<code>null</code>
123      * not permitted).
124      * @param zFormat the format object for the z values (<code>null</code>
125      * not permitted).
126      */

127     public StandardXYZToolTipGenerator(String JavaDoc formatString,
128                                        DateFormat JavaDoc xFormat,
129                                        DateFormat JavaDoc yFormat,
130                                        DateFormat JavaDoc zFormat) {
131         super(formatString, xFormat, yFormat);
132         if (zFormat == null) {
133             throw new IllegalArgumentException JavaDoc("Null 'zFormat' argument.");
134         }
135         this.zDateFormat = zFormat;
136     }
137
138     // TODO: add constructors for combinations of number and date formatters.
139

140     /**
141      * Returns the number formatter for the z-values.
142      *
143      * @return The number formatter (possibly <code>null</code>).
144      */

145     public NumberFormat JavaDoc getZFormat() {
146         return this.zFormat;
147     }
148     
149     /**
150      * Returns the date formatter for the z-values.
151      *
152      * @return The date formatter (possibly <code>null</code>).
153      */

154     public DateFormat JavaDoc getZDateFormat() {
155         return this.zDateFormat;
156     }
157
158     /**
159      * Generates a tool tip text item for a particular item within a series.
160      *
161      * @param dataset the dataset (<code>null</code> not permitted).
162      * @param series the series index (zero-based).
163      * @param item the item index (zero-based).
164      *
165      * @return The tooltip text (possibly <code>null</code>).
166      */

167     public String JavaDoc generateToolTip(XYZDataset dataset, int series, int item) {
168         return generateLabelString(dataset, series, item);
169     }
170     
171     /**
172      * Generates a label string for an item in the dataset.
173      *
174      * @param dataset the dataset (<code>null</code> not permitted).
175      * @param series the series (zero-based index).
176      * @param item the item (zero-based index).
177      *
178      * @return The label (possibly <code>null</code>).
179      */

180     public String JavaDoc generateLabelString(XYDataset dataset, int series, int item) {
181         String JavaDoc result = null;
182         Object JavaDoc[] items = createItemArray((XYZDataset) dataset, series, item);
183         result = MessageFormat.format(getFormatString(), items);
184         return result;
185     }
186
187     /**
188      * Creates the array of items that can be passed to the
189      * {@link MessageFormat} class for creating labels.
190      *
191      * @param dataset the dataset (<code>null</code> not permitted).
192      * @param series the series (zero-based index).
193      * @param item the item (zero-based index).
194      *
195      * @return The items (never <code>null</code>).
196      */

197     protected Object JavaDoc[] createItemArray(XYZDataset dataset,
198                                        int series, int item) {
199
200         Object JavaDoc[] result = new Object JavaDoc[4];
201         result[0] = dataset.getSeriesKey(series).toString();
202         
203         Number JavaDoc x = dataset.getX(series, item);
204         DateFormat JavaDoc xf = getXDateFormat();
205         if (xf != null) {
206             result[1] = xf.format(x);
207         }
208         else {
209             result[1] = getXFormat().format(x);
210         }
211         
212         Number JavaDoc y = dataset.getY(series, item);
213         DateFormat JavaDoc yf = getYDateFormat();
214         if (yf != null) {
215             result[2] = yf.format(y);
216         }
217         else {
218             result[2] = getYFormat().format(y);
219         }
220         
221         Number JavaDoc z = dataset.getZ(series, item);
222         if (this.zDateFormat != null) {
223             result[3] = this.zDateFormat.format(z);
224         }
225         else {
226             result[3] = this.zFormat.format(z);
227         }
228         
229         return result;
230         
231     }
232
233     /**
234      * Tests this object for equality with an arbitrary object.
235      *
236      * @param obj the other object (<code>null</code> permitted).
237      *
238      * @return A boolean.
239      */

240     public boolean equals(Object JavaDoc obj) {
241         if (obj == this) {
242             return true;
243         }
244         if (!(obj instanceof StandardXYZToolTipGenerator)) {
245             return false;
246         }
247         if (!super.equals(obj)) {
248             return false;
249         }
250         StandardXYZToolTipGenerator that = (StandardXYZToolTipGenerator) obj;
251         if (!ObjectUtilities.equal(this.zFormat, that.zFormat)) {
252             return false;
253         }
254         if (!ObjectUtilities.equal(this.zDateFormat, that.zDateFormat)) {
255             return false;
256         }
257         return true;
258
259     }
260
261 }
262
Popular Tags