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