KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > labels > CustomXYToolTipGenerator


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -------------------------------
27  * CustomXYItemLabelGenerator.java
28  * -------------------------------
29  * (C) Copyright 2002-2005, by Richard Atkinson and Contributors.
30  *
31  * Original Author: Richard Atkinson;
32  * Contributor(s): David Gilbert (for Object Refinery Limited);
33  *
34  * $Id: CustomXYToolTipGenerator.java,v 1.3 2005/05/19 15:43:00 mungady Exp $
35  *
36  * Changes:
37  * --------
38  * 05-Aug-2002 : Version 1, contributed by Richard Atkinson (RA);
39  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
40  * 21-Mar-2003 : Implemented Serializable (DG);
41  * 13-Aug-2003 : Implemented Cloneable (DG);
42  * 17-Nov-2003 : Implemented PublicCloneable (DG);
43  * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
44  *
45  */

46
47 package org.jfree.chart.labels;
48
49 import java.io.Serializable JavaDoc;
50 import java.util.List JavaDoc;
51
52 import org.jfree.data.xy.XYDataset;
53 import org.jfree.util.PublicCloneable;
54
55 /**
56  * A tool tip generator that stores custom tooltips. The dataset passed into
57  * the generateToolTip method is ignored.
58  *
59  * @author Richard Atkinson
60  */

61 public class CustomXYToolTipGenerator implements XYToolTipGenerator,
62                                                  Cloneable JavaDoc,
63                                                  PublicCloneable,
64                                                  Serializable JavaDoc {
65
66     /** For serialization. */
67     private static final long serialVersionUID = 8636030004670141362L;
68     
69     /** Storage for the tooltip lists. */
70     private List JavaDoc toolTipSeries = new java.util.ArrayList JavaDoc();
71
72     /**
73      * Default constructor.
74      */

75     public CustomXYToolTipGenerator() {
76         super();
77     }
78
79     /**
80      * Returns the number of tool tip lists stored by the renderer.
81      *
82      * @return The list count.
83      */

84     public int getListCount() {
85         return this.toolTipSeries.size();
86     }
87
88     /**
89      * Returns the number of tool tips in a given list.
90      *
91      * @param list the list index (zero based).
92      *
93      * @return The tooltip count.
94      */

95     public int getToolTipCount(int list) {
96
97         int result = 0;
98         List JavaDoc tooltips = (List JavaDoc) this.toolTipSeries.get(list);
99         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 JavaDoc getToolTipText(int series, int item) {
114
115         String JavaDoc result = null;
116
117         if (series < getListCount()) {
118             List JavaDoc tooltips = (List JavaDoc) this.toolTipSeries.get(series);
119             if (tooltips != null) {
120                 if (item < tooltips.size()) {
121                     result = (String JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc clone() throws CloneNotSupportedException JavaDoc {
161         
162         CustomXYToolTipGenerator clone
163             = (CustomXYToolTipGenerator) super.clone();
164         if (this.toolTipSeries != null) {
165             clone.toolTipSeries = new java.util.ArrayList JavaDoc(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 JavaDoc 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 JavaDoc t1 = getToolTipText(series, item);
189                     String JavaDoc 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 }
206
Popular Tags