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 * IntervalXYItemLabelGenerator.java
029 * ---------------------------------
030 * (C) Copyright 2008, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 26-May-2008 : Version 1 (DG);
038 *
039 */
040
041package org.jfree.chart.labels;
042
043import java.io.Serializable;
044import java.text.DateFormat;
045import java.text.MessageFormat;
046import java.text.NumberFormat;
047import java.util.Date;
048
049import org.jfree.data.xy.IntervalXYDataset;
050import org.jfree.data.xy.XYDataset;
051import org.jfree.util.PublicCloneable;
052
053/**
054 * An item label generator for datasets that implement the
055 * {@link IntervalXYDataset} interface.
056 *
057 * @since 1.0.10
058 */
059public class IntervalXYItemLabelGenerator extends AbstractXYItemLabelGenerator
060        implements XYItemLabelGenerator, Cloneable, PublicCloneable,
061                   Serializable {
062
063    /** The default item label format. */
064    public static final String DEFAULT_ITEM_LABEL_FORMAT = "{5} - {6}";
065
066    /**
067     * Creates an item label generator using default number formatters.
068     */
069    public IntervalXYItemLabelGenerator() {
070        this(DEFAULT_ITEM_LABEL_FORMAT, NumberFormat.getNumberInstance(),
071            NumberFormat.getNumberInstance());
072    }
073
074    /**
075     * Creates an item label generator using the specified number formatters.
076     *
077     * @param formatString  the item label format string (<code>null</code> not
078     *                      permitted).
079     * @param xFormat  the format object for the x values (<code>null</code>
080     *                 not permitted).
081     * @param yFormat  the format object for the y values (<code>null</code>
082     *                 not permitted).
083     */
084    public IntervalXYItemLabelGenerator(String formatString,
085        NumberFormat xFormat, NumberFormat yFormat) {
086
087        super(formatString, xFormat, yFormat);
088    }
089
090    /**
091     * Creates an item label generator using the specified formatters.
092     *
093     * @param formatString  the item label format string (<code>null</code>
094     *                      not permitted).
095     * @param xFormat  the format object for the x values (<code>null</code>
096     *                 not permitted).
097     * @param yFormat  the format object for the y values (<code>null</code>
098     *                 not permitted).
099     */
100    public IntervalXYItemLabelGenerator(String formatString,
101        DateFormat xFormat, NumberFormat yFormat) {
102
103        super(formatString, xFormat, yFormat);
104    }
105
106    /**
107     * Creates an item label generator using the specified formatters (a
108     * number formatter for the x-values and a date formatter for the
109     * y-values).
110     *
111     * @param formatString  the item label format string (<code>null</code>
112     *                      not permitted).
113     * @param xFormat  the format object for the x values (<code>null</code>
114     *                 permitted).
115     * @param yFormat  the format object for the y values (<code>null</code>
116     *                 not permitted).
117     */
118    public IntervalXYItemLabelGenerator(String formatString,
119            NumberFormat xFormat, DateFormat yFormat) {
120
121        super(formatString, xFormat, yFormat);
122    }
123
124    /**
125     * Creates a label generator using the specified date formatters.
126     *
127     * @param formatString  the label format string (<code>null</code> not
128     *                      permitted).
129     * @param xFormat  the format object for the x values (<code>null</code>
130     *                 not permitted).
131     * @param yFormat  the format object for the y values (<code>null</code>
132     *                 not permitted).
133     */
134    public IntervalXYItemLabelGenerator(String formatString,
135            DateFormat xFormat, DateFormat yFormat) {
136
137        super(formatString, xFormat, yFormat);
138    }
139
140    /**
141     * Creates the array of items that can be passed to the
142     * {@link MessageFormat} class for creating labels.
143     *
144     * @param dataset  the dataset (<code>null</code> not permitted).
145     * @param series  the series (zero-based index).
146     * @param item  the item (zero-based index).
147     *
148     * @return An array of seven items from the dataset formatted as
149     *         <code>String</code> objects (never <code>null</code>).
150     */
151    protected Object[] createItemArray(XYDataset dataset, int series,
152                                       int item) {
153
154        IntervalXYDataset intervalDataset = null;
155        if (dataset instanceof IntervalXYDataset) {
156            intervalDataset = (IntervalXYDataset) dataset;
157        }
158        Object[] result = new Object[7];
159        result[0] = dataset.getSeriesKey(series).toString();
160
161        double x = dataset.getXValue(series, item);
162        double xs = x;
163        double xe = x;
164        double y = dataset.getYValue(series, item);
165        double ys = y;
166        double ye = y;
167        if (intervalDataset != null) {
168            xs = intervalDataset.getStartXValue(series, item);
169            xe = intervalDataset.getEndXValue(series, item);
170            ys = intervalDataset.getStartYValue(series, item);
171            ye = intervalDataset.getEndYValue(series, item);
172        }
173
174        DateFormat xdf = getXDateFormat();
175        if (xdf != null) {
176            result[1] = xdf.format(new Date((long) x));
177            result[2] = xdf.format(new Date((long) xs));
178            result[3] = xdf.format(new Date((long) xe));
179        }
180        else {
181            NumberFormat xnf = getXFormat();
182            result[1] = xnf.format(x);
183            result[2] = xnf.format(xs);
184            result[3] = xnf.format(xe);
185        }
186
187        NumberFormat ynf = getYFormat();
188        DateFormat ydf = getYDateFormat();
189        if (Double.isNaN(y) && dataset.getY(series, item) == null) {
190            result[4] = getNullYString();
191        }
192        else {
193            if (ydf != null) {
194                result[4] = ydf.format(new Date((long) y));
195            }
196            else {
197                result[4] = ynf.format(y);
198            }
199        }
200        if (Double.isNaN(ys)
201                && intervalDataset.getStartY(series, item) == null) {
202            result[5] = getNullYString();
203        }
204        else {
205            if (ydf != null) {
206                result[5] = ydf.format(new Date((long) ys));
207            }
208            else {
209                result[5] = ynf.format(ys);
210            }
211        }
212        if (Double.isNaN(ye)
213                && intervalDataset.getEndY(series, item) == null) {
214            result[6] = getNullYString();
215        }
216        else {
217            if (ydf != null) {
218                result[6] = ydf.format(new Date((long) ye));
219            }
220            else {
221                result[6] = ynf.format(ye);
222            }
223        }
224        return result;
225    }
226
227    /**
228     * Generates the item label text for an item in a dataset.
229     *
230     * @param dataset  the dataset (<code>null</code> not permitted).
231     * @param series  the series index (zero-based).
232     * @param item  the item index (zero-based).
233     *
234     * @return The label text (possibly <code>null</code>).
235     */
236    public String generateLabel(XYDataset dataset, int series, int item) {
237        return generateLabelString(dataset, series, item);
238    }
239
240    /**
241     * Returns an independent copy of the generator.
242     *
243     * @return A clone.
244     *
245     * @throws CloneNotSupportedException if cloning is not supported.
246     */
247    public Object clone() throws CloneNotSupportedException {
248        return super.clone();
249    }
250
251    /**
252     * Tests this object for equality with an arbitrary object.
253     *
254     * @param obj  the other object (<code>null</code> permitted).
255     *
256     * @return A boolean.
257     */
258    public boolean equals(Object obj) {
259        if (obj == this) {
260            return true;
261        }
262        if (!(obj instanceof IntervalXYItemLabelGenerator)) {
263            return false;
264        }
265        return super.equals(obj);
266    }
267
268}