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-2008, 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-2008, 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:  * 23-Apr-2008 : Implemented PublicCloneable (DG);
  44:  *
  45:  */
  46: 
  47: package org.jfree.chart.labels;
  48: 
  49: import java.awt.Font;
  50: import java.awt.Paint;
  51: import java.awt.font.TextAttribute;
  52: import java.io.Serializable;
  53: import java.text.AttributedString;
  54: import java.text.NumberFormat;
  55: import java.util.Locale;
  56: 
  57: import org.jfree.data.general.PieDataset;
  58: import org.jfree.util.ObjectList;
  59: import org.jfree.util.PublicCloneable;
  60: 
  61: /**
  62:  * A standard item label generator for plots that use data from a
  63:  * {@link PieDataset}.
  64:  * <p>
  65:  * For the label format, use {0} where the pie section key should be inserted,
  66:  * {1} for the absolute section value and {2} for the percent amount of the pie
  67:  * section, e.g. <code>"{0} = {1} ({2})"</code> will display as
  68:  * <code>apple = 120 (5%)</code>.
  69:  */
  70: public class StandardPieSectionLabelGenerator
  71:         extends AbstractPieItemLabelGenerator
  72:         implements PieSectionLabelGenerator, Cloneable, PublicCloneable,
  73:                    Serializable {
  74: 
  75:     /** For serialization. */
  76:     private static final long serialVersionUID = 3064190563760203668L;
  77: 
  78:     /** The default section label format. */
  79:     public static final String DEFAULT_SECTION_LABEL_FORMAT = "{0}";
  80: 
  81:     /**
  82:      * An optional list of attributed labels (instances of AttributedString).
  83:      */
  84:     private ObjectList attributedLabels;
  85: 
  86:     /**
  87:      * Creates a new section label generator using
  88:      * {@link #DEFAULT_SECTION_LABEL_FORMAT} as the label format string, and
  89:      * platform default number and percentage formatters.
  90:      */
  91:     public StandardPieSectionLabelGenerator() {
  92:         this(DEFAULT_SECTION_LABEL_FORMAT, NumberFormat.getNumberInstance(),
  93:                 NumberFormat.getPercentInstance());
  94:     }
  95: 
  96:     /**
  97:      * Creates a new instance for the specified locale.
  98:      *
  99:      * @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:     public String generateSectionLabel(PieDataset dataset, Comparable key) {
 178:         return super.generateSectionLabel(dataset, key);
 179:     }
 180: 
 181:     /**
 182:      * Generates an attributed label for the specified series, or
 183:      * <code>null</code> if no attributed label is available (in which case,
 184:      * the string returned by
 185:      * {@link #generateSectionLabel(PieDataset, Comparable)} will
 186:      * provide the fallback).  Only certain attributes are recognised by the
 187:      * code that ultimately displays the labels:
 188:      * <ul>
 189:      * <li>{@link TextAttribute#FONT}: will set the font;</li>
 190:      * <li>{@link TextAttribute#POSTURE}: a value of
 191:      *     {@link TextAttribute#POSTURE_OBLIQUE} will add {@link Font#ITALIC} to
 192:      *     the current font;</li>
 193:      * <li>{@link TextAttribute#WEIGHT}: a value of
 194:      *     {@link TextAttribute#WEIGHT_BOLD} will add {@link Font#BOLD} to the
 195:      *     current font;</li>
 196:      * <li>{@link TextAttribute#FOREGROUND}: this will set the {@link Paint}
 197:      *     for the current</li>
 198:      * <li>{@link TextAttribute#SUPERSCRIPT}: the values
 199:      *     {@link TextAttribute#SUPERSCRIPT_SUB} and
 200:      *     {@link TextAttribute#SUPERSCRIPT_SUPER} are recognised.</li>
 201:      * </ul>
 202:      *
 203:      * @param dataset  the dataset (<code>null</code> not permitted).
 204:      * @param key  the key.
 205:      *
 206:      * @return An attributed label (possibly <code>null</code>).
 207:      */
 208:     public AttributedString generateAttributedSectionLabel(PieDataset dataset,
 209:                                                            Comparable key) {
 210:         return getAttributedLabel(dataset.getIndex(key));
 211:     }
 212: 
 213:     /**
 214:      * Tests the generator for equality with an arbitrary object.
 215:      *
 216:      * @param obj  the object to test against (<code>null</code> permitted).
 217:      *
 218:      * @return A boolean.
 219:      */
 220:     public boolean equals(Object obj) {
 221:         if (obj == this) {
 222:             return true;
 223:         }
 224:         if (!(obj instanceof StandardPieSectionLabelGenerator)) {
 225:             return false;
 226:         }
 227:         StandardPieSectionLabelGenerator that
 228:                 = (StandardPieSectionLabelGenerator) obj;
 229:         if (!this.attributedLabels.equals(that.attributedLabels)) {
 230:             return false;
 231:         }
 232:         if (!super.equals(obj)) {
 233:             return false;
 234:         }
 235:         return true;
 236:     }
 237: 
 238:     /**
 239:      * Returns an independent copy of the generator.
 240:      *
 241:      * @return A clone.
 242:      *
 243:      * @throws CloneNotSupportedException  should not happen.
 244:      */
 245:     public Object clone() throws CloneNotSupportedException {
 246:         return super.clone();
 247:     }
 248: 
 249: }