    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2008, 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     * 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
    043    package org.jfree.chart.plot;
    044
    045    import java.io.Serializable;
    046
    047    import org.jfree.text.TextBox;
    048
    049    /**
    050     * A structure that retains information about the label for a section in a pie
    051     * chart.
    052     */
    053    public 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        public int compareTo(Object obj) {
    221            int result = 0;
    222            if (obj instanceof PieLabelRecord) {
    223                PieLabelRecord plr = (PieLabelRecord) obj;
    224                if (this.baseY < plr.baseY) {
    225                    result = -1;
    226                }
    227                else if (this.baseY > plr.baseY) {
    228                    result = 1;
    229                }
    230            }
    231            return result;
    232        }
    233
    234        /**
    235         * Tests this record for equality with an arbitrary object.
    236         *
    237         * @param obj  the object (<code>null</code> permitted).
    238         *
    239         * @return A boolean.
    240         */
    241        public boolean equals(Object obj) {
    242            if (obj == this) {
    243                return true;
    244            }
    245            if (!(obj instanceof PieLabelRecord)) {
    246                return false;
    247            }
    248            PieLabelRecord that = (PieLabelRecord) obj;
    249            if (!this.key.equals(that.key)) {
    250                return false;
    251            }
    252            if (this.angle != that.angle) {
    253                return false;
    254            }
    255            if (this.gap != that.gap) {
    256                return false;
    257            }
    258            if (this.allocatedY != that.allocatedY) {
    259                return false;
    260            }
    261            if (this.baseY != that.baseY) {
    262                return false;
    263            }
    264            if (this.labelHeight != that.labelHeight) {
    265                return false;
    266            }
    267            if (this.linkPercent != that.linkPercent) {
    268                return false;
    269            }
    270            if (!this.label.equals(that.label)) {
    271                return false;
    272            }
    273            return true;
    274        }
    275
    276        /**
    277         * Returns a string describing the object.  This is used for debugging only.
    278         *
    279         * @return A string.
    280         */
    281        public String toString() {
    282            return this.baseY + ", " + this.key.toString();
    283        }
    284    }