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:  * Changes:
  36:  * --------
  37:  * 05-Aug-2002 : Version 1, contributed by Richard Atkinson (RA);
  38:  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
  39:  * 21-Mar-2003 : Implemented Serializable (DG);
  40:  * 13-Aug-2003 : Implemented Cloneable (DG);
  41:  * 17-Nov-2003 : Implemented PublicCloneable (DG);
  42:  * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
  43:  * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
  44:  * 
  45:  */
  46: 
  47: package org.jfree.chart.labels;
  48: 
  49: import java.io.Serializable;
  50: import java.util.List;
  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: public class CustomXYToolTipGenerator implements XYToolTipGenerator, 
  60:                                                  Cloneable, 
  61:                                                  PublicCloneable,
  62:                                                  Serializable {
  63: 
  64:     /** For serialization. */
  65:     private static final long serialVersionUID = 8636030004670141362L; 
  66:     
  67:     /** Storage for the tooltip lists. */
  68:     private List toolTipSeries = new java.util.ArrayList();
  69: 
  70:     /**
  71:      * Default constructor.
  72:      */
  73:     public CustomXYToolTipGenerator() {
  74:         super();
  75:     }
  76: 
  77:     /**
  78:      * Returns the number of tool tip lists stored by the renderer.
  79:      *
  80:      * @return The list count.
  81:      */
  82:     public int getListCount() {
  83:         return this.toolTipSeries.size();
  84:     }
  85: 
  86:     /**
  87:      * Returns the number of tool tips in a given list.
  88:      *
  89:      * @param list  the list index (zero based).
  90:      *
  91:      * @return The tooltip count.
  92:      */
  93:     public int getToolTipCount(int list) {
  94: 
  95:         int result = 0;
  96:         List tooltips = (List) this.toolTipSeries.get(list);
  97:         if (tooltips != null) {
  98:             result = tooltips.size();
  99:         }
 100:         return result;
 101:     }
 102: 
 103:     /**
 104:      * Returns the tool tip text for an item.
 105:      *
 106:      * @param series  the series index.
 107:      * @param item  the item index.
 108:      *
 109:      * @return The tool tip text.
 110:      */
 111:     public String getToolTipText(int series, int item) {
 112: 
 113:         String result = null;
 114: 
 115:         if (series < getListCount()) {
 116:             List tooltips = (List) this.toolTipSeries.get(series);
 117:             if (tooltips != null) {
 118:                 if (item < tooltips.size()) {
 119:                     result = (String) tooltips.get(item);
 120:                 }
 121:             }
 122:         }
 123: 
 124:         return result;
 125:     }
 126: 
 127:     /**
 128:      * Adds a list of tooltips for a series.
 129:      *
 130:      * @param toolTips  the list of tool tips.
 131:      */
 132:     public void addToolTipSeries(List toolTips) {
 133:         this.toolTipSeries.add(toolTips);
 134:     }
 135: 
 136:     /**
 137:      * Generates a tool tip text item for a particular item within a series.
 138:      *
 139:      * @param data  the dataset (ignored in this implementation).
 140:      * @param series  the series (zero-based index).
 141:      * @param item  the item (zero-based index).
 142:      *
 143:      * @return The tooltip text.
 144:      */
 145:     public String generateToolTip(XYDataset data, int series, int item) {
 146: 
 147:         return getToolTipText(series, item);
 148: 
 149:     }
 150: 
 151:     /**
 152:      * Returns an independent copy of the generator.
 153:      * 
 154:      * @return A clone.
 155:      * 
 156:      * @throws CloneNotSupportedException if cloning is not supported.
 157:      */
 158:     public Object clone() throws CloneNotSupportedException {
 159:         
 160:         CustomXYToolTipGenerator clone 
 161:             = (CustomXYToolTipGenerator) super.clone();
 162:         if (this.toolTipSeries != null) {
 163:             clone.toolTipSeries = new java.util.ArrayList(this.toolTipSeries);
 164:         }
 165:         return clone;
 166:         
 167:     }
 168:     /**
 169:      * Tests if this object is equal to another.
 170:      *
 171:      * @param obj  the other object.
 172:      *
 173:      * @return A boolean.
 174:      */
 175:     public boolean equals(Object obj) {
 176: 
 177:         if (obj == this) {
 178:             return true;
 179:         }
 180: 
 181:         if (obj instanceof CustomXYToolTipGenerator) {
 182:             CustomXYToolTipGenerator generator = (CustomXYToolTipGenerator) obj;
 183:             boolean result = true;
 184:             for (int series = 0; series < getListCount(); series++) {
 185:                 for (int item = 0; item < getToolTipCount(series); item++) {
 186:                     String t1 = getToolTipText(series, item);
 187:                     String t2 = generator.getToolTipText(series, item);
 188:                     if (t1 != null) {
 189:                         result = result && t1.equals(t2);
 190:                     }
 191:                     else {
 192:                         result = result && (t2 == null);
 193:                     }
 194:                 }
 195:             }
 196:             return result;
 197:         }
 198: 
 199:         return false;
 200: 
 201:     }
 202: 
 203: }