001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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 * StandardPieSectionLabelGenerator.java
029 * -------------------------------------
030 * (C) Copyright 2004-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.labels;
038
039import java.awt.Font;
040import java.awt.Paint;
041import java.awt.font.TextAttribute;
042import java.io.Serializable;
043import java.text.AttributedString;
044import java.text.NumberFormat;
045import java.util.HashMap;
046import java.util.Locale;
047import java.util.Map;
048import org.jfree.chart.util.PublicCloneable;
049
050import org.jfree.data.general.PieDataset;
051
052/**
053 * A standard item label generator for plots that use data from a
054 * {@link PieDataset}.
055 * <p>
056 * For the label format, use {0} where the pie section key should be inserted,
057 * {1} for the absolute section value and {2} for the percent amount of the pie
058 * section, e.g. {@code "{0} = {1} ({2})"} will display as
059 * {@code apple = 120 (5%)}.
060 */
061public class StandardPieSectionLabelGenerator
062        extends AbstractPieItemLabelGenerator
063        implements PieSectionLabelGenerator, Cloneable, PublicCloneable,
064                   Serializable {
065
066    /** For serialization. */
067    private static final long serialVersionUID = 3064190563760203668L;
068
069    /** The default section label format. */
070    public static final String DEFAULT_SECTION_LABEL_FORMAT = "{0}";
071
072    /**
073     * An optional map between item indices (Integer) and attributed labels 
074     * (instances of AttributedString).
075     */
076    private Map attributedLabels;
077
078    /**
079     * Creates a new section label generator using
080     * {@link #DEFAULT_SECTION_LABEL_FORMAT} as the label format string, and
081     * platform default number and percentage formatters.
082     */
083    public StandardPieSectionLabelGenerator() {
084        this(DEFAULT_SECTION_LABEL_FORMAT, NumberFormat.getNumberInstance(),
085                NumberFormat.getPercentInstance());
086    }
087
088    /**
089     * Creates a new instance for the specified locale.
090     *
091     * @param locale  the local ({@code null} not permitted).
092     */
093    public StandardPieSectionLabelGenerator(Locale locale) {
094        this(DEFAULT_SECTION_LABEL_FORMAT, locale);
095    }
096
097    /**
098     * Creates a new section label generator using the specified label format
099     * string, and platform default number and percentage formatters.
100     *
101     * @param labelFormat  the label format ({@code null} not permitted).
102     */
103    public StandardPieSectionLabelGenerator(String labelFormat) {
104        this(labelFormat, NumberFormat.getNumberInstance(),
105                NumberFormat.getPercentInstance());
106    }
107
108    /**
109     * Creates a new instance for the specified locale.
110     *
111     * @param labelFormat  the label format ({@code null} not permitted).
112     * @param locale  the local ({@code null} not permitted).
113     */
114    public StandardPieSectionLabelGenerator(String labelFormat, Locale locale) {
115        this(labelFormat, NumberFormat.getNumberInstance(locale),
116                NumberFormat.getPercentInstance(locale));
117    }
118
119    /**
120     * Creates an item label generator using the specified number formatters.
121     *
122     * @param labelFormat  the label format string ({@code null} not
123     *                     permitted).
124     * @param numberFormat  the format object for the values ({@code null}
125     *                      not permitted).
126     * @param percentFormat  the format object for the percentages
127     *                       ({@code null} not permitted).
128     */
129    public StandardPieSectionLabelGenerator(String labelFormat,
130            NumberFormat numberFormat, NumberFormat percentFormat) {
131        super(labelFormat, numberFormat, percentFormat);
132        this.attributedLabels = new HashMap();
133    }
134
135    /**
136     * Returns the attributed label for a section, or {@code null} if none
137     * is defined.
138     *
139     * @param section  the section index.
140     *
141     * @return The attributed label.
142     */
143    public AttributedString getAttributedLabel(int section) {
144        return (AttributedString) this.attributedLabels.get(section);
145    }
146
147    /**
148     * Sets the attributed label for a section.
149     *
150     * @param section  the section index.
151     * @param label  the label ({@code null} permitted).
152     */
153    public void setAttributedLabel(int section, AttributedString label) {
154        this.attributedLabels.put(section, label);
155    }
156
157    /**
158     * Generates a label for a pie section.
159     *
160     * @param dataset  the dataset ({@code null} not permitted).
161     * @param key  the section key ({@code null} not permitted).
162     *
163     * @return The label (possibly {@code null}).
164     */
165    @Override
166    public String generateSectionLabel(PieDataset dataset, Comparable key) {
167        return super.generateSectionLabel(dataset, key);
168    }
169
170    /**
171     * Generates an attributed label for the specified series, or
172     * {@code null} if no attributed label is available (in which case,
173     * the string returned by
174     * {@link #generateSectionLabel(PieDataset, Comparable)} will
175     * provide the fallback).  Only certain attributes are recognised by the
176     * code that ultimately displays the labels:
177     * <ul>
178     * <li>{@link TextAttribute#FONT}: will set the font;</li>
179     * <li>{@link TextAttribute#POSTURE}: a value of
180     *     {@link TextAttribute#POSTURE_OBLIQUE} will add {@link Font#ITALIC} to
181     *     the current font;</li>
182     * <li>{@link TextAttribute#WEIGHT}: a value of
183     *     {@link TextAttribute#WEIGHT_BOLD} will add {@link Font#BOLD} to the
184     *     current font;</li>
185     * <li>{@link TextAttribute#FOREGROUND}: this will set the {@link Paint}
186     *     for the current</li>
187     * <li>{@link TextAttribute#SUPERSCRIPT}: the values
188     *     {@link TextAttribute#SUPERSCRIPT_SUB} and
189     *     {@link TextAttribute#SUPERSCRIPT_SUPER} are recognised.</li>
190     * </ul>
191     *
192     * @param dataset  the dataset ({@code null} not permitted).
193     * @param key  the key.
194     *
195     * @return An attributed label (possibly {@code null}).
196     */
197    @Override
198    public AttributedString generateAttributedSectionLabel(PieDataset dataset,
199            Comparable key) {
200        return getAttributedLabel(dataset.getIndex(key));
201    }
202
203    /**
204     * Tests the generator for equality with an arbitrary object.
205     *
206     * @param obj  the object to test against ({@code null} permitted).
207     *
208     * @return A boolean.
209     */
210    @Override
211    public boolean equals(Object obj) {
212        if (obj == this) {
213            return true;
214        }
215        if (!(obj instanceof StandardPieSectionLabelGenerator)) {
216            return false;
217        }
218        StandardPieSectionLabelGenerator that
219                = (StandardPieSectionLabelGenerator) obj;
220        if (!this.attributedLabels.equals(that.attributedLabels)) {
221            return false;
222        }
223        return super.equals(obj);
224    }
225
226    /**
227     * Returns an independent copy of the generator.
228     *
229     * @return A clone.
230     *
231     * @throws CloneNotSupportedException  should not happen.
232     */
233    @Override
234    public Object clone() throws CloneNotSupportedException {
235        StandardPieSectionLabelGenerator clone 
236                = (StandardPieSectionLabelGenerator) super.clone();        
237        clone.attributedLabels = new HashMap();
238        clone.attributedLabels.putAll(this.attributedLabels);
239        return clone;
240    }
241
242}