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