001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2007, 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-2007, 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.2 2007/04/13 09:49:23 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(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,
121                                     CategoryLabelWidthType widthType,
122                                     float widthRatio) {
123            // argument checking delegated...
124            this(categoryAnchor, labelAnchor, TextAnchor.CENTER, 0.0, widthType, 
125                    widthRatio);
126        }
127        
128        /**
129         * Creates a new position record.  The item label anchor is a point 
130         * relative to the data item (dot, bar or other visual item) on a chart.  
131         * The item label is aligned by aligning the text anchor with the item 
132         * label anchor.
133         * 
134         * @param categoryAnchor  the category anchor (<code>null</code> not 
135         *                        permitted).
136         * @param labelAnchor  the label anchor (<code>null</code> not permitted).
137         * @param rotationAnchor  the rotation anchor (<code>null</code> not 
138         *                        permitted).
139         * @param angle  the rotation angle (<code>null</code> not permitted).
140         * @param widthType  the width type (<code>null</code> not permitted).
141         * @param widthRatio  the maximum label width as a percentage (of the 
142         *                    category space or the range space).
143         */
144        public CategoryLabelPosition(RectangleAnchor categoryAnchor, 
145                                     TextBlockAnchor labelAnchor,
146                                     TextAnchor rotationAnchor,
147                                     double angle,
148                                     CategoryLabelWidthType widthType,
149                                     float widthRatio) {
150            
151            if (categoryAnchor == null) {
152                throw new IllegalArgumentException(
153                        "Null 'categoryAnchor' argument.");
154            }
155            if (labelAnchor == null) {
156                throw new IllegalArgumentException(
157                        "Null 'labelAnchor' argument.");
158            }
159            if (rotationAnchor == null) {
160                throw new IllegalArgumentException(
161                        "Null 'rotationAnchor' argument.");
162            }
163            if (widthType == null) {
164                throw new IllegalArgumentException("Null 'widthType' argument.");   
165            }
166            
167            this.categoryAnchor = categoryAnchor;
168            this.labelAnchor = labelAnchor;
169            this.rotationAnchor = rotationAnchor;
170            this.angle = angle;
171            this.widthType = widthType;
172            this.widthRatio = widthRatio;
173        
174        }
175        
176        /**
177         * Returns the item label anchor.
178         * 
179         * @return The item label anchor (never <code>null</code>).
180         */
181        public RectangleAnchor getCategoryAnchor() {
182            return this.categoryAnchor;
183        }
184        
185        /**
186         * Returns the text block anchor.
187         * 
188         * @return The text block anchor (never <code>null</code>).
189         */
190        public TextBlockAnchor getLabelAnchor() {
191            return this.labelAnchor;
192        }
193        
194        /**
195         * Returns the rotation anchor point.
196         * 
197         * @return The rotation anchor point (never <code>null</code>).
198         */
199        public TextAnchor getRotationAnchor() {
200            return this.rotationAnchor;
201        }
202        
203        /**
204         * Returns the angle of rotation for the label.
205         * 
206         * @return The angle (in radians).
207         */
208        public double getAngle() {
209            return this.angle;
210        }
211        
212        /**
213         * Returns the width calculation type.
214         * 
215         * @return The width calculation type (never <code>null</code>).
216         */
217        public CategoryLabelWidthType getWidthType() {
218            return this.widthType;   
219        }
220        
221        /**
222         * Returns the ratio used to calculate the maximum category label width.
223         * 
224         * @return The ratio.
225         */
226        public float getWidthRatio() {
227            return this.widthRatio;   
228        }
229        
230        /**
231         * Tests this instance for equality with an arbitrary object.
232         * 
233         * @param obj  the object (<code>null</code> permitted).
234         * 
235         * @return A boolean.
236         */
237        public boolean equals(Object obj) {
238            if (obj == this) {
239                return true;
240            }
241            if (!(obj instanceof CategoryLabelPosition)) {
242                return false;
243            }
244            CategoryLabelPosition that = (CategoryLabelPosition) obj;
245            if (!this.categoryAnchor.equals(that.categoryAnchor)) {
246                return false;
247            }
248            if (!this.labelAnchor.equals(that.labelAnchor)) {
249                return false;
250            }
251            if (!this.rotationAnchor.equals(that.rotationAnchor)) {
252                return false;
253            }
254            if (this.angle != that.angle) {
255                return false;
256            }
257            if (this.widthType != that.widthType) {
258                return false;
259            }
260            if (this.widthRatio != that.widthRatio) {
261                return false;
262            }
263            return true;
264        }
265        
266        /**
267         * Returns a hash code for this object.
268         * 
269         * @return A hash code.
270         */
271        public int hashCode() {
272            int result = 19;
273            result = 37 * result + this.categoryAnchor.hashCode();
274            result = 37 * result + this.labelAnchor.hashCode();
275            result = 37 * result + this.rotationAnchor.hashCode();
276            return result;
277        }
278    
279    }