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