001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -------------------------------------
028 * IntervalCategoryToolTipGenerator.java
029 * -------------------------------------
030 * (C) Copyright 2004-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.labels;
038
039import java.text.DateFormat;
040import java.text.NumberFormat;
041
042import org.jfree.data.category.CategoryDataset;
043import org.jfree.data.category.IntervalCategoryDataset;
044
045/**
046 * A tooltip generator for plots that use data from an
047 * {@link IntervalCategoryDataset}.
048 */
049public class IntervalCategoryToolTipGenerator
050        extends StandardCategoryToolTipGenerator {
051
052    /** For serialization. */
053    private static final long serialVersionUID = -3853824986520333437L;
054
055    /** The default format string. */
056    public static final String DEFAULT_TOOL_TIP_FORMAT_STRING
057            = "({0}, {1}) = {3} - {4}";
058
059    /**
060     * Creates a new generator with a default number formatter.
061     */
062    public IntervalCategoryToolTipGenerator() {
063        super(DEFAULT_TOOL_TIP_FORMAT_STRING, NumberFormat.getInstance());
064    }
065
066    /**
067     * Creates a new generator with the specified number formatter.
068     *
069     * @param labelFormat  the label format string ({@code null} not
070     *                     permitted).
071     * @param formatter  the number formatter ({@code null} not permitted).
072     */
073    public IntervalCategoryToolTipGenerator(String labelFormat,
074                                            NumberFormat formatter) {
075        super(labelFormat, formatter);
076    }
077
078    /**
079     * Creates a new generator with the specified date formatter.
080     *
081     * @param labelFormat  the label format string ({@code null} not
082     *                     permitted).
083     * @param formatter  the date formatter ({@code null} not permitted).
084     */
085    public IntervalCategoryToolTipGenerator(String labelFormat,
086                                            DateFormat formatter) {
087        super(labelFormat, formatter);
088    }
089
090    /**
091     * Creates the array of items that can be passed to the
092     * {@code MessageFormat} class for creating labels.
093     *
094     * @param dataset  the dataset ({@code null} not permitted).
095     * @param row  the row index (zero-based).
096     * @param column  the column index (zero-based).
097     *
098     * @return The items (never {@code null}).
099     */
100    @Override
101    protected Object[] createItemArray(CategoryDataset dataset,
102                                       int row, int column) {
103        Object[] result = new Object[5];
104        result[0] = dataset.getRowKey(row).toString();
105        result[1] = dataset.getColumnKey(column).toString();
106        Number value = dataset.getValue(row, column);
107        if (getNumberFormat() != null) {
108            result[2] = getNumberFormat().format(value);
109        }
110        else if (getDateFormat() != null) {
111            result[2] = getDateFormat().format(value);
112        }
113
114        if (dataset instanceof IntervalCategoryDataset) {
115            IntervalCategoryDataset icd = (IntervalCategoryDataset) dataset;
116            Number start = icd.getStartValue(row, column);
117            Number end = icd.getEndValue(row, column);
118            if (getNumberFormat() != null) {
119                result[3] = getNumberFormat().format(start);
120                result[4] = getNumberFormat().format(end);
121            }
122            else if (getDateFormat() != null) {
123                result[3] = getDateFormat().format(start);
124                result[4] = getDateFormat().format(end);
125            }
126        }
127        return result;
128    }
129
130    /**
131     * Tests this tool tip generator for equality with an arbitrary
132     * object.
133     *
134     * @param obj  the object ({@code null} permitted).
135     *
136     * @return A boolean.
137     */
138    @Override
139    public boolean equals(Object obj) {
140        if (obj == this) {
141            return true;
142        }
143        if (!(obj instanceof IntervalCategoryToolTipGenerator)) {
144            return false;
145        }
146        // no fields to test
147        return super.equals(obj);
148    }
149
150}