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