Source for org.jfree.chart.labels.StandardXYToolTipGenerator

   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:  * StandardXYToolTipGenerator.java
  29:  * -------------------------------
  30:  * (C) Copyright 2004, 2005, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: StandardXYToolTipGenerator.java,v 1.3.2.1 2005/10/25 20:49:02 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 12-May-2004 : Version 1 (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.chart.labels;
  44: 
  45: import java.io.Serializable;
  46: import java.text.DateFormat;
  47: import java.text.NumberFormat;
  48: 
  49: import org.jfree.data.xy.XYDataset;
  50: import org.jfree.util.PublicCloneable;
  51: 
  52: /**
  53:  * A standard tool tip generator for use with an 
  54:  * {@link org.jfree.chart.renderer.xy.XYItemRenderer}.
  55:  */
  56: public class StandardXYToolTipGenerator extends AbstractXYItemLabelGenerator  
  57:                                         implements XYToolTipGenerator,
  58:                                                    Cloneable, 
  59:                                                    PublicCloneable,
  60:                                                    Serializable {
  61: 
  62:     /** For serialization. */
  63:     private static final long serialVersionUID = -3564164459039540784L;    
  64:     
  65:     /** The default tooltip format. */
  66:     public static final String DEFAULT_TOOL_TIP_FORMAT = "{0}: ({1}, {2})";
  67: 
  68:     /**
  69:      * Returns a tool tip generator that formats the x-values as dates and the 
  70:      * y-values as numbers.
  71:      * 
  72:      * @return A tool tip generator (never <code>null</code>).
  73:      */
  74:     public static StandardXYToolTipGenerator getTimeSeriesInstance() {
  75:         return new StandardXYToolTipGenerator(
  76:             DEFAULT_TOOL_TIP_FORMAT, DateFormat.getInstance(), 
  77:             NumberFormat.getInstance()
  78:         );
  79:     }
  80:     
  81:     /**
  82:      * Creates a tool tip generator using default number formatters.
  83:      */
  84:     public StandardXYToolTipGenerator() {
  85:         this(
  86:             DEFAULT_TOOL_TIP_FORMAT,
  87:             NumberFormat.getNumberInstance(), NumberFormat.getNumberInstance()
  88:         );
  89:     }
  90: 
  91:     /**
  92:      * Creates a tool tip generator using the specified number formatters.
  93:      *
  94:      * @param formatString  the item label format string (<code>null</code> not
  95:      *                      permitted).
  96:      * @param xFormat  the format object for the x values (<code>null</code> 
  97:      *                 not permitted).
  98:      * @param yFormat  the format object for the y values (<code>null</code> 
  99:      *                 not permitted).
 100:      */
 101:     public StandardXYToolTipGenerator(String formatString,
 102:                                       NumberFormat xFormat, 
 103:                                       NumberFormat yFormat) {
 104:         
 105:         super(formatString, xFormat, yFormat);
 106:     
 107:     }
 108: 
 109:     /**
 110:      * Creates a tool tip generator using the specified number formatters.
 111:      *
 112:      * @param formatString  the label format string (<code>null</code> not 
 113:      *                      permitted).
 114:      * @param xFormat  the format object for the x values (<code>null</code> 
 115:      *                 not permitted).
 116:      * @param yFormat  the format object for the y values (<code>null</code> 
 117:      *                 not permitted).
 118:      */
 119:     public StandardXYToolTipGenerator(String formatString,
 120:                                       DateFormat xFormat, 
 121:                                       NumberFormat yFormat) {
 122:         
 123:         super(formatString, xFormat, yFormat);
 124:     
 125:     }
 126: 
 127:     /**
 128:      * Creates a tool tip generator using the specified date formatters.
 129:      *
 130:      * @param formatString  the label format string (<code>null</code> not 
 131:      *                      permitted).
 132:      * @param xFormat  the format object for the x values (<code>null</code> 
 133:      *                 not permitted).
 134:      * @param yFormat  the format object for the y values (<code>null</code> 
 135:      *                 not permitted).
 136:      */
 137:     public StandardXYToolTipGenerator(String formatString,
 138:                                       DateFormat xFormat, 
 139:                                       DateFormat yFormat) {
 140:         
 141:         super(formatString, xFormat, yFormat);
 142:     
 143:     }
 144: 
 145:     /**
 146:      * Generates the tool tip text for an item in a dataset.
 147:      *
 148:      * @param dataset  the dataset (<code>null</code> not permitted).
 149:      * @param series  the series index (zero-based).
 150:      * @param item  the item index (zero-based).
 151:      *
 152:      * @return The tooltip text (possibly <code>null</code>).
 153:      */
 154:     public String generateToolTip(XYDataset dataset, int series, int item) {
 155:         return generateLabelString(dataset, series, item);
 156:     }
 157: 
 158:     /**
 159:      * Tests this object for equality with an arbitrary object.
 160:      *
 161:      * @param obj  the other object (<code>null</code> permitted).
 162:      *
 163:      * @return A boolean.
 164:      */
 165:     public boolean equals(Object obj) {
 166:         if (obj == this) {
 167:             return true;
 168:         }
 169:         if (obj instanceof StandardXYToolTipGenerator) {
 170:             return super.equals(obj);
 171:         }
 172:         return false;
 173:     }
 174: 
 175:     /**
 176:      * Returns an independent copy of the generator.
 177:      * 
 178:      * @return A clone.
 179:      * 
 180:      * @throws CloneNotSupportedException if cloning is not supported.
 181:      */
 182:     public Object clone() throws CloneNotSupportedException { 
 183:         return super.clone();
 184:     }
 185:     
 186: }