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