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