Source for org.jfree.chart.labels.StandardXYZToolTipGenerator

   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:  * StandardXYZToolTipGenerator.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: StandardXYZToolTipGenerator.java,v 1.5.2.1 2005/10/25 20:49:02 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 11-May-2003 : Version 1, split from StandardXYZItemLabelGenerator (DG);
  40:  * 15-Jul-2004 : Switched getZ() and getZValue() methods (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.chart.labels;
  45: 
  46: import java.io.Serializable;
  47: import java.text.DateFormat;
  48: import java.text.MessageFormat;
  49: import java.text.NumberFormat;
  50: 
  51: import org.jfree.data.xy.XYDataset;
  52: import org.jfree.data.xy.XYZDataset;
  53: import org.jfree.util.ObjectUtilities;
  54: 
  55: /**
  56:  * A standard item label generator for use with {@link XYZDataset} data.  Each 
  57:  * value can be formatted as a number or as a date.
  58:  */
  59: public class StandardXYZToolTipGenerator extends StandardXYToolTipGenerator
  60:                                          implements XYZToolTipGenerator,
  61:                                                     Serializable {
  62: 
  63:     /** For serialization. */
  64:     private static final long serialVersionUID = -2961577421889473503L;
  65:     
  66:     /** The default tooltip format. */
  67:     public static final String DEFAULT_TOOL_TIP_FORMAT = "{0}: ({1}, {2}, {3})";
  68: 
  69:     /** 
  70:      * A number formatter for the z value - if this is null, then zDateFormat 
  71:      * must be non-null. 
  72:      */
  73:     private NumberFormat zFormat;
  74:     
  75:     /** 
  76:      * A date formatter for the z-value - if this is null, then zFormat must be 
  77:      * non-null. 
  78:      */
  79:     private DateFormat zDateFormat;
  80: 
  81:     /**
  82:      * Creates a new tool tip generator using default number formatters for the
  83:      * x, y and z-values.
  84:      */
  85:     public StandardXYZToolTipGenerator() {
  86:         this(
  87:             DEFAULT_TOOL_TIP_FORMAT,
  88:             NumberFormat.getNumberInstance(),
  89:             NumberFormat.getNumberInstance(),
  90:             NumberFormat.getNumberInstance()
  91:         );
  92:     }
  93: 
  94:     /**
  95:      * Constructs a new tool tip generator using the specified number 
  96:      * formatters.
  97:      *
  98:      * @param formatString  the format string.
  99:      * @param xFormat  the format object for the x values (<code>null</code> 
 100:      *                 not permitted).
 101:      * @param yFormat  the format object for the y values (<code>null</code> 
 102:      *                 not permitted).
 103:      * @param zFormat  the format object for the z values (<code>null</code> 
 104:      *                 not permitted).
 105:      */
 106:     public StandardXYZToolTipGenerator(String formatString,
 107:                                        NumberFormat xFormat,
 108:                                        NumberFormat yFormat,
 109:                                        NumberFormat zFormat) {
 110:         super(formatString, xFormat, yFormat);
 111:         if (zFormat == null) {
 112:             throw new IllegalArgumentException("Null 'zFormat' argument.");   
 113:         }
 114:         this.zFormat = zFormat;
 115:     }
 116: 
 117:     /**
 118:      * Constructs a new tool tip generator using the specified date formatters.
 119:      *
 120:      * @param formatString  the format string.
 121:      * @param xFormat  the format object for the x values (<code>null</code> 
 122:      *                 not permitted).
 123:      * @param yFormat  the format object for the y values (<code>null</code> 
 124:      *                 not permitted).
 125:      * @param zFormat  the format object for the z values (<code>null</code> 
 126:      *                 not permitted).
 127:      */
 128:     public StandardXYZToolTipGenerator(String formatString,
 129:                                        DateFormat xFormat,
 130:                                        DateFormat yFormat,
 131:                                        DateFormat zFormat) {
 132:         super(formatString, xFormat, yFormat);
 133:         if (zFormat == null) {
 134:             throw new IllegalArgumentException("Null 'zFormat' argument.");   
 135:         }
 136:         this.zDateFormat = zFormat;
 137:     }
 138: 
 139:     // TODO:  add constructors for combinations of number and date formatters.
 140:     
 141:     /**
 142:      * Returns the number formatter for the z-values.
 143:      *
 144:      * @return The number formatter (possibly <code>null</code>).
 145:      */
 146:     public NumberFormat getZFormat() {
 147:         return this.zFormat;
 148:     }
 149:     
 150:     /**
 151:      * Returns the date formatter for the z-values.
 152:      *
 153:      * @return The date formatter (possibly <code>null</code>).
 154:      */
 155:     public DateFormat getZDateFormat() {
 156:         return this.zDateFormat;   
 157:     }
 158: 
 159:     /**
 160:      * Generates a tool tip text item for a particular item within a series.
 161:      *
 162:      * @param dataset  the dataset (<code>null</code> not permitted).
 163:      * @param series  the series index (zero-based).
 164:      * @param item  the item index (zero-based).
 165:      *
 166:      * @return The tooltip text (possibly <code>null</code>).
 167:      */
 168:     public String generateToolTip(XYZDataset dataset, int series, int item) {
 169:         return generateLabelString(dataset, series, item);
 170:     }
 171:     
 172:     /**
 173:      * Generates a label string for an item in the dataset.
 174:      *
 175:      * @param dataset  the dataset (<code>null</code> not permitted).
 176:      * @param series  the series (zero-based index).
 177:      * @param item  the item (zero-based index).
 178:      *
 179:      * @return The label (possibly <code>null</code>).
 180:      */
 181:     public String generateLabelString(XYDataset dataset, int series, int item) {
 182:         String result = null;    
 183:         Object[] items = createItemArray((XYZDataset) dataset, series, item);
 184:         result = MessageFormat.format(getFormatString(), items);
 185:         return result;
 186:     }
 187: 
 188:     /**
 189:      * Creates the array of items that can be passed to the 
 190:      * {@link MessageFormat} class for creating labels.
 191:      *
 192:      * @param dataset  the dataset (<code>null</code> not permitted).
 193:      * @param series  the series (zero-based index).
 194:      * @param item  the item (zero-based index).
 195:      *
 196:      * @return The items (never <code>null</code>).
 197:      */
 198:     protected Object[] createItemArray(XYZDataset dataset, 
 199:                                        int series, int item) {
 200: 
 201:         Object[] result = new Object[4];
 202:         result[0] = dataset.getSeriesKey(series).toString();
 203:         
 204:         Number x = dataset.getX(series, item);
 205:         DateFormat xf = getXDateFormat();
 206:         if (xf != null) {
 207:             result[1] = xf.format(x);   
 208:         }
 209:         else {
 210:             result[1] = getXFormat().format(x);
 211:         }
 212:         
 213:         Number y = dataset.getY(series, item);
 214:         DateFormat yf = getYDateFormat();
 215:         if (yf != null) {
 216:             result[2] = yf.format(y);
 217:         }
 218:         else {
 219:             result[2] = getYFormat().format(y);
 220:         }
 221:         
 222:         Number z = dataset.getZ(series, item);
 223:         if (this.zDateFormat != null) {
 224:             result[3] = this.zDateFormat.format(z);   
 225:         }
 226:         else {
 227:             result[3] = this.zFormat.format(z);   
 228:         }
 229:         
 230:         return result;
 231:         
 232:     }
 233: 
 234:     /**
 235:      * Tests this object for equality with an arbitrary object.
 236:      *
 237:      * @param obj  the other object (<code>null</code> permitted).
 238:      *
 239:      * @return A boolean.
 240:      */
 241:     public boolean equals(Object obj) {
 242:         if (obj == this) {
 243:             return true;
 244:         }
 245:         if (!(obj instanceof StandardXYZToolTipGenerator)) {
 246:             return false;
 247:         }
 248:         if (!super.equals(obj)) {
 249:             return false;
 250:         }
 251:         StandardXYZToolTipGenerator that = (StandardXYZToolTipGenerator) obj;
 252:         if (!ObjectUtilities.equal(this.zFormat, that.zFormat)) {
 253:             return false;
 254:         }
 255:         if (!ObjectUtilities.equal(this.zDateFormat, that.zDateFormat)) {
 256:             return false;
 257:         }
 258:         return true;
 259: 
 260:     }
 261: 
 262: }