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