1 48 49 package org.jfree.chart; 50 51 import java.awt.Graphics2D ; 52 import java.awt.geom.Rectangle2D ; 53 import java.io.IOException ; 54 import java.io.ObjectInputStream ; 55 import java.io.ObjectOutputStream ; 56 import java.io.Serializable ; 57 58 import javax.swing.event.EventListenerList ; 59 60 import org.jfree.chart.event.LegendChangeEvent; 61 import org.jfree.chart.event.LegendChangeListener; 62 63 71 public abstract class Legend implements Serializable , Cloneable { 72 73 74 public static final int WEST = 0x00; 75 76 77 public static final int NORTH = 0x01; 78 79 80 public static final int EAST = 0x02; 81 82 83 public static final int SOUTH = 0x03; 84 85 86 protected static final int INVERTED = 1 << 1; 87 88 89 protected static final int HORIZONTAL = 1 << 0; 90 91 92 private int anchor = SOUTH; 93 94 95 protected JFreeChart chart; 96 97 98 private transient EventListenerList listenerList; 99 100 107 public static Legend createInstance(JFreeChart chart) { 108 return new StandardLegend(chart); 109 } 110 111 114 public Legend() { 115 this(null); 116 } 117 118 123 protected Legend(JFreeChart chart) { 124 this.chart = chart; 125 this.listenerList = new EventListenerList (); 126 } 127 128 133 public JFreeChart getChart() { 134 return this.chart; 135 } 136 137 146 public abstract Rectangle2D draw(Graphics2D g2, Rectangle2D available, ChartRenderingInfo info); 147 148 153 public void addChangeListener(LegendChangeListener listener) { 154 this.listenerList.add(LegendChangeListener.class, listener); 155 } 156 157 162 public void removeChangeListener(LegendChangeListener listener) { 163 this.listenerList.remove(LegendChangeListener.class, listener); 164 } 165 166 171 protected void notifyListeners(LegendChangeEvent event) { 172 173 Object [] listeners = this.listenerList.getListenerList(); 174 for (int i = listeners.length - 2; i >= 0; i -= 2) { 175 if (listeners[i] == LegendChangeListener.class) { 176 ((LegendChangeListener) listeners[i + 1]).legendChanged(event); 177 } 178 } 179 180 } 181 182 189 public int getAnchor() { 190 return this.anchor; 191 } 192 193 202 public void setAnchor(int anchor) { 203 switch(anchor) { 204 case NORTH: 205 case SOUTH: 206 case WEST: 207 case EAST: 208 this.anchor = anchor; 209 notifyListeners(new LegendChangeEvent(this)); 210 break; 211 default: 212 } 213 } 214 215 222 public boolean equals(Object obj) { 223 224 if (obj == null) { 225 return false; 226 } 227 228 if (obj == this) { 229 return true; 230 } 231 232 if (obj instanceof Legend) { 233 Legend l = (Legend) obj; 234 return (this.anchor == l.anchor); 235 } 236 237 return false; 238 239 } 240 241 248 private void writeObject(ObjectOutputStream stream) throws IOException { 249 stream.defaultWriteObject(); 250 } 251 252 260 private void readObject(ObjectInputStream stream) throws IOException , ClassNotFoundException { 261 stream.defaultReadObject(); 262 this.listenerList = new EventListenerList (); } 264 265 274 protected Object clone() throws CloneNotSupportedException { 275 Legend ret = (Legend) super.clone(); 276 this.listenerList = new EventListenerList (); 277 return ret; 278 } 279 280 } 281 | Popular Tags |