Source for org.jfree.chart.labels.StandardPieSectionLabelGenerator

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * -------------------------------------
  28:  * StandardPieSectionLabelGenerator.java
  29:  * -------------------------------------
  30:  * (C) Copyright 2004-2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 09-Nov-2004 : Version 1, derived from StandardPieItemLabelGenerator (DG);
  38:  * 29-Jul-2005 : Removed unused generateToolTip() method (DG);
  39:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  40:  * 03-May-2006 : Modified DEFAULT_SECTION_LABEL_FORMAT (DG);
  41:  * 10-Jan-2007 : Include attributedLabels in equals() test (DG);
  42:  * 10-Jul-2007 : Added constructors with locale parameter (DG);
  43:  *
  44:  */
  45: 
  46: package org.jfree.chart.labels;
  47: 
  48: import java.awt.Font;
  49: import java.awt.Paint;
  50: import java.awt.font.TextAttribute;
  51: import java.io.Serializable;
  52: import java.text.AttributedString;
  53: import java.text.NumberFormat;
  54: import java.util.Locale;
  55: 
  56: import org.jfree.data.general.PieDataset;
  57: import org.jfree.util.ObjectList;
  58: 
  59: /**
  60:  * A standard item label generator for plots that use data from a 
  61:  * {@link PieDataset}.
  62:  * <p>
  63:  * For the label format, use {0} where the pie section key should be inserted,
  64:  * {1} for the absolute section value and {2} for the percent amount of the pie
  65:  * section, e.g. <code>"{0} = {1} ({2})"</code> will display as  
  66:  * <code>apple = 120 (5%)</code>.
  67:  */
  68: public class StandardPieSectionLabelGenerator 
  69:         extends AbstractPieItemLabelGenerator
  70:         implements PieSectionLabelGenerator, Cloneable, Serializable {
  71: 
  72:     /** For serialization. */
  73:     private static final long serialVersionUID = 3064190563760203668L;
  74:     
  75:     /** The default section label format. */
  76:     public static final String DEFAULT_SECTION_LABEL_FORMAT = "{0}";
  77: 
  78:     /** 
  79:      * An optional list of attributed labels (instances of AttributedString). 
  80:      */
  81:     private ObjectList attributedLabels;
  82: 
  83:     /**
  84:      * Creates a new section label generator using 
  85:      * {@link #DEFAULT_SECTION_LABEL_FORMAT} as the label format string, and 
  86:      * platform default number and percentage formatters.
  87:      */
  88:     public StandardPieSectionLabelGenerator() {
  89:         this(DEFAULT_SECTION_LABEL_FORMAT, NumberFormat.getNumberInstance(), 
  90:                 NumberFormat.getPercentInstance());
  91:     }
  92: 
  93:     /**
  94:      * Creates a new instance for the specified locale.
  95:      * 
  96:      * @param locale  the local (<code>null</code> not permitted).
  97:      * 
  98:      * @since 1.0.7
  99:      */
 100:     public StandardPieSectionLabelGenerator(Locale locale) {
 101:         this(DEFAULT_SECTION_LABEL_FORMAT, locale);
 102:     }
 103:     
 104:     /**
 105:      * Creates a new section label generator using the specified label format
 106:      * string, and platform default number and percentage formatters.
 107:      * 
 108:      * @param labelFormat  the label format (<code>null</code> not permitted).
 109:      */
 110:     public StandardPieSectionLabelGenerator(String labelFormat) {
 111:         this(labelFormat, NumberFormat.getNumberInstance(), 
 112:                 NumberFormat.getPercentInstance());   
 113:     }
 114:     
 115:     /**
 116:      * Creates a new instance for the specified locale.
 117:      * 
 118:      * @param labelFormat  the label format (<code>null</code> not permitted).
 119:      * @param locale  the local (<code>null</code> not permitted).
 120:      * 
 121:      * @since 1.0.7
 122:      */
 123:     public StandardPieSectionLabelGenerator(String labelFormat, Locale locale) {
 124:         this(labelFormat, NumberFormat.getNumberInstance(locale),
 125:                 NumberFormat.getPercentInstance(locale));
 126:     }
 127:     
 128:     /**
 129:      * Creates an item label generator using the specified number formatters.
 130:      *
 131:      * @param labelFormat  the label format string (<code>null</code> not 
 132:      *                     permitted).
 133:      * @param numberFormat  the format object for the values (<code>null</code>
 134:      *                      not permitted).
 135:      * @param percentFormat  the format object for the percentages 
 136:      *                       (<code>null</code> not permitted).
 137:      */
 138:     public StandardPieSectionLabelGenerator(String labelFormat,
 139:             NumberFormat numberFormat, NumberFormat percentFormat) {
 140:         super(labelFormat, numberFormat, percentFormat);
 141:         this.attributedLabels = new ObjectList();
 142:     }
 143: 
 144:     /**
 145:      * Returns the attributed label for a section, or <code>null</code> if none
 146:      * is defined.
 147:      * 
 148:      * @param section  the section index.
 149:      * 
 150:      * @return The attributed label.
 151:      */
 152:     public AttributedString getAttributedLabel(int section) {
 153:         return (AttributedString) this.attributedLabels.get(section);    
 154:     }
 155:     
 156:     /**
 157:      * Sets the attributed label for a section.
 158:      * 
 159:      * @param section  the section index.
 160:      * @param label  the label (<code>null</code> permitted).
 161:      */
 162:     public void setAttributedLabel(int section, AttributedString label) {
 163:         this.attributedLabels.set(section, label);
 164:     }
 165:     
 166:     /**
 167:      * Generates a label for a pie section.
 168:      * 
 169:      * @param dataset  the dataset (<code>null</code> not permitted).
 170:      * @param key  the section key (<code>null</code> not permitted).
 171:      * 
 172:      * @return The label (possibly <code>null</code>).
 173:      */
 174:     public String generateSectionLabel(PieDataset dataset, Comparable key) {
 175:         return super.generateSectionLabel(dataset, key);
 176:     }
 177: 
 178:     /**
 179:      * Generates an attributed label for the specified series, or 
 180:      * <code>null</code> if no attributed label is available (in which case,
 181:      * the string returned by 
 182:      * {@link #generateSectionLabel(PieDataset, Comparable)} will 
 183:      * provide the fallback).  Only certain attributes are recognised by the 
 184:      * code that ultimately displays the labels: 
 185:      * <ul>
 186:      * <li>{@link TextAttribute#FONT}: will set the font;</li>
 187:      * <li>{@link TextAttribute#POSTURE}: a value of 
 188:      *     {@link TextAttribute#POSTURE_OBLIQUE} will add {@link Font#ITALIC} to
 189:      *     the current font;</li>
 190:      * <li>{@link TextAttribute#WEIGHT}: a value of 
 191:      *     {@link TextAttribute#WEIGHT_BOLD} will add {@link Font#BOLD} to the 
 192:      *     current font;</li>
 193:      * <li>{@link TextAttribute#FOREGROUND}: this will set the {@link Paint} 
 194:      *     for the current</li>
 195:      * <li>{@link TextAttribute#SUPERSCRIPT}: the values 
 196:      *     {@link TextAttribute#SUPERSCRIPT_SUB} and 
 197:      *     {@link TextAttribute#SUPERSCRIPT_SUPER} are recognised.</li> 
 198:      * </ul>
 199:      * 
 200:      * @param dataset  the dataset (<code>null</code> not permitted).
 201:      * @param key  the key.
 202:      * 
 203:      * @return An attributed label (possibly <code>null</code>).
 204:      */
 205:     public AttributedString generateAttributedSectionLabel(PieDataset dataset, 
 206:                                                            Comparable key) {
 207:         return getAttributedLabel(dataset.getIndex(key));
 208:     }
 209: 
 210:     /**
 211:      * Tests the generator for equality with an arbitrary object.
 212:      *
 213:      * @param obj  the object to test against (<code>null</code> permitted).
 214:      *
 215:      * @return A boolean.
 216:      */
 217:     public boolean equals(Object obj) {
 218:         if (obj == this) {
 219:             return true;
 220:         }
 221:         if (!(obj instanceof StandardPieSectionLabelGenerator)) {
 222:             return false;
 223:         }
 224:         StandardPieSectionLabelGenerator that 
 225:                 = (StandardPieSectionLabelGenerator) obj;
 226:         if (!this.attributedLabels.equals(that.attributedLabels)) {
 227:             return false;
 228:         }
 229:         if (!super.equals(obj)) {
 230:             return false;
 231:         }
 232:         return true;
 233:     }
 234: 
 235:     /**
 236:      * Returns an independent copy of the generator.
 237:      * 
 238:      * @return A clone.
 239:      * 
 240:      * @throws CloneNotSupportedException  should not happen.
 241:      */
 242:     public Object clone() throws CloneNotSupportedException {      
 243:         return super.clone();
 244:     }
 245: 
 246: }