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