1 61 62 package org.jfree.chart.title; 63 64 import java.awt.Graphics2D ; 65 import java.awt.geom.Rectangle2D ; 66 import java.io.IOException ; 67 import java.io.ObjectInputStream ; 68 import java.io.ObjectOutputStream ; 69 import java.io.Serializable ; 70 71 import javax.swing.event.EventListenerList ; 72 73 import org.jfree.chart.block.AbstractBlock; 74 import org.jfree.chart.block.Block; 75 import org.jfree.chart.event.TitleChangeEvent; 76 import org.jfree.chart.event.TitleChangeListener; 77 import org.jfree.ui.HorizontalAlignment; 78 import org.jfree.ui.RectangleEdge; 79 import org.jfree.ui.RectangleInsets; 80 import org.jfree.ui.VerticalAlignment; 81 import org.jfree.util.ObjectUtilities; 82 83 92 public abstract class Title extends AbstractBlock 93 implements Block, Cloneable , Serializable { 94 95 96 private static final long serialVersionUID = -6675162505277817221L; 97 98 99 public static final RectangleEdge DEFAULT_POSITION = RectangleEdge.TOP; 100 101 102 public static final HorizontalAlignment 103 DEFAULT_HORIZONTAL_ALIGNMENT = HorizontalAlignment.CENTER; 104 105 106 public static final VerticalAlignment 107 DEFAULT_VERTICAL_ALIGNMENT = VerticalAlignment.CENTER; 108 109 110 public static final RectangleInsets DEFAULT_PADDING = new RectangleInsets( 111 1, 1, 1, 1 112 ); 113 114 115 private RectangleEdge position; 116 117 118 private HorizontalAlignment horizontalAlignment; 119 120 121 private VerticalAlignment verticalAlignment; 122 123 124 private transient EventListenerList listenerList; 125 126 129 private boolean notify; 130 131 134 protected Title() { 135 this( 136 Title.DEFAULT_POSITION, 137 Title.DEFAULT_HORIZONTAL_ALIGNMENT, 138 Title.DEFAULT_VERTICAL_ALIGNMENT, 139 Title.DEFAULT_PADDING 140 ); 141 } 142 143 153 protected Title(RectangleEdge position, 154 HorizontalAlignment horizontalAlignment, 155 VerticalAlignment verticalAlignment) { 156 157 this( 158 position, horizontalAlignment, verticalAlignment, 159 Title.DEFAULT_PADDING 160 ); 161 162 } 163 164 178 protected Title(RectangleEdge position, 179 HorizontalAlignment horizontalAlignment, 180 VerticalAlignment verticalAlignment, 181 RectangleInsets padding) { 182 183 if (position == null) { 185 throw new IllegalArgumentException ("Null 'position' argument."); 186 } 187 if (horizontalAlignment == null) { 188 throw new IllegalArgumentException ( 189 "Null 'horizontalAlignment' argument." 190 ); 191 } 192 193 if (verticalAlignment == null) { 194 throw new IllegalArgumentException ( 195 "Null 'verticalAlignment' argument." 196 ); 197 } 198 if (padding == null) { 199 throw new IllegalArgumentException ("Null 'spacer' argument."); 200 } 201 202 this.position = position; 203 this.horizontalAlignment = horizontalAlignment; 204 this.verticalAlignment = verticalAlignment; 205 setPadding(padding); 206 this.listenerList = new EventListenerList (); 207 this.notify = true; 208 209 } 210 211 216 public RectangleEdge getPosition() { 217 return this.position; 218 } 219 220 226 public void setPosition(RectangleEdge position) { 227 if (position == null) { 228 throw new IllegalArgumentException ("Null 'position' argument."); 229 } 230 if (this.position != position) { 231 this.position = position; 232 notifyListeners(new TitleChangeEvent(this)); 233 } 234 } 235 236 241 public HorizontalAlignment getHorizontalAlignment() { 242 return this.horizontalAlignment; 243 } 244 245 252 public void setHorizontalAlignment(HorizontalAlignment alignment) { 253 if (alignment == null) { 254 throw new IllegalArgumentException ("Null 'alignment' argument."); 255 } 256 if (this.horizontalAlignment != alignment) { 257 this.horizontalAlignment = alignment; 258 notifyListeners(new TitleChangeEvent(this)); 259 } 260 } 261 262 267 public VerticalAlignment getVerticalAlignment() { 268 return this.verticalAlignment; 269 } 270 271 278 public void setVerticalAlignment(VerticalAlignment alignment) { 279 if (alignment == null) { 280 throw new IllegalArgumentException ("Null 'alignment' argument."); 281 } 282 if (this.verticalAlignment != alignment) { 283 this.verticalAlignment = alignment; 284 notifyListeners(new TitleChangeEvent(this)); 285 } 286 } 287 288 294 public boolean getNotify() { 295 return this.notify; 296 } 297 298 305 public void setNotify(boolean flag) { 306 this.notify = flag; 307 if (flag) { 308 notifyListeners(new TitleChangeEvent(this)); 309 } 310 } 311 312 320 public abstract void draw(Graphics2D g2, Rectangle2D area); 321 322 334 public Object clone() throws CloneNotSupportedException { 335 336 Title duplicate = (Title) super.clone(); 337 duplicate.listenerList = new EventListenerList (); 338 return duplicate; 340 } 341 342 347 public void addChangeListener(TitleChangeListener listener) { 348 this.listenerList.add(TitleChangeListener.class, listener); 349 } 350 351 356 public void removeChangeListener(TitleChangeListener listener) { 357 this.listenerList.remove(TitleChangeListener.class, listener); 358 } 359 360 367 protected void notifyListeners(TitleChangeEvent event) { 368 if (this.notify) { 369 Object [] listeners = this.listenerList.getListenerList(); 370 for (int i = listeners.length - 2; i >= 0; i -= 2) { 371 if (listeners[i] == TitleChangeListener.class) { 372 ((TitleChangeListener) listeners[i + 1]).titleChanged( 373 event 374 ); 375 } 376 } 377 } 378 } 379 380 387 public boolean equals(Object obj) { 388 if (obj == this) { 389 return true; 390 } 391 if (!(obj instanceof Title)) { 392 return false; 393 } 394 if (!super.equals(obj)) { 395 return false; 396 } 397 Title that = (Title) obj; 398 if (this.position != that.position) { 399 return false; 400 } 401 if (this.horizontalAlignment != that.horizontalAlignment) { 402 return false; 403 } 404 if (this.verticalAlignment != that.verticalAlignment) { 405 return false; 406 } 407 if (this.notify != that.notify) { 408 return false; 409 } 410 return true; 411 } 412 413 418 public int hashCode() { 419 int result = 193; 420 result = 37 * result + ObjectUtilities.hashCode(this.position); 421 result = 37 * result 422 + ObjectUtilities.hashCode(this.horizontalAlignment); 423 result = 37 * result + ObjectUtilities.hashCode(this.verticalAlignment); 424 return result; 425 } 426 427 434 private void writeObject(ObjectOutputStream stream) throws IOException { 435 stream.defaultWriteObject(); 436 } 437 438 446 private void readObject(ObjectInputStream stream) 447 throws IOException , ClassNotFoundException { 448 stream.defaultReadObject(); 449 this.listenerList = new EventListenerList (); 450 } 451 452 } 453 | Popular Tags |