001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2007, 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 * CustomXYItemLabelGenerator.java
029 * -------------------------------
030 * (C) Copyright 2002-2007, 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, 
061                                                 PublicCloneable,
062                                                 Serializable {
063
064    /** For serialization. */
065    private static final long serialVersionUID = 8636030004670141362L; 
066    
067    /** Storage for the tooltip lists. */
068    private List toolTipSeries = new java.util.ArrayList();
069
070    /**
071     * Default constructor.
072     */
073    public CustomXYToolTipGenerator() {
074        super();
075    }
076
077    /**
078     * Returns the number of tool tip lists stored by the renderer.
079     *
080     * @return The list count.
081     */
082    public int getListCount() {
083        return this.toolTipSeries.size();
084    }
085
086    /**
087     * Returns the number of tool tips in a given list.
088     *
089     * @param list  the list index (zero based).
090     *
091     * @return The tooltip count.
092     */
093    public int getToolTipCount(int list) {
094
095        int result = 0;
096        List tooltips = (List) this.toolTipSeries.get(list);
097        if (tooltips != null) {
098            result = tooltips.size();
099        }
100        return result;
101    }
102
103    /**
104     * Returns the tool tip text for an item.
105     *
106     * @param series  the series index.
107     * @param item  the item index.
108     *
109     * @return The tool tip text.
110     */
111    public String getToolTipText(int series, int item) {
112
113        String result = null;
114
115        if (series < getListCount()) {
116            List tooltips = (List) this.toolTipSeries.get(series);
117            if (tooltips != null) {
118                if (item < tooltips.size()) {
119                    result = (String) tooltips.get(item);
120                }
121            }
122        }
123
124        return result;
125    }
126
127    /**
128     * Adds a list of tooltips for a series.
129     *
130     * @param toolTips  the list of tool tips.
131     */
132    public void addToolTipSeries(List toolTips) {
133        this.toolTipSeries.add(toolTips);
134    }
135
136    /**
137     * Generates a tool tip text item for a particular item within a series.
138     *
139     * @param data  the dataset (ignored in this implementation).
140     * @param series  the series (zero-based index).
141     * @param item  the item (zero-based index).
142     *
143     * @return The tooltip text.
144     */
145    public String generateToolTip(XYDataset data, int series, int item) {
146
147        return getToolTipText(series, item);
148
149    }
150
151    /**
152     * Returns an independent copy of the generator.
153     * 
154     * @return A clone.
155     * 
156     * @throws CloneNotSupportedException if cloning is not supported.
157     */
158    public Object clone() throws CloneNotSupportedException {
159        
160        CustomXYToolTipGenerator clone 
161            = (CustomXYToolTipGenerator) super.clone();
162        if (this.toolTipSeries != null) {
163            clone.toolTipSeries = new java.util.ArrayList(this.toolTipSeries);
164        }
165        return clone;
166        
167    }
168    /**
169     * Tests if this object is equal to another.
170     *
171     * @param obj  the other object.
172     *
173     * @return A boolean.
174     */
175    public boolean equals(Object obj) {
176
177        if (obj == this) {
178            return true;
179        }
180
181        if (obj instanceof CustomXYToolTipGenerator) {
182            CustomXYToolTipGenerator generator = (CustomXYToolTipGenerator) obj;
183            boolean result = true;
184            for (int series = 0; series < getListCount(); series++) {
185                for (int item = 0; item < getToolTipCount(series); item++) {
186                    String t1 = getToolTipText(series, item);
187                    String t2 = generator.getToolTipText(series, item);
188                    if (t1 != null) {
189                        result = result && t1.equals(t2);
190                    }
191                    else {
192                        result = result && (t2 == null);
193                    }
194                }
195            }
196            return result;
197        }
198
199        return false;
200
201    }
202
203}