1 46 47 package org.jfree.chart; 48 49 import java.awt.Graphics2D ; 50 import java.awt.geom.Rectangle2D ; 51 import java.io.IOException ; 52 import java.io.ObjectInputStream ; 53 import java.io.ObjectOutputStream ; 54 import java.io.Serializable ; 55 56 import javax.swing.event.EventListenerList ; 57 58 import org.jfree.chart.event.TitleChangeEvent; 59 import org.jfree.chart.event.TitleChangeListener; 60 import org.jfree.util.ObjectUtils; 61 62 76 public abstract class AbstractTitle extends Object implements Cloneable , Serializable { 77 78 79 public static final int TOP = 0; 80 81 82 public static final int BOTTOM = 1; 83 84 85 public static final int RIGHT = 2; 86 87 88 public static final int LEFT = 3; 89 90 91 public static final int NORTH = 0; 92 93 94 public static final int SOUTH = 1; 95 96 97 public static final int EAST = 2; 98 99 100 public static final int WEST = 3; 101 102 103 public static final int CENTER = 4; 104 105 106 public static final int MIDDLE = 4; 107 108 109 public static final int DEFAULT_POSITION = AbstractTitle.TOP; 110 111 112 public static final int DEFAULT_HORIZONTAL_ALIGNMENT = AbstractTitle.CENTER; 113 114 115 public static final int DEFAULT_VERTICAL_ALIGNMENT = AbstractTitle.MIDDLE; 116 117 118 public static final Spacer DEFAULT_SPACER = new Spacer(Spacer.RELATIVE, 0.01, 0.30, 0.01, 0.15); 119 120 124 private int position; 125 126 127 private int horizontalAlignment; 128 129 130 private int verticalAlignment; 131 132 133 private Spacer spacer; 134 135 136 private transient EventListenerList listenerList; 137 138 139 private boolean notify; 140 141 144 protected AbstractTitle() { 145 146 this(AbstractTitle.DEFAULT_POSITION, 147 AbstractTitle.DEFAULT_HORIZONTAL_ALIGNMENT, 148 AbstractTitle.DEFAULT_VERTICAL_ALIGNMENT, 149 AbstractTitle.DEFAULT_SPACER); 150 151 } 152 153 160 protected AbstractTitle(int position, int horizontalAlignment, int verticalAlignment) { 161 162 this(position, 163 horizontalAlignment, verticalAlignment, 164 AbstractTitle.DEFAULT_SPACER); 165 166 } 167 168 180 protected AbstractTitle(int position, 181 int horizontalAlignment, int verticalAlignment, 182 Spacer spacer) { 183 184 if (!isValidPosition(position)) { 186 throw new IllegalArgumentException ("AbstractTitle(): invalid position."); 187 } 188 189 if (!AbstractTitle.isValidHorizontalAlignment(horizontalAlignment)) { 190 throw new IllegalArgumentException ("AbstractTitle(): invalid horizontal alignment."); 191 } 192 193 if (!AbstractTitle.isValidVerticalAlignment(verticalAlignment)) { 194 throw new IllegalArgumentException ("AbstractTitle(): invalid vertical alignment."); 195 } 196 if (spacer == null) { 197 throw new NullPointerException ("AbstractTitle(..): Spacer is null."); 198 } 199 200 this.position = position; 202 this.horizontalAlignment = horizontalAlignment; 203 this.verticalAlignment = verticalAlignment; 204 this.spacer = spacer; 205 this.listenerList = new EventListenerList (); 206 this.notify = true; 207 208 } 209 210 217 public int getPosition() { 218 return this.position; 219 } 220 221 227 public void setPosition(int position) { 228 229 if (this.position != position) { 230 if (!isValidPosition(position)) { 232 throw new IllegalArgumentException ("AbstractTitle(): invalid position."); 233 } 234 this.position = position; 235 notifyListeners(new TitleChangeEvent(this)); 236 } 237 } 238 239 245 public int getHorizontalAlignment() { 246 return this.horizontalAlignment; 247 } 248 249 256 public void setHorizontalAlignment(int alignment) { 257 if (this.horizontalAlignment != alignment) { 258 if (!AbstractTitle.isValidHorizontalAlignment(horizontalAlignment)) { 259 throw new IllegalArgumentException ( 260 "AbstractTitle.setHorizontalAlignment(): invalid horizontal alignment."); 261 } 262 this.horizontalAlignment = alignment; 263 notifyListeners(new TitleChangeEvent(this)); 264 } 265 } 266 267 273 public int getVerticalAlignment() { 274 return this.verticalAlignment; 275 } 276 277 285 public void setVerticalAlignment(int alignment) { 286 if (this.verticalAlignment != alignment) { 287 if (!AbstractTitle.isValidVerticalAlignment(verticalAlignment)) { 288 throw new IllegalArgumentException ( 289 "AbstractTitle.setVerticalAlignment(): invalid vertical alignment."); 290 } 291 this.verticalAlignment = alignment; 292 notifyListeners(new TitleChangeEvent(this)); 293 } 294 } 295 296 302 public Spacer getSpacer() { 303 return this.spacer; 304 } 305 306 312 public void setSpacer(Spacer spacer) { 313 314 if (spacer == null) { 315 throw new NullPointerException ("AbstractTitle.setSpacer(..): Null argument."); 316 } 317 if (!this.spacer.equals(spacer)) { 318 this.spacer = spacer; 319 notifyListeners(new TitleChangeEvent(this)); 320 } 321 322 } 323 324 329 public boolean getNotify() { 330 return this.notify; 331 } 332 333 340 public void setNotify(boolean flag) { 341 this.notify = flag; 342 } 343 344 351 public abstract boolean isValidPosition(int position); 352 353 362 public abstract double getPreferredWidth(Graphics2D g2); 363 364 373 public abstract double getPreferredHeight(Graphics2D g2); 374 375 381 public abstract void draw(Graphics2D g2, Rectangle2D titleArea); 382 383 393 public Object clone() { 394 395 AbstractTitle duplicate = null; 396 397 try { 398 duplicate = (AbstractTitle) super.clone(); 399 } 400 catch (CloneNotSupportedException e) { 401 throw new RuntimeException ("AbstractTitle.clone()"); 403 } 404 405 duplicate.listenerList = new EventListenerList (); 406 407 return duplicate; 409 } 410 411 416 public void addChangeListener(TitleChangeListener listener) { 417 this.listenerList.add(TitleChangeListener.class, listener); 418 } 419 420 425 public void removeChangeListener(TitleChangeListener listener) { 426 this.listenerList.remove(TitleChangeListener.class, listener); 427 } 428 429 434 protected void notifyListeners(TitleChangeEvent event) { 435 436 if (this.notify) { 437 438 Object [] listeners = this.listenerList.getListenerList(); 439 for (int i = listeners.length - 2; i >= 0; i -= 2) { 440 if (listeners[i] == TitleChangeListener.class) { 441 ((TitleChangeListener) listeners[i + 1]).titleChanged(event); 442 } 443 } 444 } 445 446 } 447 448 455 protected static boolean isValidHorizontalAlignment(int code) { 456 457 switch (code) { 458 case AbstractTitle.LEFT: 459 return true; 460 case AbstractTitle.MIDDLE: 461 return true; 462 case AbstractTitle.RIGHT: 463 return true; 464 default: 465 return false; 466 } 467 468 } 469 470 477 protected static boolean isValidVerticalAlignment(int code) { 478 479 switch (code) { 480 case AbstractTitle.TOP: 481 return true; 482 case AbstractTitle.MIDDLE: 483 return true; 484 case AbstractTitle.BOTTOM: 485 return true; 486 default: 487 return false; 488 } 489 490 } 491 492 499 public boolean equals(Object obj) { 500 501 if (obj == null) { 502 return false; 503 } 504 505 if (obj == this) { 506 return true; 507 } 508 509 if (obj instanceof AbstractTitle) { 510 511 AbstractTitle t = (AbstractTitle) obj; 512 513 if (this.position != t.position) { 514 return false; 515 } 516 if (this.horizontalAlignment != t.horizontalAlignment) { 517 return false; 518 } 519 if (this.verticalAlignment != t.verticalAlignment) { 520 return false; 521 } 522 if (ObjectUtils.equal(this.spacer, t.spacer) == false) { 523 return false; 524 } 525 if (this.notify != t.notify) { 526 return false; 527 } 528 529 return true; 530 531 } 532 533 return false; 534 535 } 536 537 544 private void writeObject(ObjectOutputStream stream) throws IOException { 545 stream.defaultWriteObject(); 546 } 547 548 556 private void readObject(ObjectInputStream stream) throws IOException , ClassNotFoundException { 557 stream.defaultReadObject(); 558 this.listenerList = new EventListenerList (); 559 } 560 561 } 562 | Popular Tags |