Source for 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
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * -------------------------------
  28:  * CustomXYItemLabelGenerator.java
  29:  * -------------------------------
  30:  * (C) Copyright 2002-2005, by Richard Atkinson and Contributors.
  31:  *
  32:  * Original Author:  Richard Atkinson;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: CustomXYToolTipGenerator.java,v 1.3.2.1 2005/10/25 20:49:02 mungady Exp $
  36:  *
  37:  * Changes:
  38:  * --------
  39:  * 05-Aug-2002 : Version 1, contributed by Richard Atkinson (RA);
  40:  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
  41:  * 21-Mar-2003 : Implemented Serializable (DG);
  42:  * 13-Aug-2003 : Implemented Cloneable (DG);
  43:  * 17-Nov-2003 : Implemented PublicCloneable (DG);
  44:  * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
  45:  * 
  46:  */
  47: 
  48: package org.jfree.chart.labels;
  49: 
  50: import java.io.Serializable;
  51: import java.util.List;
  52: 
  53: import org.jfree.data.xy.XYDataset;
  54: import org.jfree.util.PublicCloneable;
  55: 
  56: /**
  57:  * A tool tip generator that stores custom tooltips. The dataset passed into 
  58:  * the generateToolTip method is ignored.
  59:  *
  60:  * @author Richard Atkinson
  61:  */
  62: public class CustomXYToolTipGenerator implements XYToolTipGenerator, 
  63:                                                  Cloneable, 
  64:                                                  PublicCloneable,
  65:                                                  Serializable {
  66: 
  67:     /** For serialization. */
  68:     private static final long serialVersionUID = 8636030004670141362L; 
  69:     
  70:     /** Storage for the tooltip lists. */
  71:     private List toolTipSeries = new java.util.ArrayList();
  72: 
  73:     /**
  74:      * Default constructor.
  75:      */
  76:     public CustomXYToolTipGenerator() {
  77:         super();
  78:     }
  79: 
  80:     /**
  81:      * Returns the number of tool tip lists stored by the renderer.
  82:      *
  83:      * @return The list count.
  84:      */
  85:     public int getListCount() {
  86:         return this.toolTipSeries.size();
  87:     }
  88: 
  89:     /**
  90:      * Returns the number of tool tips in a given list.
  91:      *
  92:      * @param list  the list index (zero based).
  93:      *
  94:      * @return The tooltip count.
  95:      */
  96:     public int getToolTipCount(int list) {
  97: 
  98:         int result = 0;
  99:         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: }