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 AbstractXYItemLabelGenerator
064        implements XYItemLabelGenerator, Serializable {
065        
066        static final long serialVersionUID = -8458568928021240922L;
067    
068        /** The default item label format. */
069        public static final String DEFAULT_FORMAT_STRING = "{3}";
070    
071        /** 
072         * A number formatter for the z value - if this is <code>null</code>, then 
073         * zDateFormat must be non-null. 
074         */
075        private NumberFormat zFormat;
076        
077        /** 
078         * A date formatter for the z-value - if this is null, then zFormat must be 
079         * non-null. 
080         */
081        private DateFormat zDateFormat;
082    
083        /**
084         * Creates a new tool tip generator using default number formatters for the
085         * x, y and z-values.
086         */
087        public BubbleXYItemLabelGenerator() {
088            this(DEFAULT_FORMAT_STRING, NumberFormat.getNumberInstance(),
089                    NumberFormat.getNumberInstance(), 
090                    NumberFormat.getNumberInstance());
091        }
092    
093        /**
094         * Constructs a new tool tip generator using the specified number 
095         * formatters.
096         *
097         * @param formatString  the format string.
098         * @param xFormat  the format object for the x values (<code>null</code> 
099         *                 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 BubbleXYItemLabelGenerator(String formatString, 
106                NumberFormat xFormat, NumberFormat yFormat, NumberFormat zFormat) {
107            super(formatString, xFormat, yFormat);
108            if (zFormat == null) {
109                throw new IllegalArgumentException("Null 'zFormat' argument.");   
110            }
111            this.zFormat = zFormat;
112        }
113    
114        /**
115         * Constructs a new item label generator using the specified date 
116         * formatters.
117         *
118         * @param formatString  the format string.
119         * @param xFormat  the format object for the x values (<code>null</code> 
120         *                 not permitted).
121         * @param yFormat  the format object for the y values (<code>null</code> 
122         *                 not permitted).
123         * @param zFormat  the format object for the z values (<code>null</code> 
124         *                 not permitted).
125         */
126        public BubbleXYItemLabelGenerator(String formatString, 
127                DateFormat xFormat, DateFormat yFormat, DateFormat zFormat) {
128            super(formatString, xFormat, yFormat);
129            if (zFormat == null) {
130                throw new IllegalArgumentException("Null 'zFormat' argument.");   
131            }
132            this.zDateFormat = zFormat;
133        }
134        
135        /**
136         * Returns the number formatter for the z-values.
137         *
138         * @return The number formatter (possibly <code>null</code>).
139         */
140        public NumberFormat getZFormat() {
141            return this.zFormat;
142        }
143        
144        /**
145         * Returns the date formatter for the z-values.
146         *
147         * @return The date formatter (possibly <code>null</code>).
148         */
149        public DateFormat getZDateFormat() {
150            return this.zDateFormat;   
151        }
152    
153        /**
154         * Generates an item label for a particular item within a series.
155         *
156         * @param dataset  the dataset (<code>null</code> not permitted).
157         * @param series  the series index (zero-based).
158         * @param item  the item index (zero-based).
159         *
160         * @return The item label (possibly <code>null</code>).
161         */
162        public String generateLabel(XYDataset dataset, int series, int item) {
163            return generateLabelString(dataset, series, item);
164        }
165        
166        /**
167         * Generates a label string for an item in the dataset.
168         *
169         * @param dataset  the dataset (<code>null</code> not permitted).
170         * @param series  the series (zero-based index).
171         * @param item  the item (zero-based index).
172         *
173         * @return The label (possibly <code>null</code>).
174         */
175        public String generateLabelString(XYDataset dataset, int series, int item) {
176            String result = null;    
177            Object[] items = null;
178            if (dataset instanceof XYZDataset) {
179                items = createItemArray((XYZDataset) dataset, series, item);
180            }
181            else {
182                items = createItemArray(dataset, series, item);
183            }
184            result = MessageFormat.format(getFormatString(), items);
185            return result;
186        }
187    
188        /**
189         * Creates the array of items that can be passed to the 
190         * {@link MessageFormat} class for creating labels.
191         *
192         * @param dataset  the dataset (<code>null</code> not permitted).
193         * @param series  the series (zero-based index).
194         * @param item  the item (zero-based index).
195         *
196         * @return The items (never <code>null</code>).
197         */
198        protected Object[] createItemArray(XYZDataset dataset, 
199                                           int series, int item) {
200    
201            Object[] result = new Object[4];
202            result[0] = dataset.getSeriesKey(series).toString();
203     
204            Number x = dataset.getX(series, item);
205            DateFormat xf = getXDateFormat();
206            if (xf != null) {
207                result[1] = xf.format(x);   
208            }
209            else {
210                result[1] = getXFormat().format(x);
211            }
212            
213            Number y = dataset.getY(series, item);
214            DateFormat yf = getYDateFormat();
215            if (yf != null) {
216                result[2] = yf.format(y);
217            }
218            else {
219                result[2] = getYFormat().format(y);
220            }
221            
222            Number z = dataset.getZ(series, item);
223            if (this.zDateFormat != null) {
224                result[3] = this.zDateFormat.format(z);   
225            }
226            else {
227                result[3] = this.zFormat.format(z);   
228            }
229            
230            return result;
231            
232        }
233    
234        /**
235         * Tests this object for equality with an arbitrary object.
236         *
237         * @param obj  the other object (<code>null</code> permitted).
238         *
239         * @return A boolean.
240         */
241        public boolean equals(Object obj) {
242            if (obj == this) {
243                return true;
244            }
245            if (!(obj instanceof BubbleXYItemLabelGenerator)) {
246                return false;
247            }
248            if (!super.equals(obj)) {
249                return false;
250            }
251            BubbleXYItemLabelGenerator that = (BubbleXYItemLabelGenerator) obj;
252            if (!ObjectUtilities.equal(this.zFormat, that.zFormat)) {
253                return false;
254            }
255            if (!ObjectUtilities.equal(this.zDateFormat, that.zDateFormat)) {
256                return false;
257            }
258            return true;
259        }
260    
261    }