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-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:  * StandardXYSeriesLabelGenerator.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: StandardXYSeriesLabelGenerator.java,v 1.5.2.1 2005/10/25 20:49:02 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 16-Nov-2004 : Version 1 (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:  */
  55: public class StandardXYSeriesLabelGenerator implements XYSeriesLabelGenerator, 
  56:                                                        Cloneable, 
  57:                                                        PublicCloneable,
  58:                                                        Serializable {
  59: 
  60:     /** For serialization. */
  61:     private static final long serialVersionUID = 1916017081848400024L;
  62:     
  63:     /** The default item label format. */
  64:     public static final String DEFAULT_LABEL_FORMAT = "{0}";
  65:     
  66:     /** The format pattern. */
  67:     private String formatPattern;
  68: 
  69:     /**
  70:      * Creates a default series label generator (uses 
  71:      * {@link #DEFAULT_LABEL_FORMAT}).
  72:      */
  73:     public StandardXYSeriesLabelGenerator() {
  74:         this(DEFAULT_LABEL_FORMAT);
  75:     }
  76:     
  77:     /**
  78:      * Creates a new series label generator.
  79:      * 
  80:      * @param format  the format pattern (<code>null</code> not permitted).
  81:      */
  82:     public StandardXYSeriesLabelGenerator(String format) {
  83:         if (format == null) {
  84:             throw new IllegalArgumentException("Null 'format' argument.");
  85:         }
  86:         this.formatPattern = format;
  87:     }
  88: 
  89:     /**
  90:      * Generates a label for the specified series.  This label will be
  91:      * used for the chart legend.
  92:      * 
  93:      * @param dataset  the dataset (<code>null</code> not permitted).
  94:      * @param series  the series.
  95:      * 
  96:      * @return A series label.
  97:      */
  98:     public String generateLabel(XYDataset dataset, int series) {
  99:         if (dataset == null) {
 100:             throw new IllegalArgumentException("Null 'dataset' argument.");
 101:         }
 102:         String label = MessageFormat.format(
 103:             this.formatPattern, createItemArray(dataset, series)
 104:         );
 105:         return label;
 106:     }
 107: 
 108:     /**
 109:      * Creates the array of items that can be passed to the 
 110:      * {@link MessageFormat} class for creating labels.
 111:      *
 112:      * @param dataset  the dataset (<code>null</code> not permitted).
 113:      * @param series  the series (zero-based index).
 114:      *
 115:      * @return The items (never <code>null</code>).
 116:      */
 117:     protected Object[] createItemArray(XYDataset dataset, int series) {
 118:         Object[] result = new Object[1];
 119:         result[0] = dataset.getSeriesKey(series).toString();
 120:         return result;
 121:     }
 122: 
 123:     /**
 124:      * Returns an independent copy of the generator.
 125:      * 
 126:      * @return A clone.
 127:      * 
 128:      * @throws CloneNotSupportedException if cloning is not supported.
 129:      */
 130:     public Object clone() throws CloneNotSupportedException { 
 131:         return super.clone();
 132:     }
 133:     
 134:     /**
 135:      * Tests this object for equality with an arbitrary object.
 136:      *
 137:      * @param obj  the other object (<code>null</code> permitted).
 138:      *
 139:      * @return A boolean.
 140:      */
 141:     public boolean equals(Object obj) {
 142:         if (obj == this) {
 143:             return true;
 144:         }
 145:         if (!(obj instanceof StandardXYSeriesLabelGenerator)) {
 146:             return false;
 147:         }
 148:         if (!super.equals(obj)) {
 149:             return false;
 150:         }
 151:         return true;
 152:     }
 153: 
 154: }