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 * CategoryLabelPosition.java
029 * --------------------------
030 * (C) Copyright 2003-2013, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 31-Oct-2003 : Version 1 (DG);
038 * 17-Feb-2004 : Added new constructor (DG);
039 * 23-Mar-2004 : Added width calculation parameters (DG);
040 * 07-Jan-2005 : Fixed bug in equals() method (DG);
041 * 11-Jan-2005 : Removed deprecated constructor in preparation for the 1.0.0
042 *               release (DG);
043 * 02-Jul-2013 : Use ParamChecks (DG);
044 *
045 */
046
047package org.jfree.chart.axis;
048
049import java.io.Serializable;
050import org.jfree.chart.util.ParamChecks;
051
052import org.jfree.text.TextBlockAnchor;
053import org.jfree.ui.RectangleAnchor;
054import 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 */
061public 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(RectangleAnchor.CENTER, TextBlockAnchor.BOTTOM_CENTER,
092                TextAnchor.CENTER, 0.0, CategoryLabelWidthType.CATEGORY, 0.95f);
093    }
094
095    /**
096     * Creates a new category label position record.
097     *
098     * @param categoryAnchor  the category anchor (<code>null</code> not
099     *                        permitted).
100     * @param labelAnchor  the label anchor (<code>null</code> not permitted).
101     */
102    public CategoryLabelPosition(RectangleAnchor categoryAnchor,
103                                 TextBlockAnchor labelAnchor) {
104        // argument checking delegated...
105        this(categoryAnchor, labelAnchor, TextAnchor.CENTER, 0.0,
106                CategoryLabelWidthType.CATEGORY, 0.95f);
107    }
108
109    /**
110     * Creates a new category label position record.
111     *
112     * @param categoryAnchor  the category anchor (<code>null</code> not
113     *                        permitted).
114     * @param labelAnchor  the label anchor (<code>null</code> not permitted).
115     * @param widthType  the width type (<code>null</code> not permitted).
116     * @param widthRatio  the maximum label width as a percentage (of the
117     *                    category space or the range space).
118     */
119    public CategoryLabelPosition(RectangleAnchor categoryAnchor,
120            TextBlockAnchor labelAnchor, CategoryLabelWidthType widthType,
121            float widthRatio) {
122        // argument checking delegated...
123        this(categoryAnchor, labelAnchor, TextAnchor.CENTER, 0.0, widthType,
124                widthRatio);
125    }
126
127    /**
128     * Creates a new position record.  The item label anchor is a point
129     * relative to the data item (dot, bar or other visual item) on a chart.
130     * The item label is aligned by aligning the text anchor with the item
131     * label anchor.
132     *
133     * @param categoryAnchor  the category anchor (<code>null</code> not
134     *                        permitted).
135     * @param labelAnchor  the label anchor (<code>null</code> not permitted).
136     * @param rotationAnchor  the rotation anchor (<code>null</code> not
137     *                        permitted).
138     * @param angle  the rotation angle (<code>null</code> not permitted).
139     * @param widthType  the width type (<code>null</code> not permitted).
140     * @param widthRatio  the maximum label width as a percentage (of the
141     *                    category space or the range space).
142     */
143    public CategoryLabelPosition(RectangleAnchor categoryAnchor,
144            TextBlockAnchor labelAnchor, TextAnchor rotationAnchor, 
145            double angle, CategoryLabelWidthType widthType, float widthRatio) {
146
147        ParamChecks.nullNotPermitted(categoryAnchor, "categoryAnchor");
148        ParamChecks.nullNotPermitted(labelAnchor, "labelAnchor");
149        ParamChecks.nullNotPermitted(rotationAnchor, "rotationAnchor");
150        ParamChecks.nullNotPermitted(widthType, "widthType");
151
152        this.categoryAnchor = categoryAnchor;
153        this.labelAnchor = labelAnchor;
154        this.rotationAnchor = rotationAnchor;
155        this.angle = angle;
156        this.widthType = widthType;
157        this.widthRatio = widthRatio;
158
159    }
160
161    /**
162     * Returns the item label anchor.
163     *
164     * @return The item label anchor (never <code>null</code>).
165     */
166    public RectangleAnchor getCategoryAnchor() {
167        return this.categoryAnchor;
168    }
169
170    /**
171     * Returns the text block anchor.
172     *
173     * @return The text block anchor (never <code>null</code>).
174     */
175    public TextBlockAnchor getLabelAnchor() {
176        return this.labelAnchor;
177    }
178
179    /**
180     * Returns the rotation anchor point.
181     *
182     * @return The rotation anchor point (never <code>null</code>).
183     */
184    public TextAnchor getRotationAnchor() {
185        return this.rotationAnchor;
186    }
187
188    /**
189     * Returns the angle of rotation for the label.
190     *
191     * @return The angle (in radians).
192     */
193    public double getAngle() {
194        return this.angle;
195    }
196
197    /**
198     * Returns the width calculation type.
199     *
200     * @return The width calculation type (never <code>null</code>).
201     */
202    public CategoryLabelWidthType getWidthType() {
203        return this.widthType;
204    }
205
206    /**
207     * Returns the ratio used to calculate the maximum category label width.
208     *
209     * @return The ratio.
210     */
211    public float getWidthRatio() {
212        return this.widthRatio;
213    }
214
215    /**
216     * Tests this instance for equality with an arbitrary object.
217     *
218     * @param obj  the object (<code>null</code> permitted).
219     *
220     * @return A boolean.
221     */
222    @Override
223    public boolean equals(Object obj) {
224        if (obj == this) {
225            return true;
226        }
227        if (!(obj instanceof CategoryLabelPosition)) {
228            return false;
229        }
230        CategoryLabelPosition that = (CategoryLabelPosition) obj;
231        if (!this.categoryAnchor.equals(that.categoryAnchor)) {
232            return false;
233        }
234        if (!this.labelAnchor.equals(that.labelAnchor)) {
235            return false;
236        }
237        if (!this.rotationAnchor.equals(that.rotationAnchor)) {
238            return false;
239        }
240        if (this.angle != that.angle) {
241            return false;
242        }
243        if (this.widthType != that.widthType) {
244            return false;
245        }
246        if (this.widthRatio != that.widthRatio) {
247            return false;
248        }
249        return true;
250    }
251
252    /**
253     * Returns a hash code for this object.
254     *
255     * @return A hash code.
256     */
257    @Override
258    public int hashCode() {
259        int result = 19;
260        result = 37 * result + this.categoryAnchor.hashCode();
261        result = 37 * result + this.labelAnchor.hashCode();
262        result = 37 * result + this.rotationAnchor.hashCode();
263        return result;
264    }
265
266}