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 * PieLabelRecord.java
029 * -------------------
030 * (C) Copyright 2004-2008, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 08-Mar-2004 : Version 1 (DG);
038 * 14-Jun-2007 : Implemented Serializable, updated API docs (DG);
039 * 21-Nov-2007 : Implemented equals() to shut up FindBugs (DG);
040 *
041 */
042
043package org.jfree.chart.plot;
044
045import java.io.Serializable;
046
047import org.jfree.text.TextBox;
048
049/**
050 * A structure that retains information about the label for a section in a pie
051 * chart.
052 */
053public class PieLabelRecord implements Comparable, Serializable {
054
055    /** The section key. */
056    private Comparable key;
057
058    /** The angle of the centre of the section (in radians). */
059    private double angle;
060
061    /** The base y-coordinate. */
062    private double baseY;
063
064    /** The allocated y-coordinate. */
065    private double allocatedY;
066
067    /** The label. */
068    private TextBox label;
069
070    /** The label height. */
071    private double labelHeight;
072
073    /** The gap. */
074    private double gap;
075
076    /** The link percent. */
077    private double linkPercent;
078
079    /**
080     * Creates a new record.
081     *
082     * @param key  the section key.
083     * @param angle  the angle to the middle of the section (in radians).
084     * @param baseY  the base y-coordinate.
085     * @param label  the section label.
086     * @param labelHeight  the label height (in Java2D units).
087     * @param gap  the offset to the left.
088     * @param linkPercent  the link percent.
089     */
090    public PieLabelRecord(Comparable key, double angle, double baseY,
091                          TextBox label, double labelHeight, double gap,
092                          double linkPercent) {
093        this.key = key;
094        this.angle = angle;
095        this.baseY = baseY;
096        this.allocatedY = baseY;
097        this.label = label;
098        this.labelHeight = labelHeight;
099        this.gap = gap;
100        this.linkPercent = linkPercent;
101    }
102
103    /**
104     * Returns the base y-coordinate.  This is where the label will appear if
105     * there is no overlapping of labels.
106     *
107     * @return The base y-coordinate.
108     */
109    public double getBaseY() {
110        return this.baseY;
111    }
112
113    /**
114     * Sets the base y-coordinate.
115     *
116     * @param base  the base y-coordinate.
117     */
118    public void setBaseY(double base) {
119        this.baseY = base;
120    }
121
122    /**
123     * Returns the lower bound of the label.
124     *
125     * @return The lower bound.
126     */
127    public double getLowerY() {
128        return this.allocatedY - this.labelHeight / 2.0;
129    }
130
131    /**
132     * Returns the upper bound of the label.
133     *
134     * @return The upper bound.
135     */
136    public double getUpperY() {
137        return this.allocatedY + this.labelHeight / 2.0;
138    }
139
140    /**
141     * Returns the angle of the middle of the section, in radians.
142     *
143     * @return The angle, in radians.
144     */
145    public double getAngle() {
146        return this.angle;
147    }
148
149    /**
150     * Returns the key for the section that the label applies to.
151     *
152     * @return The key.
153     */
154    public Comparable getKey() {
155        return this.key;
156    }
157
158    /**
159     * Returns the label.
160     *
161     * @return The label.
162     */
163    public TextBox getLabel() {
164        return this.label;
165    }
166
167    /**
168     * Returns the label height (you could derive this from the label itself,
169     * but we cache the value so it can be retrieved quickly).
170     *
171     * @return The label height (in Java2D units).
172     */
173    public double getLabelHeight() {
174        return this.labelHeight;
175    }
176
177    /**
178     * Returns the allocated y-coordinate.
179     *
180     * @return The allocated y-coordinate.
181     */
182    public double getAllocatedY() {
183        return this.allocatedY;
184    }
185
186    /**
187     * Sets the allocated y-coordinate.
188     *
189     * @param y  the y-coordinate.
190     */
191    public void setAllocatedY(double y) {
192        this.allocatedY = y;
193    }
194
195    /**
196     * Returns the gap.
197     *
198     * @return The gap.
199     */
200    public double getGap() {
201        return this.gap;
202    }
203
204    /**
205     * Returns the link percent.
206     *
207     * @return The link percent.
208     */
209    public double getLinkPercent() {
210        return this.linkPercent;
211    }
212
213    /**
214     * Compares this object to an arbitrary object.
215     *
216     * @param obj  the object to compare against.
217     *
218     * @return An integer that specifies the relative order of the two objects.
219     */
220    @Override
221    public int compareTo(Object obj) {
222        int result = 0;
223        if (obj instanceof PieLabelRecord) {
224            PieLabelRecord plr = (PieLabelRecord) obj;
225            if (this.baseY < plr.baseY) {
226                result = -1;
227            }
228            else if (this.baseY > plr.baseY) {
229                result = 1;
230            }
231        }
232        return result;
233    }
234
235    /**
236     * Tests this record for equality with an arbitrary object.
237     *
238     * @param obj  the object (<code>null</code> permitted).
239     *
240     * @return A boolean.
241     */
242    @Override
243    public boolean equals(Object obj) {
244        if (obj == this) {
245            return true;
246        }
247        if (!(obj instanceof PieLabelRecord)) {
248            return false;
249        }
250        PieLabelRecord that = (PieLabelRecord) obj;
251        if (!this.key.equals(that.key)) {
252            return false;
253        }
254        if (this.angle != that.angle) {
255            return false;
256        }
257        if (this.gap != that.gap) {
258            return false;
259        }
260        if (this.allocatedY != that.allocatedY) {
261            return false;
262        }
263        if (this.baseY != that.baseY) {
264            return false;
265        }
266        if (this.labelHeight != that.labelHeight) {
267            return false;
268        }
269        if (this.linkPercent != that.linkPercent) {
270            return false;
271        }
272        if (!this.label.equals(that.label)) {
273            return false;
274        }
275        return true;
276    }
277
278    /**
279     * Returns a string describing the object.  This is used for debugging only.
280     *
281     * @return A string.
282     */
283    @Override
284    public String toString() {
285        return this.baseY + ", " + this.key.toString();
286    }
287}