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