    001/* ========================================================================
    002 * JCommon : a free general purpose class 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/jcommon/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 * TextAnchor.java
    029 * ---------------
    030 * (C) Copyright 2003-2005, by Object Refinery Limited.
    031 *
    032 * Original Author:  David Gilbert (for Object Refinery Limited);
    033 * Contributor(s):   -;
    034 *
    035 * $Id: TextAnchor.java,v 1.5 2005/10/18 13:18:34 mungady Exp $
    036 *
    037 * Changes:
    038 * --------
    039 * 10-Jun-2003 : Version 1 (DG);
    040 * 11-Jan-2005 : Removed deprecated code (DG);
    041 *
    042 */
    043
    044package org.jfree.ui;
    045
    046import java.io.ObjectStreamException;
    047import java.io.Serializable;
    048
    049/**
    050 * Used to indicate the position of an anchor point for a text string.  This is
    051 * frequently used to align a string to a fixed point in some coordinate space.
    052 *
    053 * @author David Gilbert
    054 */
    055public final class TextAnchor implements Serializable {
    056
    057    /** For serialization. */
    058    private static final long serialVersionUID = 8219158940496719660L;
    059
    060    /** Top/left. */
    061    public static final TextAnchor TOP_LEFT
    062        = new TextAnchor("TextAnchor.TOP_LEFT");
    063
    064    /** Top/center. */
    065    public static final TextAnchor TOP_CENTER
    066        = new TextAnchor("TextAnchor.TOP_CENTER");
    067
    068    /** Top/right. */
    069    public static final TextAnchor TOP_RIGHT
    070        = new TextAnchor("TextAnchor.TOP_RIGHT");
    071
    072    /** Half-ascent/left. */
    073    public static final TextAnchor HALF_ASCENT_LEFT
    074        = new TextAnchor("TextAnchor.HALF_ASCENT_LEFT");
    075
    076    /** Half-ascent/center. */
    077    public static final TextAnchor HALF_ASCENT_CENTER
    078        = new TextAnchor("TextAnchor.HALF_ASCENT_CENTER");
    079
    080    /** Half-ascent/right. */
    081    public static final TextAnchor HALF_ASCENT_RIGHT
    082        = new TextAnchor("TextAnchor.HALF_ASCENT_RIGHT");
    083
    084    /** Middle/left. */
    085    public static final TextAnchor CENTER_LEFT
    086        = new TextAnchor("TextAnchor.CENTER_LEFT");
    087
    088    /** Middle/center. */
    089    public static final TextAnchor CENTER = new TextAnchor("TextAnchor.CENTER");
    090
    091    /** Middle/right. */
    092    public static final TextAnchor CENTER_RIGHT
    093        = new TextAnchor("TextAnchor.CENTER_RIGHT");
    094
    095    /** Baseline/left. */
    096    public static final TextAnchor BASELINE_LEFT
    097        = new TextAnchor("TextAnchor.BASELINE_LEFT");
    098
    099    /** Baseline/center. */
    100    public static final TextAnchor BASELINE_CENTER
    101        = new TextAnchor("TextAnchor.BASELINE_CENTER");
    102
    103    /** Baseline/right. */
    104    public static final TextAnchor BASELINE_RIGHT
    105        = new TextAnchor("TextAnchor.BASELINE_RIGHT");
    106
    107    /** Bottom/left. */
    108    public static final TextAnchor BOTTOM_LEFT
    109        = new TextAnchor("TextAnchor.BOTTOM_LEFT");
    110
    111    /** Bottom/center. */
    112    public static final TextAnchor BOTTOM_CENTER
    113        = new TextAnchor("TextAnchor.BOTTOM_CENTER");
    114
    115    /** Bottom/right. */
    116    public static final TextAnchor BOTTOM_RIGHT
    117        = new TextAnchor("TextAnchor.BOTTOM_RIGHT");
    118
    119    /** The name. */
    120    private String name;
    121
    122    /**
    123     * Private constructor.
    124     *
    125     * @param name  the name.
    126     */
    127    private TextAnchor(final String name) {
    128        this.name = name;
    129    }
    130
    131    /**
    132     * Returns a string representing the object.
    133     *
    134     * @return The string.
    135     */
    136    public String toString() {
    137        return this.name;
    138    }
    139
    140    /**
    141     * Returns <code>true</code> if this object is equal to the specified
    142     * object, and <code>false</code> otherwise.
    143     *
    144     * @param o  the other object.
    145     *
    146     * @return A boolean.
    147     */
    148    public boolean equals(final Object o) {
    149
    150        if (this == o) {
    151            return true;
    152        }
    153        if (!(o instanceof TextAnchor)) {
    154            return false;
    155        }
    156
    157        final TextAnchor order = (TextAnchor) o;
    158        if (!this.name.equals(order.name)) {
    159            return false;
    160        }
    161
    162        return true;
    163    }
    164
    165    /**
    166     * Returns a hash code value for the object.
    167     *
    168     * @return The hashcode
    169     */
    170    public int hashCode() {
    171        return this.name.hashCode();
    172    }
    173
    174    /**
    175     * Ensures that serialization returns the unique instances.
    176     *
    177     * @return The object.
    178     *
    179     * @throws ObjectStreamException if there is a problem.
    180     */
    181    private Object readResolve() throws ObjectStreamException {
    182        TextAnchor result = null;
    183        if (this.equals(TextAnchor.TOP_LEFT)) {
    184            result = TextAnchor.TOP_LEFT;
    185        }
    186        else if (this.equals(TextAnchor.TOP_CENTER)) {
    187            result = TextAnchor.TOP_CENTER;
    188        }
    189        else if (this.equals(TextAnchor.TOP_RIGHT)) {
    190            result = TextAnchor.TOP_RIGHT;
    191        }
    192        else if (this.equals(TextAnchor.BOTTOM_LEFT)) {
    193            result = TextAnchor.BOTTOM_LEFT;
    194        }
    195        else if (this.equals(TextAnchor.BOTTOM_CENTER)) {
    196            result = TextAnchor.BOTTOM_CENTER;
    197        }
    198        else if (this.equals(TextAnchor.BOTTOM_RIGHT)) {
    199            result = TextAnchor.BOTTOM_RIGHT;
    200        }
    201        else if (this.equals(TextAnchor.BASELINE_LEFT)) {
    202            result = TextAnchor.BASELINE_LEFT;
    203        }
    204        else if (this.equals(TextAnchor.BASELINE_CENTER)) {
    205            result = TextAnchor.BASELINE_CENTER;
    206        }
    207        else if (this.equals(TextAnchor.BASELINE_RIGHT)) {
    208            result = TextAnchor.BASELINE_RIGHT;
    209        }
    210        else if (this.equals(TextAnchor.CENTER_LEFT)) {
    211            result = TextAnchor.CENTER_LEFT;
    212        }
    213        else if (this.equals(TextAnchor.CENTER)) {
    214            result = TextAnchor.CENTER;
    215        }
    216        else if (this.equals(TextAnchor.CENTER_RIGHT)) {
    217            result = TextAnchor.CENTER_RIGHT;
    218        }
    219        else if (this.equals(TextAnchor.HALF_ASCENT_LEFT)) {
    220            result = TextAnchor.HALF_ASCENT_LEFT;
    221        }
    222        else if (this.equals(TextAnchor.HALF_ASCENT_CENTER)) {
    223            result = TextAnchor.HALF_ASCENT_CENTER;
    224        }
    225        else if (this.equals(TextAnchor.HALF_ASCENT_RIGHT)) {
    226            result = TextAnchor.HALF_ASCENT_RIGHT;
    227        }
    228        return result;
    229    }
    230
    231}