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