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-2007, 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-2007, 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.2 2007/02/02 15:52:42 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:  * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
  46:  * 
  47:  */
  48: 
  49: package org.jfree.chart.labels;
  50: 
  51: import java.io.Serializable;
  52: import java.util.List;
  53: 
  54: import org.jfree.data.xy.XYDataset;
  55: import org.jfree.util.PublicCloneable;
  56: 
  57: /**
  58:  * A tool tip generator that stores custom tooltips. The dataset passed into 
  59:  * the generateToolTip method is ignored.
  60:  */
  61: public class CustomXYToolTipGenerator implements XYToolTipGenerator, 
  62:                                                  Cloneable, 
  63:                                                  PublicCloneable,
  64:                                                  Serializable {
  65: 
  66:     /** For serialization. */
  67:     private static final long serialVersionUID = 8636030004670141362L; 
  68:     
  69:     /** Storage for the tooltip lists. */
  70:     private List toolTipSeries = new java.util.ArrayList();
  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 tooltips = (List) 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 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: }