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