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 * TextBlockAnchor.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: TextBlockAnchor.java,v 1.4 2005/10/18 13:17:16 mungady Exp $
036 *
037 * Changes:
038 * --------
039 * 06-Nov-2003 : Version 1 (DG);
040 * 22-Mar-2004 : Added readResolve() method (DG); 
041 * 
042 */
043
044package org.jfree.text;
045
046import java.io.ObjectStreamException;
047import java.io.Serializable;
048
049/**
050 * Used to indicate the position of an anchor point for a text block.
051 *
052 * @author David Gilbert
053 */
054public final class TextBlockAnchor implements Serializable {
055
056    /** For serialization. */
057    private static final long serialVersionUID = -3045058380983401544L;
058    
059    /** Top/left. */
060    public static final TextBlockAnchor TOP_LEFT 
061        = new TextBlockAnchor("TextBlockAnchor.TOP_LEFT");
062
063    /** Top/center. */
064    public static final TextBlockAnchor TOP_CENTER = new TextBlockAnchor(
065        "TextBlockAnchor.TOP_CENTER"
066    );
067
068    /** Top/right. */
069    public static final TextBlockAnchor TOP_RIGHT = new TextBlockAnchor(
070       "TextBlockAnchor.TOP_RIGHT"
071    );
072
073    /** Middle/left. */
074    public static final TextBlockAnchor CENTER_LEFT = new TextBlockAnchor(
075        "TextBlockAnchor.CENTER_LEFT"
076    );
077
078    /** Middle/center. */
079    public static final TextBlockAnchor CENTER 
080        = new TextBlockAnchor("TextBlockAnchor.CENTER");
081
082    /** Middle/right. */
083    public static final TextBlockAnchor CENTER_RIGHT = new TextBlockAnchor(
084        "TextBlockAnchor.CENTER_RIGHT"
085    );
086
087    /** Bottom/left. */
088    public static final TextBlockAnchor BOTTOM_LEFT 
089        = new TextBlockAnchor("TextBlockAnchor.BOTTOM_LEFT");
090
091    /** Bottom/center. */
092    public static final TextBlockAnchor BOTTOM_CENTER 
093        = new TextBlockAnchor("TextBlockAnchor.BOTTOM_CENTER");
094
095    /** Bottom/right. */
096    public static final TextBlockAnchor BOTTOM_RIGHT 
097        = new TextBlockAnchor("TextBlockAnchor.BOTTOM_RIGHT");
098
099    /** The name. */
100    private String name;
101
102    /**
103     * Private constructor.
104     *
105     * @param name  the name.
106     */
107    private TextBlockAnchor(final String name) {
108        this.name = name;
109    }
110
111    /**
112     * Returns a string representing the object.
113     *
114     * @return The string.
115     */
116    public String toString() {
117        return this.name;
118    }
119
120    /**
121     * Returns <code>true</code> if this object is equal to the specified 
122     * object, and <code>false</code> otherwise.
123     *
124     * @param o  the other object.
125     *
126     * @return A boolean.
127     */
128    public boolean equals(final Object o) {
129
130        if (this == o) {
131            return true;
132        }
133        if (!(o instanceof TextBlockAnchor)) {
134            return false;
135        }
136
137        final TextBlockAnchor other = (TextBlockAnchor) o;
138        if (!this.name.equals(other.name)) {
139            return false;
140        }
141
142        return true;
143    }
144
145    /**
146     * Returns a hash code value for the object.
147     *
148     * @return the hashcode
149     */
150    public int hashCode() {
151        return this.name.hashCode();
152    }
153
154    /**
155     * Ensures that serialization returns the unique instances.
156     * 
157     * @return The object.
158     * 
159     * @throws ObjectStreamException if there is a problem.
160     */
161    private Object readResolve() throws ObjectStreamException {
162        if (this.equals(TextBlockAnchor.TOP_CENTER)) {
163            return TextBlockAnchor.TOP_CENTER;
164        }
165        else if (this.equals(TextBlockAnchor.TOP_LEFT)) {
166            return TextBlockAnchor.TOP_LEFT;
167        }
168        else if (this.equals(TextBlockAnchor.TOP_RIGHT)) {
169            return TextBlockAnchor.TOP_RIGHT;
170        }
171        else if (this.equals(TextBlockAnchor.CENTER)) {
172            return TextBlockAnchor.CENTER;
173        }
174        else if (this.equals(TextBlockAnchor.CENTER_LEFT)) {
175            return TextBlockAnchor.CENTER_LEFT;
176        }
177        else if (this.equals(TextBlockAnchor.CENTER_RIGHT)) {
178            return TextBlockAnchor.CENTER_RIGHT;
179        }
180        else if (this.equals(TextBlockAnchor.BOTTOM_CENTER)) {
181            return TextBlockAnchor.BOTTOM_CENTER;
182        }
183        else if (this.equals(TextBlockAnchor.BOTTOM_LEFT)) {
184            return TextBlockAnchor.BOTTOM_LEFT;
185        }
186        else if (this.equals(TextBlockAnchor.BOTTOM_RIGHT)) {
187            return TextBlockAnchor.BOTTOM_RIGHT;
188        }
189        return null;
190    }
191
192}