Source for org.jfree.chart.labels.StandardXYSeriesLabelGenerator

   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:  * StandardXYSeriesLabelGenerator.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:  * 16-Nov-2004 : Version 1 (DG);
  38:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  39:  * 24-Nov-2006 : Fixed equals() method and updated API docs (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.chart.labels;
  44: 
  45: import java.io.Serializable;
  46: import java.text.MessageFormat;
  47: 
  48: import org.jfree.data.xy.XYDataset;
  49: import org.jfree.util.PublicCloneable;
  50: 
  51: /**
  52:  * A standard series label generator for plots that use data from 
  53:  * an {@link org.jfree.data.xy.XYDataset}.
  54:  * <br><br>
  55:  * This class implements <code>PublicCloneable</code> by mistake but we retain
  56:  * this for the sake of backward compatibility.
  57:  */
  58: public class StandardXYSeriesLabelGenerator implements XYSeriesLabelGenerator, 
  59:                                                        Cloneable, 
  60:                                                        PublicCloneable,
  61:                                                        Serializable {
  62: 
  63:     /** For serialization. */
  64:     private static final long serialVersionUID = 1916017081848400024L;
  65:     
  66:     /** The default item label format. */
  67:     public static final String DEFAULT_LABEL_FORMAT = "{0}";
  68:     
  69:     /** The format pattern. */
  70:     private String formatPattern;
  71: 
  72:     /**
  73:      * Creates a default series label generator (uses 
  74:      * {@link #DEFAULT_LABEL_FORMAT}).
  75:      */
  76:     public StandardXYSeriesLabelGenerator() {
  77:         this(DEFAULT_LABEL_FORMAT);
  78:     }
  79:     
  80:     /**
  81:      * Creates a new series label generator.
  82:      * 
  83:      * @param format  the format pattern (<code>null</code> not permitted).
  84:      */
  85:     public StandardXYSeriesLabelGenerator(String format) {
  86:         if (format == null) {
  87:             throw new IllegalArgumentException("Null 'format' argument.");
  88:         }
  89:         this.formatPattern = format;
  90:     }
  91: 
  92:     /**
  93:      * Generates a label for the specified series.  This label will be
  94:      * used for the chart legend.
  95:      * 
  96:      * @param dataset  the dataset (<code>null</code> not permitted).
  97:      * @param series  the series.
  98:      * 
  99:      * @return A series label.
 100:      */
 101:     public String generateLabel(XYDataset dataset, int series) {
 102:         if (dataset == null) {
 103:             throw new IllegalArgumentException("Null 'dataset' argument.");
 104:         }
 105:         String label = MessageFormat.format(
 106:             this.formatPattern, createItemArray(dataset, series)
 107:         );
 108:         return label;
 109:     }
 110: 
 111:     /**
 112:      * Creates the array of items that can be passed to the 
 113:      * {@link MessageFormat} class for creating labels.
 114:      *
 115:      * @param dataset  the dataset (<code>null</code> not permitted).
 116:      * @param series  the series (zero-based index).
 117:      *
 118:      * @return The items (never <code>null</code>).
 119:      */
 120:     protected Object[] createItemArray(XYDataset dataset, int series) {
 121:         Object[] result = new Object[1];
 122:         result[0] = dataset.getSeriesKey(series).toString();
 123:         return result;
 124:     }
 125: 
 126:     /**
 127:      * Returns an independent copy of the generator.  This is unnecessary, 
 128:      * because instances are immutable anyway, but we retain this 
 129:      * behaviour for backwards compatibility.
 130:      * 
 131:      * @return A clone.
 132:      * 
 133:      * @throws CloneNotSupportedException if cloning is not supported.
 134:      */
 135:     public Object clone() throws CloneNotSupportedException { 
 136:         return super.clone();
 137:     }
 138:     
 139:     /**
 140:      * Tests this object for equality with an arbitrary object.
 141:      *
 142:      * @param obj  the other object (<code>null</code> permitted).
 143:      *
 144:      * @return A boolean.
 145:      */
 146:     public boolean equals(Object obj) {
 147:         if (obj == this) {
 148:             return true;
 149:         }
 150:         if (!(obj instanceof StandardXYSeriesLabelGenerator)) {
 151:             return false;
 152:         }
 153:         StandardXYSeriesLabelGenerator that 
 154:                 = (StandardXYSeriesLabelGenerator) obj;
 155:         if (!this.formatPattern.equals(that.formatPattern)) {
 156:             return false;
 157:         }
 158:         return true;
 159:     }
 160: 
 161: }