    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2011, 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
    047    package org.jfree.chart.labels;
    048
    049    import java.io.Serializable;
    050    import java.util.List;
    051
    052    import org.jfree.data.xy.XYDataset;
    053    import 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     */
    059    public 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        public String generateToolTip(XYDataset data, int series, int item) {
    144
    145            return getToolTipText(series, item);
    146
    147        }
    148
    149        /**
    150         * Returns an independent copy of the generator.
    151         *
    152         * @return A clone.
    153         *
    154         * @throws CloneNotSupportedException if cloning is not supported.
    155         */
    156        public Object clone() throws CloneNotSupportedException {
    157
    158            CustomXYToolTipGenerator clone
    159                = (CustomXYToolTipGenerator) super.clone();
    160            if (this.toolTipSeries != null) {
    161                clone.toolTipSeries = new java.util.ArrayList(this.toolTipSeries);
    162            }
    163            return clone;
    164
    165        }
    166        /**
    167         * Tests if this object is equal to another.
    168         *
    169         * @param obj  the other object.
    170         *
    171         * @return A boolean.
    172         */
    173        public boolean equals(Object obj) {
    174
    175            if (obj == this) {
    176                return true;
    177            }
    178
    179            if (obj instanceof CustomXYToolTipGenerator) {
    180                CustomXYToolTipGenerator generator = (CustomXYToolTipGenerator) obj;
    181                boolean result = true;
    182                for (int series = 0; series < getListCount(); series++) {
    183                    for (int item = 0; item < getToolTipCount(series); item++) {
    184                        String t1 = getToolTipText(series, item);
    185                        String t2 = generator.getToolTipText(series, item);
    186                        if (t1 != null) {
    187                            result = result && t1.equals(t2);
    188                        }
    189                        else {
    190                            result = result && (t2 == null);
    191                        }
    192                    }
    193                }
    194                return result;
    195            }
    196
    197            return false;
    198
    199        }
    200
    201    }