001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * MultipleXYSeriesLabelGenerator.java
029: * -----------------------------------
030: * (C) Copyright 2004, 2005, 2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: MultipleXYSeriesLabelGenerator.java,v 1.5.2.2 2007/02/20 15:32:06 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 19-Nov-2004 : Version 1 (DG);
040: * 18-Apr-2005 : Use StringBuffer (DG);
041: * 20-Feb-2007 : Fixed for equals() and cloning() (DG);
042: *
043: */
044:
045: package org.jfree.chart.labels;
046:
047: import java.io.Serializable;
048: import java.text.MessageFormat;
049: import java.util.HashMap;
050: import java.util.Iterator;
051: import java.util.List;
052: import java.util.Map;
053: import java.util.Set;
054:
055: import org.jfree.data.xy.XYDataset;
056: import org.jfree.util.PublicCloneable;
057:
058: /**
059: * A series label generator for plots that use data from
060: * an {@link org.jfree.data.xy.XYDataset}.
061: */
062: public class MultipleXYSeriesLabelGenerator implements
063: XYSeriesLabelGenerator, Cloneable, PublicCloneable,
064: Serializable {
065:
066: /** For serialization. */
067: private static final long serialVersionUID = 138976236941898560L;
068:
069: /** The default item label format. */
070: public static final String DEFAULT_LABEL_FORMAT = "{0}";
071:
072: /** The format pattern for the initial part of the label. */
073: private String formatPattern;
074:
075: /** The format pattern for additional labels. */
076: private String additionalFormatPattern;
077:
078: /** Storage for the additional series labels. */
079: private Map seriesLabelLists;
080:
081: /**
082: * Creates an item label generator using default number formatters.
083: */
084: public MultipleXYSeriesLabelGenerator() {
085: this (DEFAULT_LABEL_FORMAT);
086: }
087:
088: /**
089: * Creates a new series label generator.
090: *
091: * @param format the format pattern (<code>null</code> not permitted).
092: */
093: public MultipleXYSeriesLabelGenerator(String format) {
094: if (format == null) {
095: throw new IllegalArgumentException(
096: "Null 'format' argument.");
097: }
098: this .formatPattern = format;
099: this .additionalFormatPattern = "\n{0}";
100: this .seriesLabelLists = new HashMap();
101: }
102:
103: /**
104: * Adds an extra label for the specified series.
105: *
106: * @param series the series index.
107: * @param label the label.
108: */
109: public void addSeriesLabel(int series, String label) {
110: Integer key = new Integer(series);
111: List labelList = (List) this .seriesLabelLists.get(key);
112: if (labelList == null) {
113: labelList = new java.util.ArrayList();
114: this .seriesLabelLists.put(key, labelList);
115: }
116: labelList.add(label);
117: }
118:
119: /**
120: * Clears the extra labels for the specified series.
121: *
122: * @param series the series index.
123: */
124: public void clearSeriesLabels(int series) {
125: Integer key = new Integer(series);
126: this .seriesLabelLists.put(key, null);
127: }
128:
129: /**
130: * Generates a label for the specified series. This label will be
131: * used for the chart legend.
132: *
133: * @param dataset the dataset (<code>null</code> not permitted).
134: * @param series the series.
135: *
136: * @return A series label.
137: */
138: public String generateLabel(XYDataset dataset, int series) {
139: if (dataset == null) {
140: throw new IllegalArgumentException(
141: "Null 'dataset' argument.");
142: }
143: StringBuffer label = new StringBuffer();
144: label.append(MessageFormat.format(this .formatPattern,
145: createItemArray(dataset, series)));
146: Integer key = new Integer(series);
147: List extraLabels = (List) this .seriesLabelLists.get(key);
148: if (extraLabels != null) {
149: Object[] temp = new Object[1];
150: for (int i = 0; i < extraLabels.size(); i++) {
151: temp[0] = extraLabels.get(i);
152: String labelAddition = MessageFormat.format(
153: this .additionalFormatPattern, temp);
154: label.append(labelAddition);
155: }
156: }
157: return label.toString();
158: }
159:
160: /**
161: * Creates the array of items that can be passed to the
162: * {@link MessageFormat} class for creating labels.
163: *
164: * @param dataset the dataset (<code>null</code> not permitted).
165: * @param series the series (zero-based index).
166: *
167: * @return The items (never <code>null</code>).
168: */
169: protected Object[] createItemArray(XYDataset dataset, int series) {
170: Object[] result = new Object[1];
171: result[0] = dataset.getSeriesKey(series).toString();
172: return result;
173: }
174:
175: /**
176: * Returns an independent copy of the generator.
177: *
178: * @return A clone.
179: *
180: * @throws CloneNotSupportedException if cloning is not supported.
181: */
182: public Object clone() throws CloneNotSupportedException {
183: MultipleXYSeriesLabelGenerator clone = (MultipleXYSeriesLabelGenerator) super
184: .clone();
185: clone.seriesLabelLists = new HashMap();
186: Set keys = this .seriesLabelLists.keySet();
187: Iterator iterator = keys.iterator();
188: while (iterator.hasNext()) {
189: Object key = iterator.next();
190: Object entry = this .seriesLabelLists.get(key);
191: Object toAdd = entry;
192: if (entry instanceof PublicCloneable) {
193: PublicCloneable pc = (PublicCloneable) entry;
194: toAdd = pc.clone();
195: }
196: clone.seriesLabelLists.put(key, toAdd);
197: }
198: return clone;
199: }
200:
201: /**
202: * Tests this object for equality with an arbitrary object.
203: *
204: * @param obj the other object (<code>null</code> permitted).
205: *
206: * @return A boolean.
207: */
208: public boolean equals(Object obj) {
209: if (obj == this ) {
210: return true;
211: }
212: if (!(obj instanceof MultipleXYSeriesLabelGenerator)) {
213: return false;
214: }
215: MultipleXYSeriesLabelGenerator that = (MultipleXYSeriesLabelGenerator) obj;
216: if (!this .formatPattern.equals(that.formatPattern)) {
217: return false;
218: }
219: if (!this .additionalFormatPattern
220: .equals(that.additionalFormatPattern)) {
221: return false;
222: }
223: if (!this .seriesLabelLists.equals(that.seriesLabelLists)) {
224: return false;
225: }
226: return true;
227: }
228:
229: }
|