Source for org.jfree.chart.labels.StandardCategorySeriesLabelGenerator

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2006, 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:  * StandardCategorySeriesLabelGenerator.java
  29:  * -----------------------------------------
  30:  * (C) Copyright 2005, 2006, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: StandardCategorySeriesLabelGenerator.java,v 1.2.2.2 2006/05/03 15:44:02 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 20-Apr-2005 : Version 1 (DG);
  40:  * ------------- JFREECHART 1.0.0 ---------------------------------------------
  41:  * 03-May-2006 : Fixed equals() method (bug 1481102) (DG);
  42:  *
  43:  */
  44: 
  45: package org.jfree.chart.labels;
  46: 
  47: import java.io.Serializable;
  48: import java.text.MessageFormat;
  49: 
  50: import org.jfree.data.category.CategoryDataset;
  51: import org.jfree.util.PublicCloneable;
  52: 
  53: /**
  54:  * A standard series label generator for plots that use data from 
  55:  * a {@link org.jfree.data.category.CategoryDataset}.
  56:  */
  57: public class StandardCategorySeriesLabelGenerator implements 
  58:     CategorySeriesLabelGenerator, Cloneable, PublicCloneable, Serializable {
  59: 
  60:     /** For serialization. */
  61:     private static final long serialVersionUID = 4630760091523940820L;
  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 StandardCategorySeriesLabelGenerator() {
  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 StandardCategorySeriesLabelGenerator(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.
  91:      * 
  92:      * @param dataset  the dataset (<code>null</code> not permitted).
  93:      * @param series  the series.
  94:      * 
  95:      * @return A series label.
  96:      */
  97:     public String generateLabel(CategoryDataset dataset, int series) {
  98:         if (dataset == null) {
  99:             throw new IllegalArgumentException("Null 'dataset' argument.");
 100:         }
 101:         String label = MessageFormat.format(this.formatPattern, 
 102:                 createItemArray(dataset, series));
 103:         return label;
 104:     }
 105: 
 106:     /**
 107:      * Creates the array of items that can be passed to the 
 108:      * {@link MessageFormat} class for creating labels.
 109:      *
 110:      * @param dataset  the dataset (<code>null</code> not permitted).
 111:      * @param series  the series (zero-based index).
 112:      *
 113:      * @return The items (never <code>null</code>).
 114:      */
 115:     protected Object[] createItemArray(CategoryDataset dataset, int series) {
 116:         Object[] result = new Object[1];
 117:         result[0] = dataset.getRowKey(series).toString();
 118:         return result;
 119:     }
 120: 
 121:     /**
 122:      * Returns an independent copy of the generator.
 123:      * 
 124:      * @return A clone.
 125:      * 
 126:      * @throws CloneNotSupportedException if cloning is not supported.
 127:      */
 128:     public Object clone() throws CloneNotSupportedException { 
 129:         return super.clone();
 130:     }
 131:     
 132:     /**
 133:      * Tests this object for equality with an arbitrary object.
 134:      *
 135:      * @param obj  the other object (<code>null</code> permitted).
 136:      *
 137:      * @return A boolean.
 138:      */
 139:     public boolean equals(Object obj) {
 140:         if (obj == this) {
 141:             return true;
 142:         }
 143:         if (!(obj instanceof StandardCategorySeriesLabelGenerator)) {
 144:             return false;
 145:         }
 146:         StandardCategorySeriesLabelGenerator that 
 147:                 = (StandardCategorySeriesLabelGenerator) obj;
 148:         if (!this.formatPattern.equals(that.formatPattern)) {
 149:             return false;
 150:         }
 151:         return true;
 152:     }
 153: 
 154: }