001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2020, 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 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 * @since 1.0.20 121 */ 122 public boolean isLeft() { 123 return this == BASELINE_LEFT || this == BOTTOM_LEFT 124 || this == CENTER_LEFT || this == HALF_ASCENT_LEFT 125 || this == TOP_LEFT; 126 } 127 128 /** 129 * Returns {@code true} if the anchor is a right-side anchor, and 130 * {@code false} otherwise. 131 * 132 * @return A boolean. 133 * 134 * @since 1.0.20 135 */ 136 public boolean isRight() { 137 return this == BASELINE_RIGHT || this == BOTTOM_RIGHT 138 || this == CENTER_RIGHT || this == HALF_ASCENT_RIGHT 139 || this == TOP_RIGHT; 140 } 141 142 /** 143 * Returns {@code true} if the anchor is a center anchor, and 144 * {@code false} otherwise. 145 * 146 * @return A boolean. 147 * 148 * @since 1.0.20 149 */ 150 public boolean isHorizontalCenter() { 151 return this == BASELINE_CENTER || this == BOTTOM_CENTER 152 || this == CENTER || this == HALF_ASCENT_CENTER 153 || this == TOP_CENTER; 154 } 155 156 /** 157 * Returns {@code true} if the anchor is a top anchor, and 158 * {@code false} otherwise. 159 * 160 * @return A boolean. 161 * 162 * @since 1.0.20 163 */ 164 public boolean isTop() { 165 return this == TOP_LEFT || this == TOP_CENTER || this == TOP_RIGHT; 166 } 167 168 /** 169 * Returns {@code true} if the anchor is a bottom anchor, and 170 * {@code false} otherwise. 171 * 172 * @return A boolean. 173 * 174 * @since 1.0.20 175 */ 176 public boolean isBottom() { 177 return this == BOTTOM_LEFT || this == BOTTOM_CENTER 178 || this == BOTTOM_RIGHT; 179 } 180 181 /** 182 * Returns {@code true} if the anchor is a baseline anchor, and 183 * {@code false} otherwise. 184 * 185 * @return A boolean. 186 * 187 * @since 1.0.20 188 */ 189 public boolean isBaseline() { 190 return this == BASELINE_LEFT || this == BASELINE_CENTER 191 || this == BASELINE_RIGHT; 192 } 193 194 /** 195 * Returns {@code true} if the anchor is a half-ascent anchor, and 196 * {@code false} otherwise. 197 * 198 * @return A boolean. 199 * 200 * @since 1.0.20 201 */ 202 public boolean isHalfAscent() { 203 return this == HALF_ASCENT_LEFT || this == HALF_ASCENT_CENTER 204 || this == HALF_ASCENT_RIGHT; 205 } 206 207 /** 208 * Returns {@code true} if the anchor is a half-ascent anchor, and 209 * {@code false} otherwise. 210 * 211 * @return A boolean. 212 * 213 * @since 1.0.20 214 */ 215 public boolean isVerticalCenter() { 216 return this == CENTER_LEFT || this == CENTER || this == CENTER_RIGHT; 217 } 218 219 /** 220 * Returns a string representing the object. 221 * 222 * @return The string. 223 */ 224 @Override 225 public String toString() { 226 return this.name; 227 } 228 229 /** 230 * Returns {@code true} if this object is equal to the specified 231 * object, and {@code false} otherwise. 232 * 233 * @param o the other object. 234 * 235 * @return A boolean. 236 */ 237 @Override 238 public boolean equals(Object o) { 239 240 if (this == o) { 241 return true; 242 } 243 if (!(o instanceof TextAnchor)) { 244 return false; 245 } 246 247 TextAnchor order = (TextAnchor) o; 248 if (!this.name.equals(order.name)) { 249 return false; 250 } 251 252 return true; 253 } 254 255 /** 256 * Returns a hash code value for the object. 257 * 258 * @return The hashcode 259 */ 260 @Override 261 public int hashCode() { 262 return this.name.hashCode(); 263 } 264 265 /** 266 * Ensures that serialization returns the unique instances. 267 * 268 * @return The object. 269 * 270 * @throws ObjectStreamException if there is a problem. 271 */ 272 private Object readResolve() throws ObjectStreamException { 273 TextAnchor result = null; 274 if (this.equals(TextAnchor.TOP_LEFT)) { 275 result = TextAnchor.TOP_LEFT; 276 } 277 else if (this.equals(TextAnchor.TOP_CENTER)) { 278 result = TextAnchor.TOP_CENTER; 279 } 280 else if (this.equals(TextAnchor.TOP_RIGHT)) { 281 result = TextAnchor.TOP_RIGHT; 282 } 283 else if (this.equals(TextAnchor.BOTTOM_LEFT)) { 284 result = TextAnchor.BOTTOM_LEFT; 285 } 286 else if (this.equals(TextAnchor.BOTTOM_CENTER)) { 287 result = TextAnchor.BOTTOM_CENTER; 288 } 289 else if (this.equals(TextAnchor.BOTTOM_RIGHT)) { 290 result = TextAnchor.BOTTOM_RIGHT; 291 } 292 else if (this.equals(TextAnchor.BASELINE_LEFT)) { 293 result = TextAnchor.BASELINE_LEFT; 294 } 295 else if (this.equals(TextAnchor.BASELINE_CENTER)) { 296 result = TextAnchor.BASELINE_CENTER; 297 } 298 else if (this.equals(TextAnchor.BASELINE_RIGHT)) { 299 result = TextAnchor.BASELINE_RIGHT; 300 } 301 else if (this.equals(TextAnchor.CENTER_LEFT)) { 302 result = TextAnchor.CENTER_LEFT; 303 } 304 else if (this.equals(TextAnchor.CENTER)) { 305 result = TextAnchor.CENTER; 306 } 307 else if (this.equals(TextAnchor.CENTER_RIGHT)) { 308 result = TextAnchor.CENTER_RIGHT; 309 } 310 else if (this.equals(TextAnchor.HALF_ASCENT_LEFT)) { 311 result = TextAnchor.HALF_ASCENT_LEFT; 312 } 313 else if (this.equals(TextAnchor.HALF_ASCENT_CENTER)) { 314 result = TextAnchor.HALF_ASCENT_CENTER; 315 } 316 else if (this.equals(TextAnchor.HALF_ASCENT_RIGHT)) { 317 result = TextAnchor.HALF_ASCENT_RIGHT; 318 } 319 return result; 320 } 321 322}