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