    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2005, 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     * CategoryLabelPosition.java
    029     * --------------------------
    030     * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: CategoryLabelPosition.java,v 1.7.2.1 2005/10/25 20:37:34 mungady Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 31-Oct-2003 : Version 1 (DG);
    040     * 17-Feb-2004 : Added new constructor (DG);
    041     * 23-Mar-2004 : Added width calculation parameters (DG);
    042     * 07-Jan-2005 : Fixed bug in equals() method (DG);
    043     * 11-Jan-2005 : Removed deprecated constructor in preparation for the 1.0.0
    044     *               release (DG);
    045     *
    046     */
    047
    048    package org.jfree.chart.axis;
    049
    050    import java.io.Serializable;
    051
    052    import org.jfree.text.TextBlockAnchor;
    053    import org.jfree.ui.RectangleAnchor;
    054    import org.jfree.ui.TextAnchor;
    055
    056    /**
    057     * The attributes that control the position of the labels for the categories on
    058     * a {@link CategoryAxis}. Instances of this class are immutable and other
    059     * JFreeChart classes rely upon this.
    060     */
    061    public class CategoryLabelPosition implements Serializable {
    062
    063        /** For serialization. */
    064        private static final long serialVersionUID = 5168681143844183864L;
    065
    066        /** The category anchor point. */
    067        private RectangleAnchor categoryAnchor;
    068
    069        /** The text block anchor. */
    070        private TextBlockAnchor labelAnchor;
    071
    072        /** The rotation anchor. */
    073        private TextAnchor rotationAnchor;
    074
    075        /** The rotation angle (in radians). */
    076        private double angle;
    077
    078        /** The width calculation type. */
    079        private CategoryLabelWidthType widthType;
    080
    081        /**
    082         * The maximum label width as a percentage of the category space or the
    083         * range space.
    084         */
    085        private float widthRatio;
    086
    087        /**
    088         * Creates a new position record with default settings.
    089         */
    090        public CategoryLabelPosition() {
    091            this(
    092                RectangleAnchor.CENTER, TextBlockAnchor.BOTTOM_CENTER,
    093                TextAnchor.CENTER, 0.0, CategoryLabelWidthType.CATEGORY, 0.95f
    094            );
    095        }
    096
    097        /**
    098         * Creates a new category label position record.
    099         *
    100         * @param categoryAnchor  the category anchor (<code>null</code> not
    101         *                        permitted).
    102         * @param labelAnchor  the label anchor (<code>null</code> not permitted).
    103         */
    104        public CategoryLabelPosition(RectangleAnchor categoryAnchor,
    105                                     TextBlockAnchor labelAnchor) {
    106            // argument checking delegated...
    107            this(
    108                categoryAnchor, labelAnchor, TextAnchor.CENTER, 0.0,
    109                CategoryLabelWidthType.CATEGORY, 0.95f
    110            );
    111        }
    112
    113        /**
    114         * Creates a new category label position record.
    115         *
    116         * @param categoryAnchor  the category anchor (<code>null</code> not
    117         *                        permitted).
    118         * @param labelAnchor  the label anchor (<code>null</code> not permitted).
    119         * @param widthType  the width type (<code>null</code> not permitted).
    120         * @param widthRatio  the maximum label width as a percentage (of the
    121         *                    category space or the range space).
    122         */
    123        public CategoryLabelPosition(RectangleAnchor categoryAnchor,
    124                                     TextBlockAnchor labelAnchor,
    125                                     CategoryLabelWidthType widthType,
    126                                     float widthRatio) {
    127            // argument checking delegated...
    128            this(
    129                categoryAnchor, labelAnchor, TextAnchor.CENTER,
    130                0.0, widthType, widthRatio
    131            );
    132        }
    133
    134        /**
    135         * Creates a new position record.  The item label anchor is a point
    136         * relative to the data item (dot, bar or other visual item) on a chart.
    137         * The item label is aligned by aligning the text anchor with the item
    138         * label anchor.
    139         *
    140         * @param categoryAnchor  the category anchor (<code>null</code> not
    141         *                        permitted).
    142         * @param labelAnchor  the label anchor (<code>null</code> not permitted).
    143         * @param rotationAnchor  the rotation anchor (<code>null</code> not
    144         *                        permitted).
    145         * @param angle  the rotation angle (<code>null</code> not permitted).
    146         * @param widthType  the width type (<code>null</code> not permitted).
    147         * @param widthRatio  the maximum label width as a percentage (of the
    148         *                    category space or the range space).
    149         */
    150        public CategoryLabelPosition(RectangleAnchor categoryAnchor,
    151                                     TextBlockAnchor labelAnchor,
    152                                     TextAnchor rotationAnchor,
    153                                     double angle,
    154                                     CategoryLabelWidthType widthType,
    155                                     float widthRatio) {
    156
    157            if (categoryAnchor == null) {
    158                throw new IllegalArgumentException(
    159                    "Null 'categoryAnchor' argument."
    160                );
    161            }
    162            if (labelAnchor == null) {
    163                throw new IllegalArgumentException(
    164                    "Null 'labelAnchor' argument."
    165                );
    166            }
    167            if (rotationAnchor == null) {
    168                throw new IllegalArgumentException(
    169                    "Null 'rotationAnchor' argument."
    170                );
    171            }
    172            if (widthType == null) {
    173                throw new IllegalArgumentException("Null 'widthType' argument.");
    174            }
    175
    176            this.categoryAnchor = categoryAnchor;
    177            this.labelAnchor = labelAnchor;
    178            this.rotationAnchor = rotationAnchor;
    179            this.angle = angle;
    180            this.widthType = widthType;
    181            this.widthRatio = widthRatio;
    182
    183        }
    184
    185        /**
    186         * Returns the item label anchor.
    187         *
    188         * @return The item label anchor (never <code>null</code>).
    189         */
    190        public RectangleAnchor getCategoryAnchor() {
    191            return this.categoryAnchor;
    192        }
    193
    194        /**
    195         * Returns the text block anchor.
    196         *
    197         * @return The text block anchor (never <code>null</code>).
    198         */
    199        public TextBlockAnchor getLabelAnchor() {
    200            return this.labelAnchor;
    201        }
    202
    203        /**
    204         * Returns the rotation anchor point.
    205         *
    206         * @return The rotation anchor point (never <code>null</code>).
    207         */
    208        public TextAnchor getRotationAnchor() {
    209            return this.rotationAnchor;
    210        }
    211
    212        /**
    213         * Returns the angle of rotation for the label.
    214         *
    215         * @return The angle (in radians).
    216         */
    217        public double getAngle() {
    218            return this.angle;
    219        }
    220
    221        /**
    222         * Returns the width calculation type.
    223         *
    224         * @return The width calculation type.
    225         */
    226        public CategoryLabelWidthType getWidthType() {
    227            return this.widthType;
    228        }
    229
    230        /**
    231         * Returns the ratio used to calculate the maximum category label width.
    232         *
    233         * @return The ratio.
    234         */
    235        public float getWidthRatio() {
    236            return this.widthRatio;
    237        }
    238
    239        /**
    240         * Tests this instance for equality with an arbitrary object.
    241         *
    242         * @param obj  the object (<code>null</code> permitted).
    243         *
    244         * @return A boolean.
    245         */
    246        public boolean equals(Object obj) {
    247            if (obj == this) {
    248                return true;
    249            }
    250            if (!(obj instanceof CategoryLabelPosition)) {
    251                return false;
    252            }
    253            CategoryLabelPosition that = (CategoryLabelPosition) obj;
    254            if (!this.categoryAnchor.equals(that.categoryAnchor)) {
    255                return false;
    256            }
    257            if (!this.labelAnchor.equals(that.labelAnchor)) {
    258                return false;
    259            }
    260            if (!this.rotationAnchor.equals(that.rotationAnchor)) {
    261                return false;
    262            }
    263            if (this.angle != that.angle) {
    264                return false;
    265            }
    266            if (this.widthType != that.widthType) {
    267                return false;
    268            }
    269            if (this.widthRatio != that.widthRatio) {
    270                return false;
    271            }
    272            return true;
    273        }
    274
    275        /**
    276         * Returns a hash code for this object.
    277         *
    278         * @return A hash code.
    279         */
    280        public int hashCode() {
    281            int result = 19;
    282            result = 37 * result + this.categoryAnchor.hashCode();
    283            result = 37 * result + this.labelAnchor.hashCode();
    284            result = 37 * result + this.rotationAnchor.hashCode();
    285            return result;
    286        }
    287
    288    }