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