1 58 59 package org.jfree.chart; 60 61 import java.awt.Graphics2D ; 62 import java.awt.geom.Rectangle2D ; 63 import java.io.IOException ; 64 import java.io.ObjectInputStream ; 65 import java.io.ObjectOutputStream ; 66 import java.io.Serializable ; 67 68 import javax.swing.event.EventListenerList ; 69 70 import org.jfree.chart.event.LegendChangeEvent; 71 import org.jfree.chart.event.LegendChangeListener; 72 73 79 public abstract class OldLegend implements Serializable , Cloneable { 80 81 82 private static final long serialVersionUID = 7706174413964908242L; 83 84 88 private static final int NORTHWEST = 0xA0; 89 90 94 private static final int NORTHEAST = 0xB0; 95 96 100 private static final int SOUTHEAST = 0xC0; 101 102 106 private static final int SOUTHWEST = 0xD0; 107 108 109 public static final int WEST = 0x00; 110 111 112 public static final int WEST_NORTHWEST = WEST + NORTHWEST; 113 114 115 public static final int WEST_SOUTHWEST = WEST + SOUTHWEST; 116 117 118 public static final int NORTH = 0x01; 119 120 121 public static final int NORTH_NORTHWEST = NORTH + NORTHWEST; 122 123 124 public static final int NORTH_NORTHEAST = NORTH + NORTHEAST; 125 126 127 public static final int EAST = 0x02; 128 129 130 public static final int EAST_NORTHEAST = EAST + NORTHEAST; 131 132 133 public static final int EAST_SOUTHEAST = EAST + SOUTHEAST; 134 135 136 public static final int SOUTH = 0x03; 137 138 139 public static final int SOUTH_SOUTHWEST = SOUTH + SOUTHWEST; 140 141 142 public static final int SOUTH_SOUTHEAST = SOUTH + SOUTHEAST; 143 144 148 protected static final int INVERTED = 1 << 1; 149 150 154 protected static final int HORIZONTAL = 1 << 0; 155 156 157 private int anchor = SOUTH; 158 159 164 private JFreeChart chart; 165 166 167 private transient EventListenerList listenerList; 168 169 176 public static OldLegend createInstance(JFreeChart chart) { 177 return new DefaultOldLegend(); 178 } 179 180 183 public OldLegend() { 184 this.listenerList = new EventListenerList (); 185 } 186 187 192 public JFreeChart getChart() { 193 return this.chart; 194 } 195 196 203 protected void registerChart(JFreeChart chart) { 204 this.chart = chart; 205 } 206 207 219 public abstract Rectangle2D draw(Graphics2D g2, Rectangle2D available, 220 ChartRenderingInfo info); 221 222 227 public void addChangeListener(LegendChangeListener listener) { 228 this.listenerList.add(LegendChangeListener.class, listener); 229 } 230 231 236 public void removeChangeListener(LegendChangeListener listener) { 237 this.listenerList.remove(LegendChangeListener.class, listener); 238 } 239 240 246 protected void notifyListeners(LegendChangeEvent event) { 247 248 Object [] listeners = this.listenerList.getListenerList(); 249 for (int i = listeners.length - 2; i >= 0; i -= 2) { 250 if (listeners[i] == LegendChangeListener.class) { 251 ((LegendChangeListener) listeners[i + 1]).legendChanged(event); 252 } 253 } 254 255 } 256 257 264 public int getAnchor() { 265 return this.anchor; 266 } 267 268 278 public void setAnchor(int anchor) { 279 if (isValidAnchor(anchor)) { 280 this.anchor = anchor; 281 notifyListeners(new LegendChangeEvent(this)); 282 } 283 } 284 285 292 private boolean isValidAnchor(int anchor) { 293 switch (anchor) { 294 case NORTH: 295 case NORTH_NORTHEAST: 296 case NORTH_NORTHWEST: 297 case SOUTH: 298 case SOUTH_SOUTHEAST: 299 case SOUTH_SOUTHWEST: 300 case WEST: 301 case WEST_NORTHWEST: 302 case WEST_SOUTHWEST: 303 case EAST: 304 case EAST_NORTHEAST: 305 case EAST_SOUTHEAST: 306 return true; 307 default: 308 return false; 309 } 310 } 311 312 317 protected boolean isAnchoredToTop() { 318 switch (this.anchor) { 319 case WEST_NORTHWEST: 320 case NORTH_NORTHWEST: 321 case NORTH: 322 case NORTH_NORTHEAST: 323 case EAST_NORTHEAST: 324 return true; 325 default: 326 return false; 327 } 328 } 329 330 337 protected boolean isAnchoredToMiddle() { 338 return this.anchor == EAST || this.anchor == WEST; 339 } 340 341 348 protected boolean isAnchoredToBottom() { 349 switch (this.anchor) { 350 case WEST_SOUTHWEST: 351 case SOUTH_SOUTHWEST: 352 case SOUTH: 353 case SOUTH_SOUTHEAST: 354 case EAST_SOUTHEAST: 355 return true; 356 default: 357 return false; 358 } 359 } 360 361 366 protected boolean isAnchoredToLeft() { 367 switch (this.anchor) { 368 case NORTH_NORTHWEST: 369 case WEST_NORTHWEST: 370 case WEST: 371 case WEST_SOUTHWEST: 372 case SOUTH_SOUTHWEST: 373 return true; 374 default: 375 return false; 376 } 377 } 378 379 386 protected boolean isAnchoredToRight() { 387 switch (this.anchor) { 388 case NORTH_NORTHEAST: 389 case EAST_NORTHEAST: 390 case EAST: 391 case EAST_SOUTHEAST: 392 case SOUTH_SOUTHEAST: 393 return true; 394 default: 395 return false; 396 } 397 } 398 399 406 protected boolean isAnchoredToCenter() { 407 return this.anchor == NORTH || this.anchor == SOUTH; 408 } 409 410 417 public boolean equals(Object obj) { 418 419 if (obj == this) { 420 return true; 421 } 422 if (obj instanceof OldLegend) { 423 OldLegend l = (OldLegend) obj; 424 return (this.anchor == l.anchor); 425 } 426 return false; 427 428 } 429 430 437 private void writeObject(ObjectOutputStream stream) throws IOException { 438 stream.defaultWriteObject(); 439 } 440 441 449 private void readObject(ObjectInputStream stream) 450 throws IOException , ClassNotFoundException { 451 stream.defaultReadObject(); 452 this.listenerList = new EventListenerList (); 453 } 455 456 465 protected Object clone() throws CloneNotSupportedException { 466 OldLegend ret = (OldLegend) super.clone(); 467 this.listenerList = new EventListenerList (); 468 return ret; 469 } 470 471 } 472 | Popular Tags |