001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -----------------------------------
028 * MultipleXYSeriesLabelGenerator.java
029 * -----------------------------------
030 * (C) Copyright 2004-2013, 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 * 03-Jul-2013 : Use ParamChecks (DG);
042 *
043 */
044
045package org.jfree.chart.labels;
046
047import java.io.Serializable;
048import java.text.MessageFormat;
049import java.util.HashMap;
050import java.util.Iterator;
051import java.util.List;
052import java.util.Map;
053import java.util.Set;
054
055import org.jfree.chart.HashUtilities;
056import org.jfree.chart.util.ParamChecks;
057import org.jfree.data.xy.XYDataset;
058import org.jfree.util.PublicCloneable;
059
060/**
061 * A series label generator for plots that use data from
062 * an {@link org.jfree.data.xy.XYDataset}.
063 */
064public class MultipleXYSeriesLabelGenerator implements XYSeriesLabelGenerator,
065        Cloneable, PublicCloneable, Serializable {
066
067    /** For serialization. */
068    private static final long serialVersionUID = 138976236941898560L;
069
070    /** The default item label format. */
071    public static final String DEFAULT_LABEL_FORMAT = "{0}";
072
073    /** The format pattern for the initial part of the label. */
074    private String formatPattern;
075
076    /** The format pattern for additional labels. */
077    private String additionalFormatPattern;
078
079    /** Storage for the additional series labels. */
080    private Map seriesLabelLists;
081
082    /**
083     * Creates an item label generator using default number formatters.
084     */
085    public MultipleXYSeriesLabelGenerator() {
086        this(DEFAULT_LABEL_FORMAT);
087    }
088
089    /**
090     * Creates a new series label generator.
091     *
092     * @param format  the format pattern (<code>null</code> not permitted).
093     */
094    public MultipleXYSeriesLabelGenerator(String format) {
095        ParamChecks.nullNotPermitted(format, "format");
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    @Override
137    public String generateLabel(XYDataset dataset, int series) {
138        ParamChecks.nullNotPermitted(dataset, "dataset");
139        StringBuilder label = new StringBuilder();
140        label.append(MessageFormat.format(this.formatPattern,
141                createItemArray(dataset, series)));
142        Integer key = new Integer(series);
143        List extraLabels = (List) this.seriesLabelLists.get(key);
144        if (extraLabels != null) {
145            Object[] temp = new Object[1];
146            for (int i = 0; i < extraLabels.size(); i++) {
147                temp[0] = extraLabels.get(i);
148                String labelAddition = MessageFormat.format(
149                        this.additionalFormatPattern, temp);
150                label.append(labelAddition);
151            }
152        }
153        return label.toString();
154    }
155
156    /**
157     * Creates the array of items that can be passed to the
158     * {@link MessageFormat} class for creating labels.
159     *
160     * @param dataset  the dataset (<code>null</code> not permitted).
161     * @param series  the series (zero-based index).
162     *
163     * @return The items (never <code>null</code>).
164     */
165    protected Object[] createItemArray(XYDataset dataset, int series) {
166        Object[] result = new Object[1];
167        result[0] = dataset.getSeriesKey(series).toString();
168        return result;
169    }
170
171    /**
172     * Returns an independent copy of the generator.
173     *
174     * @return A clone.
175     *
176     * @throws CloneNotSupportedException if cloning is not supported.
177     */
178    @Override
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    @Override
206    public boolean equals(Object obj) {
207        if (obj == this) {
208            return true;
209        }
210        if (!(obj instanceof MultipleXYSeriesLabelGenerator)) {
211            return false;
212        }
213        MultipleXYSeriesLabelGenerator that
214                = (MultipleXYSeriesLabelGenerator) obj;
215        if (!this.formatPattern.equals(that.formatPattern)) {
216            return false;
217        }
218        if (!this.additionalFormatPattern.equals(
219                that.additionalFormatPattern)) {
220            return false;
221        }
222        if (!this.seriesLabelLists.equals(that.seriesLabelLists)) {
223            return false;
224        }
225        return true;
226    }
227
228    /**
229     * Returns a hash code for this instance.
230     *
231     * @return A hash code.
232     */
233    @Override
234    public int hashCode() {
235        int result = 127;
236        result = HashUtilities.hashCode(result, this.formatPattern);
237        result = HashUtilities.hashCode(result, this.additionalFormatPattern);
238        result = HashUtilities.hashCode(result, this.seriesLabelLists);
239        return result;
240    }
241
242}