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