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 * CustomXYToolTipGenerator.java
029 * -----------------------------
030 * (C) Copyright 2002-present, by Richard Atkinson and Contributors.
031 *
032 * Original Author:  Richard Atkinson;
033 * Contributor(s):   David Gilbert;
034 *
035 */
036
037package org.jfree.chart.labels;
038
039import java.io.Serializable;
040import java.util.List;
041import org.jfree.chart.util.PublicCloneable;
042
043import org.jfree.data.xy.XYDataset;
044
045/**
046 * A tool tip generator that stores custom tooltips. The dataset passed into
047 * the generateToolTip method is ignored.
048 */
049public class CustomXYToolTipGenerator implements XYToolTipGenerator,
050        Cloneable, PublicCloneable, Serializable {
051
052    /** For serialization. */
053    private static final long serialVersionUID = 8636030004670141362L;
054
055    /** Storage for the tooltip lists. */
056    private List toolTipSeries = new java.util.ArrayList();
057
058    /**
059     * Default constructor.
060     */
061    public CustomXYToolTipGenerator() {
062        super();
063    }
064
065    /**
066     * Returns the number of tool tip lists stored by the renderer.
067     *
068     * @return The list count.
069     */
070    public int getListCount() {
071        return this.toolTipSeries.size();
072    }
073
074    /**
075     * Returns the number of tool tips in a given list.
076     *
077     * @param list  the list index (zero based).
078     *
079     * @return The tooltip count.
080     */
081    public int getToolTipCount(int list) {
082
083        int result = 0;
084        List tooltips = (List) this.toolTipSeries.get(list);
085        if (tooltips != null) {
086            result = tooltips.size();
087        }
088        return result;
089    }
090
091    /**
092     * Returns the tool tip text for an item.
093     *
094     * @param series  the series index.
095     * @param item  the item index.
096     *
097     * @return The tool tip text.
098     */
099    public String getToolTipText(int series, int item) {
100
101        String result = null;
102
103        if (series < getListCount()) {
104            List tooltips = (List) this.toolTipSeries.get(series);
105            if (tooltips != null) {
106                if (item < tooltips.size()) {
107                    result = (String) tooltips.get(item);
108                }
109            }
110        }
111
112        return result;
113    }
114
115    /**
116     * Adds a list of tooltips for a series.
117     *
118     * @param toolTips  the list of tool tips.
119     */
120    public void addToolTipSeries(List toolTips) {
121        this.toolTipSeries.add(toolTips);
122    }
123
124    /**
125     * Generates a tool tip text item for a particular item within a series.
126     *
127     * @param data  the dataset (ignored in this implementation).
128     * @param series  the series (zero-based index).
129     * @param item  the item (zero-based index).
130     *
131     * @return The tooltip text.
132     */
133    @Override
134    public String generateToolTip(XYDataset data, int series, int item) {
135        return getToolTipText(series, item);
136    }
137
138    /**
139     * Returns an independent copy of the generator.
140     *
141     * @return A clone.
142     *
143     * @throws CloneNotSupportedException if cloning is not supported.
144     */
145    @Override
146    public Object clone() throws CloneNotSupportedException {
147        CustomXYToolTipGenerator clone
148            = (CustomXYToolTipGenerator) super.clone();
149        if (this.toolTipSeries != null) {
150            clone.toolTipSeries = new java.util.ArrayList(this.toolTipSeries);
151        }
152        return clone;
153    }
154
155    /**
156     * Tests if this object is equal to another.
157     *
158     * @param obj  the other object.
159     *
160     * @return A boolean.
161     */
162    @Override
163    public boolean equals(Object obj) {
164        if (obj == this) {
165            return true;
166        }
167        if (obj instanceof CustomXYToolTipGenerator) {
168            CustomXYToolTipGenerator generator = (CustomXYToolTipGenerator) obj;
169            boolean result = true;
170            for (int series = 0; series < getListCount(); series++) {
171                for (int item = 0; item < getToolTipCount(series); item++) {
172                    String t1 = getToolTipText(series, item);
173                    String t2 = generator.getToolTipText(series, item);
174                    if (t1 != null) {
175                        result = result && t1.equals(t2);
176                    }
177                    else {
178                        result = result && (t2 == null);
179                    }
180                }
181            }
182            return result;
183        }
184        return false;
185    }
186
187}