Source for org.jfree.chart.labels.BubbleXYItemLabelGenerator

   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:  * BubbleXYItemLabelGenerator.java
  29:  * -------------------------------
  30:  * (C) Copyright 2005-2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 13-Dec-2005 : Version 1, based on StandardXYZToolTipGenerator (DG);
  38:  * 26-Jan-2006 : Renamed StandardXYZItemLabelGenerator 
  39:  *               --> BubbleXYItemLabelGenerator (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.MessageFormat;
  48: import java.text.NumberFormat;
  49: 
  50: import org.jfree.chart.renderer.xy.XYBubbleRenderer;
  51: import org.jfree.data.xy.XYDataset;
  52: import org.jfree.data.xy.XYZDataset;
  53: import org.jfree.util.ObjectUtilities;
  54: 
  55: /**
  56:  * An item label generator defined for use with the {@link XYBubbleRenderer}
  57:  * class, or any other class that uses an {@link XYZDataset}.
  58:  * 
  59:  * @since 1.0.1
  60:  */
  61: public class BubbleXYItemLabelGenerator extends AbstractXYItemLabelGenerator
  62:     implements XYItemLabelGenerator, Serializable {
  63:     
  64:     static final long serialVersionUID = -8458568928021240922L;
  65: 
  66:     /** The default item label format. */
  67:     public static final String DEFAULT_FORMAT_STRING = "{3}";
  68: 
  69:     /** 
  70:      * A number formatter for the z value - if this is <code>null</code>, then 
  71:      * zDateFormat 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 BubbleXYItemLabelGenerator() {
  86:         this(DEFAULT_FORMAT_STRING, NumberFormat.getNumberInstance(),
  87:                 NumberFormat.getNumberInstance(), 
  88:                 NumberFormat.getNumberInstance());
  89:     }
  90: 
  91:     /**
  92:      * Constructs a new tool tip generator using the specified number 
  93:      * formatters.
  94:      *
  95:      * @param formatString  the format string.
  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:      * @param zFormat  the format object for the z values (<code>null</code> 
 101:      *                 not permitted).
 102:      */
 103:     public BubbleXYItemLabelGenerator(String formatString, 
 104:             NumberFormat xFormat, NumberFormat yFormat, NumberFormat zFormat) {
 105:         super(formatString, xFormat, yFormat);
 106:         if (zFormat == null) {
 107:             throw new IllegalArgumentException("Null 'zFormat' argument.");   
 108:         }
 109:         this.zFormat = zFormat;
 110:     }
 111: 
 112:     /**
 113:      * Constructs a new item label generator using the specified date 
 114:      * formatters.
 115:      *
 116:      * @param formatString  the format string.
 117:      * @param xFormat  the format object for the x values (<code>null</code> 
 118:      *                 not permitted).
 119:      * @param yFormat  the format object for the y values (<code>null</code> 
 120:      *                 not permitted).
 121:      * @param zFormat  the format object for the z values (<code>null</code> 
 122:      *                 not permitted).
 123:      */
 124:     public BubbleXYItemLabelGenerator(String formatString, 
 125:             DateFormat xFormat, DateFormat yFormat, DateFormat zFormat) {
 126:         super(formatString, xFormat, yFormat);
 127:         if (zFormat == null) {
 128:             throw new IllegalArgumentException("Null 'zFormat' argument.");   
 129:         }
 130:         this.zDateFormat = zFormat;
 131:     }
 132:     
 133:     /**
 134:      * Returns the number formatter for the z-values.
 135:      *
 136:      * @return The number formatter (possibly <code>null</code>).
 137:      */
 138:     public NumberFormat getZFormat() {
 139:         return this.zFormat;
 140:     }
 141:     
 142:     /**
 143:      * Returns the date formatter for the z-values.
 144:      *
 145:      * @return The date formatter (possibly <code>null</code>).
 146:      */
 147:     public DateFormat getZDateFormat() {
 148:         return this.zDateFormat;   
 149:     }
 150: 
 151:     /**
 152:      * Generates an item label for a particular item within a series.
 153:      *
 154:      * @param dataset  the dataset (<code>null</code> not permitted).
 155:      * @param series  the series index (zero-based).
 156:      * @param item  the item index (zero-based).
 157:      *
 158:      * @return The item label (possibly <code>null</code>).
 159:      */
 160:     public String generateLabel(XYDataset dataset, int series, int item) {
 161:         return generateLabelString(dataset, series, item);
 162:     }
 163:     
 164:     /**
 165:      * Generates a label string for an item in the dataset.
 166:      *
 167:      * @param dataset  the dataset (<code>null</code> not permitted).
 168:      * @param series  the series (zero-based index).
 169:      * @param item  the item (zero-based index).
 170:      *
 171:      * @return The label (possibly <code>null</code>).
 172:      */
 173:     public String generateLabelString(XYDataset dataset, int series, int item) {
 174:         String result = null;    
 175:         Object[] items = null;
 176:         if (dataset instanceof XYZDataset) {
 177:             items = createItemArray((XYZDataset) dataset, series, item);
 178:         }
 179:         else {
 180:             items = createItemArray(dataset, series, item);
 181:         }
 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 BubbleXYItemLabelGenerator)) {
 244:             return false;
 245:         }
 246:         if (!super.equals(obj)) {
 247:             return false;
 248:         }
 249:         BubbleXYItemLabelGenerator that = (BubbleXYItemLabelGenerator) 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: }