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     * ItemLabelPosition.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: ItemLabelPosition.java,v 1.3.2.1 2005/10/25 20:49:02 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 27-Oct-2003 : Version 1 (DG);
040     * 19-Feb-2004 : Moved to org.jfree.chart.labels, updated Javadocs and argument 
041     *               checking (DG);
042     * 26-Feb-2004 : Added new constructor (DG);
043     *
044     */
045    
046    package org.jfree.chart.labels;
047    
048    import java.io.Serializable;
049    
050    import org.jfree.ui.TextAnchor;
051    
052    /**
053     * The attributes that control the position of the label for each data item on 
054     * a chart.  Instances of this class are immutable.
055     */
056    public class ItemLabelPosition implements Serializable {
057    
058        /** For serialization. */
059        private static final long serialVersionUID = 5845390630157034499L;
060        
061        /** The item label anchor point. */
062        private ItemLabelAnchor itemLabelAnchor;
063        
064        /** The text anchor. */
065        private TextAnchor textAnchor;
066        
067        /** The rotation anchor. */
068        private TextAnchor rotationAnchor;
069    
070        /** The rotation angle. */    
071        private double angle;
072        
073        /**
074         * Creates a new position record with default settings.
075         */
076        public ItemLabelPosition() {
077            this(
078                ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER, 
079                TextAnchor.CENTER, 0.0
080            );
081        }
082        
083        /**
084         * Creates a new position record (with zero rotation).
085         * 
086         * @param itemLabelAnchor  the item label anchor (<code>null</code> not 
087         *                         permitted).
088         * @param textAnchor  the text anchor (<code>null</code> not permitted).
089         */
090        public ItemLabelPosition(ItemLabelAnchor itemLabelAnchor, 
091                                 TextAnchor textAnchor) {
092            this(itemLabelAnchor, textAnchor, TextAnchor.CENTER, 0.0);    
093        }
094        
095        /**
096         * Creates a new position record.  The item label anchor is a point 
097         * relative to the data item (dot, bar or other visual item) on a chart.  
098         * The item label is aligned by aligning the text anchor with the 
099         * item label anchor.
100         * 
101         * @param itemLabelAnchor  the item label anchor (<code>null</code> not 
102         *                         permitted).
103         * @param textAnchor  the text anchor (<code>null</code> not permitted).
104         * @param rotationAnchor  the rotation anchor (<code>null</code> not 
105         *                        permitted).
106         * @param angle  the rotation angle (in radians).
107         */
108        public ItemLabelPosition(ItemLabelAnchor itemLabelAnchor, 
109                                 TextAnchor textAnchor,
110                                 TextAnchor rotationAnchor,
111                                 double angle) {
112                  
113            if (itemLabelAnchor == null) {
114                throw new IllegalArgumentException(
115                    "Null 'itemLabelAnchor' argument."
116                );
117            }
118            if (textAnchor == null) {
119                throw new IllegalArgumentException("Null 'textAnchor' argument.");
120            }
121            if (rotationAnchor == null) {
122                throw new IllegalArgumentException(
123                    "Null 'rotationAnchor' argument."
124                );
125            }
126            
127            this.itemLabelAnchor = itemLabelAnchor;
128            this.textAnchor = textAnchor;
129            this.rotationAnchor = rotationAnchor;
130            this.angle = angle;
131        
132        }
133        
134        /**
135         * Returns the item label anchor.
136         * 
137         * @return The item label anchor (never <code>null</code>).
138         */
139        public ItemLabelAnchor getItemLabelAnchor() {
140            return this.itemLabelAnchor;
141        }
142        
143        /**
144         * Returns the text anchor.
145         * 
146         * @return The text anchor (never <code>null</code>).
147         */
148        public TextAnchor getTextAnchor() {
149            return this.textAnchor;
150        }
151        
152        /**
153         * Returns the rotation anchor point.
154         * 
155         * @return The rotation anchor point (never <code>null</code>).
156         */
157        public TextAnchor getRotationAnchor() {
158            return this.rotationAnchor;
159        }
160        
161        /**
162         * Returns the angle of rotation for the label.
163         * 
164         * @return The angle (in radians).
165         */
166        public double getAngle() {
167            return this.angle;
168        }
169        
170        /**
171         * Tests this object for equality with an arbitrary object.
172         * 
173         * @param obj  the object (<code>null</code> permitted).
174         * 
175         * @return A boolean.
176         */
177        public boolean equals(Object obj) {  
178            if (obj == this) {
179                return true;
180            }    
181            if (!(obj instanceof ItemLabelPosition)) {
182                return false;
183            }
184            ItemLabelPosition that = (ItemLabelPosition) obj;
185            if (!this.itemLabelAnchor.equals(that.itemLabelAnchor)) {
186                return false;
187            }
188            if (!this.textAnchor.equals(that.textAnchor)) {
189                return false;
190            }
191            if (!this.rotationAnchor.equals(that.rotationAnchor)) {
192                return false;
193            }
194            if (this.angle != that.angle) {
195                return false;
196            }     
197            return true;
198        }
199    
200    }