001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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
029package org.jfree.chart.ui;
030
031import java.io.ObjectStreamException;
032import java.io.Serializable;
033
034/**
035 * Used to indicate the position of an anchor point for a text string.  This is
036 * frequently used to align a string to a fixed point in some coordinate space.
037 */
038public final class TextAnchor implements Serializable {
039
040    /** For serialization. */
041    private static final long serialVersionUID = 8219158940496719660L;
042    
043    /** Top/left. */
044    public static final TextAnchor TOP_LEFT 
045        = new TextAnchor("TextAnchor.TOP_LEFT");
046
047    /** Top/center. */
048    public static final TextAnchor TOP_CENTER 
049        = new TextAnchor("TextAnchor.TOP_CENTER");
050
051    /** Top/right. */
052    public static final TextAnchor TOP_RIGHT 
053        = new TextAnchor("TextAnchor.TOP_RIGHT");
054
055    /** Half-ascent/left. */
056    public static final TextAnchor HALF_ASCENT_LEFT 
057        = new TextAnchor("TextAnchor.HALF_ASCENT_LEFT");
058
059    /** Half-ascent/center. */
060    public static final TextAnchor HALF_ASCENT_CENTER 
061        = new TextAnchor("TextAnchor.HALF_ASCENT_CENTER");
062
063    /** Half-ascent/right. */
064    public static final TextAnchor HALF_ASCENT_RIGHT 
065        = new TextAnchor("TextAnchor.HALF_ASCENT_RIGHT");
066
067    /** Middle/left. */
068    public static final TextAnchor CENTER_LEFT 
069        = new TextAnchor("TextAnchor.CENTER_LEFT");
070
071    /** Middle/center. */
072    public static final TextAnchor CENTER = new TextAnchor("TextAnchor.CENTER");
073
074    /** Middle/right. */
075    public static final TextAnchor CENTER_RIGHT 
076        = new TextAnchor("TextAnchor.CENTER_RIGHT");
077
078    /** Baseline/left. */
079    public static final TextAnchor BASELINE_LEFT 
080        = new TextAnchor("TextAnchor.BASELINE_LEFT");
081
082    /** Baseline/center. */
083    public static final TextAnchor BASELINE_CENTER 
084        = new TextAnchor("TextAnchor.BASELINE_CENTER");
085
086    /** Baseline/right. */
087    public static final TextAnchor BASELINE_RIGHT 
088        = new TextAnchor("TextAnchor.BASELINE_RIGHT");
089
090    /** Bottom/left. */
091    public static final TextAnchor BOTTOM_LEFT 
092        = new TextAnchor("TextAnchor.BOTTOM_LEFT");
093
094    /** Bottom/center. */
095    public static final TextAnchor BOTTOM_CENTER 
096        = new TextAnchor("TextAnchor.BOTTOM_CENTER");
097
098    /** Bottom/right. */
099    public static final TextAnchor BOTTOM_RIGHT 
100        = new TextAnchor("TextAnchor.BOTTOM_RIGHT");
101
102    /** The name. */
103    private String name;
104
105    /**
106     * Private constructor.
107     *
108     * @param name  the name.
109     */
110    private TextAnchor(String name) {
111        this.name = name;
112    }
113    
114    /** 
115     * Returns {@code true} if the anchor is a left-side anchor, and
116     * {@code false} otherwise.
117     * 
118     * @return A boolean.
119     */
120    public boolean isLeft() {
121        return this == BASELINE_LEFT || this == BOTTOM_LEFT 
122                || this == CENTER_LEFT || this == HALF_ASCENT_LEFT 
123                || this == TOP_LEFT;
124    }
125
126    /** 
127     * Returns {@code true} if the anchor is a right-side anchor, and
128     * {@code false} otherwise.
129     * 
130     * @return A boolean.
131     */
132    public boolean isRight() {
133        return this == BASELINE_RIGHT || this == BOTTOM_RIGHT 
134                || this == CENTER_RIGHT || this == HALF_ASCENT_RIGHT 
135                || this == TOP_RIGHT;
136    }
137
138    /** 
139     * Returns {@code true} if the anchor is a center anchor, and
140     * {@code false} otherwise.
141     * 
142     * @return A boolean.
143     */
144    public boolean isHorizontalCenter() {
145        return this == BASELINE_CENTER || this == BOTTOM_CENTER 
146                || this == CENTER || this == HALF_ASCENT_CENTER 
147                || this == TOP_CENTER;
148    }
149
150    /** 
151     * Returns {@code true} if the anchor is a top anchor, and
152     * {@code false} otherwise.
153     * 
154     * @return A boolean.
155     */
156    public boolean isTop() {
157        return this == TOP_LEFT || this == TOP_CENTER || this == TOP_RIGHT;
158    }
159
160    /** 
161     * Returns {@code true} if the anchor is a bottom anchor, and
162     * {@code false} otherwise.
163     * 
164     * @return A boolean.
165     */
166    public boolean isBottom() {
167        return this == BOTTOM_LEFT || this == BOTTOM_CENTER 
168                || this == BOTTOM_RIGHT;
169    }
170    
171    /** 
172     * Returns {@code true} if the anchor is a baseline anchor, and
173     * {@code false} otherwise.
174     * 
175     * @return A boolean.
176     */
177    public boolean isBaseline() {
178        return this == BASELINE_LEFT || this == BASELINE_CENTER 
179                || this == BASELINE_RIGHT;
180    }
181    
182    /** 
183     * Returns {@code true} if the anchor is a half-ascent anchor, and
184     * {@code false} otherwise.
185     * 
186     * @return A boolean.
187     */
188    public boolean isHalfAscent() {
189        return this == HALF_ASCENT_LEFT || this == HALF_ASCENT_CENTER 
190                || this == HALF_ASCENT_RIGHT;
191    }
192    
193    /** 
194     * Returns {@code true} if the anchor is a half-ascent anchor, and
195     * {@code false} otherwise.
196     * 
197     * @return A boolean.
198     */
199    public boolean isVerticalCenter() {
200        return this == CENTER_LEFT || this == CENTER  || this == CENTER_RIGHT;
201    }
202    
203    /**
204     * Returns a string representing the object.
205     *
206     * @return The string.
207     */
208    @Override
209    public String toString() {
210        return this.name;
211    }
212
213    /**
214     * Returns {@code true} if this object is equal to the specified 
215     * object, and {@code false} otherwise.
216     *
217     * @param o  the other object.
218     *
219     * @return A boolean.
220     */
221    @Override
222    public boolean equals(Object o) {
223
224        if (this == o) {
225            return true;
226        }
227        if (!(o instanceof TextAnchor)) {
228            return false;
229        }
230
231        TextAnchor order = (TextAnchor) o;
232        if (!this.name.equals(order.name)) {
233            return false;
234        }
235
236        return true;
237    }
238
239    /**
240     * Returns a hash code value for the object.
241     *
242     * @return The hashcode
243     */
244    @Override
245    public int hashCode() {
246        return this.name.hashCode();
247    }
248
249    /**
250     * Ensures that serialization returns the unique instances.
251     * 
252     * @return The object.
253     * 
254     * @throws ObjectStreamException if there is a problem.
255     */
256    private Object readResolve() throws ObjectStreamException {
257        TextAnchor result = null;
258        if (this.equals(TextAnchor.TOP_LEFT)) {
259            result = TextAnchor.TOP_LEFT;
260        }
261        else if (this.equals(TextAnchor.TOP_CENTER)) {
262            result = TextAnchor.TOP_CENTER;
263        }
264        else if (this.equals(TextAnchor.TOP_RIGHT)) {
265            result = TextAnchor.TOP_RIGHT;
266        }
267        else if (this.equals(TextAnchor.BOTTOM_LEFT)) {
268            result = TextAnchor.BOTTOM_LEFT;
269        }
270        else if (this.equals(TextAnchor.BOTTOM_CENTER)) {
271            result = TextAnchor.BOTTOM_CENTER;
272        }
273        else if (this.equals(TextAnchor.BOTTOM_RIGHT)) {
274            result = TextAnchor.BOTTOM_RIGHT;
275        }
276        else if (this.equals(TextAnchor.BASELINE_LEFT)) {
277            result = TextAnchor.BASELINE_LEFT;
278        }
279        else if (this.equals(TextAnchor.BASELINE_CENTER)) {
280            result = TextAnchor.BASELINE_CENTER;
281        }
282        else if (this.equals(TextAnchor.BASELINE_RIGHT)) {
283            result = TextAnchor.BASELINE_RIGHT;
284        }
285        else if (this.equals(TextAnchor.CENTER_LEFT)) {
286            result = TextAnchor.CENTER_LEFT;
287        }
288        else if (this.equals(TextAnchor.CENTER)) {
289            result = TextAnchor.CENTER;
290        }
291        else if (this.equals(TextAnchor.CENTER_RIGHT)) {
292            result = TextAnchor.CENTER_RIGHT;
293        }
294        else if (this.equals(TextAnchor.HALF_ASCENT_LEFT)) {
295            result = TextAnchor.HALF_ASCENT_LEFT;
296        }
297        else if (this.equals(TextAnchor.HALF_ASCENT_CENTER)) {
298            result = TextAnchor.HALF_ASCENT_CENTER;
299        }
300        else if (this.equals(TextAnchor.HALF_ASCENT_RIGHT)) {
301            result = TextAnchor.HALF_ASCENT_RIGHT;
302        }
303        return result;
304    }
305
306}