001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -----------------------------------------
028: * StandardCategorySeriesLabelGenerator.java
029: * -----------------------------------------
030: * (C) Copyright 2005, 2006, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: StandardCategorySeriesLabelGenerator.java,v 1.2.2.2 2006/05/03 15:44:02 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 20-Apr-2005 : Version 1 (DG);
040: * ------------- JFREECHART 1.0.0 ---------------------------------------------
041: * 03-May-2006 : Fixed equals() method (bug 1481102) (DG);
042: *
043: */
044:
045: package org.jfree.chart.labels;
046:
047: import java.io.Serializable;
048: import java.text.MessageFormat;
049:
050: import org.jfree.data.category.CategoryDataset;
051: import org.jfree.util.PublicCloneable;
052:
053: /**
054: * A standard series label generator for plots that use data from
055: * a {@link org.jfree.data.category.CategoryDataset}.
056: */
057: public class StandardCategorySeriesLabelGenerator implements
058: CategorySeriesLabelGenerator, Cloneable, PublicCloneable,
059: Serializable {
060:
061: /** For serialization. */
062: private static final long serialVersionUID = 4630760091523940820L;
063:
064: /** The default item label format. */
065: public static final String DEFAULT_LABEL_FORMAT = "{0}";
066:
067: /** The format pattern. */
068: private String formatPattern;
069:
070: /**
071: * Creates a default series label generator (uses
072: * {@link #DEFAULT_LABEL_FORMAT}).
073: */
074: public StandardCategorySeriesLabelGenerator() {
075: this (DEFAULT_LABEL_FORMAT);
076: }
077:
078: /**
079: * Creates a new series label generator.
080: *
081: * @param format the format pattern (<code>null</code> not permitted).
082: */
083: public StandardCategorySeriesLabelGenerator(String format) {
084: if (format == null) {
085: throw new IllegalArgumentException(
086: "Null 'format' argument.");
087: }
088: this .formatPattern = format;
089: }
090:
091: /**
092: * Generates a label for the specified series.
093: *
094: * @param dataset the dataset (<code>null</code> not permitted).
095: * @param series the series.
096: *
097: * @return A series label.
098: */
099: public String generateLabel(CategoryDataset dataset, int series) {
100: if (dataset == null) {
101: throw new IllegalArgumentException(
102: "Null 'dataset' argument.");
103: }
104: String label = MessageFormat.format(this .formatPattern,
105: createItemArray(dataset, series));
106: return label;
107: }
108:
109: /**
110: * Creates the array of items that can be passed to the
111: * {@link MessageFormat} class for creating labels.
112: *
113: * @param dataset the dataset (<code>null</code> not permitted).
114: * @param series the series (zero-based index).
115: *
116: * @return The items (never <code>null</code>).
117: */
118: protected Object[] createItemArray(CategoryDataset dataset,
119: int series) {
120: Object[] result = new Object[1];
121: result[0] = dataset.getRowKey(series).toString();
122: return result;
123: }
124:
125: /**
126: * Returns an independent copy of the generator.
127: *
128: * @return A clone.
129: *
130: * @throws CloneNotSupportedException if cloning is not supported.
131: */
132: public Object clone() throws CloneNotSupportedException {
133: return super .clone();
134: }
135:
136: /**
137: * Tests this object for equality with an arbitrary object.
138: *
139: * @param obj the other object (<code>null</code> permitted).
140: *
141: * @return A boolean.
142: */
143: public boolean equals(Object obj) {
144: if (obj == this ) {
145: return true;
146: }
147: if (!(obj instanceof StandardCategorySeriesLabelGenerator)) {
148: return false;
149: }
150: StandardCategorySeriesLabelGenerator that = (StandardCategorySeriesLabelGenerator) obj;
151: if (!this .formatPattern.equals(that.formatPattern)) {
152: return false;
153: }
154: return true;
155: }
156:
157: }
|