    001/* ===========================================================
    002 * JFreeChart : a free chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C) Copyright 2000-2011, 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 * Tick.java
    029 * ---------
    030 * (C) Copyright 2000-2008, by Object Refinery Limited.
    031 *
    032 * Original Author:  David Gilbert (for Object Refinery Limited);
    033 * Contributor(s):   Nicolas Brodu;
    034 *
    035 * Changes
    036 * -------
    037 * 18-Sep-2001 : Added standard header and fixed DOS encoding problem (DG);
    038 * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
    039 * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
    040 * 26-Mar-2003 : Implemented Serializable (DG);
    041 * 12-Sep-2003 : Implemented Cloneable (NB);
    042 * 07-Nov-2003 : Added subclasses for particular types of ticks (DG);
    043 *
    044 */
    045
    046package org.jfree.chart.axis;
    047
    048import java.io.Serializable;
    049
    050import org.jfree.ui.TextAnchor;
    051import org.jfree.util.ObjectUtilities;
    052
    053/**
    054 * The base class used to represent labelled ticks along an axis.
    055 */
    056public abstract class Tick implements Serializable, Cloneable {
    057
    058    /** For serialization. */
    059    private static final long serialVersionUID = 6668230383875149773L;
    060
    061    /** A text version of the tick value. */
    062    private String text;
    063
    064    /** The text anchor for the tick label. */
    065    private TextAnchor textAnchor;
    066
    067    /** The rotation anchor for the tick label. */
    068    private TextAnchor rotationAnchor;
    069
    070    /** The rotation angle. */
    071    private double angle;
    072
    073    /**
    074     * Creates a new tick.
    075     *
    076     * @param text  the formatted version of the tick value.
    077     * @param textAnchor  the text anchor (<code>null</code> not permitted).
    078     * @param rotationAnchor  the rotation anchor (<code>null</code> not
    079     *                        permitted).
    080     * @param angle  the angle.
    081     */
    082    public Tick(String text, TextAnchor textAnchor, TextAnchor rotationAnchor,
    083                double angle) {
    084        if (textAnchor == null) {
    085            throw new IllegalArgumentException("Null 'textAnchor' argument.");
    086        }
    087        if (rotationAnchor == null) {
    088            throw new IllegalArgumentException(
    089                "Null 'rotationAnchor' argument."
    090            );
    091        }
    092        this.text = text;
    093        this.textAnchor = textAnchor;
    094        this.rotationAnchor = rotationAnchor;
    095        this.angle = angle;
    096    }
    097
    098    /**
    099     * Returns the text version of the tick value.
    100     *
    101     * @return A string (possibly <code>null</code>;
    102     */
    103    public String getText() {
    104        return this.text;
    105    }
    106
    107    /**
    108     * Returns the text anchor.
    109     *
    110     * @return The text anchor (never <code>null</code>).
    111     */
    112    public TextAnchor getTextAnchor() {
    113        return this.textAnchor;
    114    }
    115
    116    /**
    117     * Returns the text anchor that defines the point around which the label is
    118     * rotated.
    119     *
    120     * @return A text anchor (never <code>null</code>).
    121     */
    122    public TextAnchor getRotationAnchor() {
    123        return this.rotationAnchor;
    124    }
    125
    126    /**
    127     * Returns the angle.
    128     *
    129     * @return The angle.
    130     */
    131    public double getAngle() {
    132        return this.angle;
    133    }
    134
    135    /**
    136     * Tests this tick for equality with an arbitrary object.
    137     *
    138     * @param obj  the object (<code>null</code> permitted).
    139     *
    140     * @return A boolean.
    141     */
    142    public boolean equals(Object obj) {
    143        if (this == obj) {
    144            return true;
    145        }
    146        if (obj instanceof Tick) {
    147            Tick t = (Tick) obj;
    148            if (!ObjectUtilities.equal(this.text, t.text)) {
    149                return false;
    150            }
    151            if (!ObjectUtilities.equal(this.textAnchor, t.textAnchor)) {
    152                return false;
    153            }
    154            if (!ObjectUtilities.equal(this.rotationAnchor, t.rotationAnchor)) {
    155                return false;
    156            }
    157            if (!(this.angle == t.angle)) {
    158                return false;
    159            }
    160            return true;
    161        }
    162        return false;
    163    }
    164
    165    /**
    166     * Returns a clone of the tick.
    167     *
    168     * @return A clone.
    169     *
    170     * @throws CloneNotSupportedException if there is a problem cloning.
    171     */
    172    public Object clone() throws CloneNotSupportedException {
    173        Tick clone = (Tick) super.clone();
    174        return clone;
    175    }
    176
    177    /**
    178     * Returns a string representation of the tick.
    179     *
    180     * @return A string.
    181     */
    182    public String toString() {
    183        return this.text;
    184    }
    185}