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