KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > OldLegend


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -----------
27  * Legend.java
28  * -----------
29  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): Andrzej Porebski;
33  * Jim Moore;
34  * Nicolas Brodu;
35  * Barak Naveh;
36  *
37  * $Id: OldLegend.java,v 1.2 2005/05/19 15:40:55 mungady Exp $
38  *
39  * Changes (from 20-Jun-2001)
40  * --------------------------
41  * 20-Jun-2001 : Modifications submitted by Andrzej Porebski for legend
42  * placement;
43  * 18-Sep-2001 : Updated header and fixed DOS encoding problem (DG);
44  * 07-Nov-2001 : Tidied up Javadoc comments (DG);
45  * 06-Mar-2002 : Updated import statements (DG);
46  * 20-Jun-2002 : Added outlineKeyBoxes attribute suggested by Jim Moore (DG);
47  * 14-Oct-2002 : Changed listener storage structure (DG);
48  * 14-Jan-2003 : Changed constructor to protected, moved outer-gap to
49  * subclass (DG);
50  * 27-Mar-2003 : Implemented Serializable (DG);
51  * 05-Jun-2003 : Added ChartRenderingInfo parameter to draw() method (DG);
52  * 11-Sep-2003 : Cloning support
53  * 26-Mar-2004 : Added 8 more legend anchor points (BN);
54  * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0
55  * release (DG);
56  *
57  */

58
59 package org.jfree.chart;
60
61 import java.awt.Graphics2D JavaDoc;
62 import java.awt.geom.Rectangle2D JavaDoc;
63 import java.io.IOException JavaDoc;
64 import java.io.ObjectInputStream JavaDoc;
65 import java.io.ObjectOutputStream JavaDoc;
66 import java.io.Serializable JavaDoc;
67
68 import javax.swing.event.EventListenerList JavaDoc;
69
70 import org.jfree.chart.event.LegendChangeEvent;
71 import org.jfree.chart.event.LegendChangeListener;
72
73 /**
74  * A chart legend shows the names and visual representations of the series that
75  * are plotted in a chart.
76  *
77  * @see DefaultOldLegend
78  */

79 public abstract class OldLegend implements Serializable JavaDoc, Cloneable JavaDoc {
80
81     /** For serialization. */
82     private static final long serialVersionUID = 7706174413964908242L;
83     
84     /**
85      * Internal value used as NORTHWEST diagonal component of the anchor
86      * value.
87      */

88     private static final int NORTHWEST = 0xA0;
89
90     /**
91      * Internal value used as NORTHEAST diagonal component of the anchor
92      * value.
93      */

94     private static final int NORTHEAST = 0xB0;
95
96     /**
97      * Internal value used as SOUTHEAST diagonal component of the anchor
98      * value.
99      */

100     private static final int SOUTHEAST = 0xC0;
101
102     /**
103      * Internal value used as SOUTHWEST diagonal component of the anchor
104      * value.
105      */

106     private static final int SOUTHWEST = 0xD0;
107     
108     /** Constant anchor value for legend position WEST. */
109     public static final int WEST = 0x00;
110
111     /** Constant anchor value for legend position WEST_NORTHWEST. */
112     public static final int WEST_NORTHWEST = WEST + NORTHWEST;
113
114     /** Constant anchor value for legend position WEST_SOUTHWEST. */
115     public static final int WEST_SOUTHWEST = WEST + SOUTHWEST;
116
117     /** Constant anchor value for legend position NORTH. */
118     public static final int NORTH = 0x01;
119
120     /** Constant anchor value for legend position NORTH_NORTHWEST. */
121     public static final int NORTH_NORTHWEST = NORTH + NORTHWEST;
122
123     /** Constant anchor value for legend position NORTH_NORTHEAST. */
124     public static final int NORTH_NORTHEAST = NORTH + NORTHEAST;
125
126     /** Constant anchor value for legend position EAST. */
127     public static final int EAST = 0x02;
128
129     /** Constant anchor value for legend position EAST_NORTHEAST. */
130     public static final int EAST_NORTHEAST = EAST + NORTHEAST;
131
132     /** Constant anchor value for legend position EAST_SOUTHEAST. */
133     public static final int EAST_SOUTHEAST = EAST + SOUTHEAST;
134
135     /** Constant anchor value for legend position SOUTH. */
136     public static final int SOUTH = 0x03;
137
138     /** Constant anchor value for legend position SOUTH_SOUTHWEST. */
139     public static final int SOUTH_SOUTHWEST = SOUTH + SOUTHWEST;
140
141     /** Constant anchor value for legend position SOUTH_SOUTHEAST. */
142     public static final int SOUTH_SOUTHEAST = SOUTH + SOUTHEAST;
143
144     /**
145      * Internal value indicating the bit holding the value of interest in the
146      * anchor value.
147      */

148     protected static final int INVERTED = 1 << 1;
149
150     /**
151      * Internal value indicating the bit holding the value of interest in the
152      * anchor value.
153      */

154     protected static final int HORIZONTAL = 1 << 0;
155
156     /** The current location anchor of the legend. */
157     private int anchor = SOUTH;
158
159     /**
160      * A reference to the chart that the legend belongs to
161      * (used for access to the dataset).
162      * <!-- use registerChart() instead -->
163      */

164     private JFreeChart chart;
165
166     /** Storage for registered change listeners. */
167     private transient EventListenerList JavaDoc listenerList;
168
169     /**
170      * Static factory method that returns a concrete subclass of Legend.
171      *
172      * @param chart the chart that the legend belongs to.
173      *
174      * @return A StandardLegend.
175      */

176     public static OldLegend createInstance(JFreeChart chart) {
177         return new DefaultOldLegend();
178     }
179
180     /**
181      * Default constructor.
182      */

183     public OldLegend() {
184         this.listenerList = new EventListenerList JavaDoc();
185     }
186
187     /**
188      * Returns the chart that this legend belongs to.
189      *
190      * @return The chart.
191      */

192     public JFreeChart getChart() {
193         return this.chart;
194     }
195
196     /**
197      * Internal maintenance method to update the reference to the central
198      * JFreeChart object.
199      *
200      * @param chart the chart, may be null, if the legend gets removed from
201      * the chart.
202      */

203     protected void registerChart(JFreeChart chart) {
204         this.chart = chart;
205     }
206
207     /**
208      * Draws the legend on a Java 2D graphics device (such as the screen or a
209      * printer).
210      *
211      * @param g2 the graphics device.
212      * @param available the area within which the legend (and plot) should be
213      * drawn.
214      * @param info a carrier for returning information about the entities in
215      * the legend.
216      *
217      * @return The area remaining after the legend has drawn itself.
218      */

219     public abstract Rectangle2D JavaDoc draw(Graphics2D JavaDoc g2, Rectangle2D JavaDoc available,
220                                      ChartRenderingInfo info);
221
222     /**
223      * Registers an object for notification of changes to the legend.
224      *
225      * @param listener the object that is being registered.
226      */

227     public void addChangeListener(LegendChangeListener listener) {
228         this.listenerList.add(LegendChangeListener.class, listener);
229     }
230
231     /**
232      * Deregisters an object for notification of changes to the legend.
233      *
234      * @param listener the object that is being deregistered.
235      */

236     public void removeChangeListener(LegendChangeListener listener) {
237         this.listenerList.remove(LegendChangeListener.class, listener);
238     }
239
240     /**
241      * Notifies all registered listeners that the chart legend has changed in
242      * some way.
243      *
244      * @param event information about the change to the legend.
245      */

246     protected void notifyListeners(LegendChangeEvent event) {
247
248         Object JavaDoc[] 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     /**
258      * Returns the current anchor of this legend.
259      * <p>
260      * The default anchor for this legend is <code>SOUTH</code>.
261      *
262      * @return The current anchor.
263      */

264     public int getAnchor() {
265         return this.anchor;
266     }
267
268     /**
269      * Sets the current anchor of this legend.
270      * <P>
271      * The anchor can be one of: <code>NORTH</code>, <code>SOUTH</code>,
272      * <code>EAST</code>, <code>WEST</code>. If a valid anchor value is
273      * provided, the current anchor is set and an update event is triggered.
274      * Otherwise, no change is made.
275      *
276      * @param anchor the new anchor value.
277      */

278     public void setAnchor(int anchor) {
279         if (isValidAnchor(anchor)) {
280             this.anchor = anchor;
281             notifyListeners(new LegendChangeEvent(this));
282         }
283     }
284
285     /**
286      * Tests if the specified anchor is a valid anchor.
287      *
288      * @param anchor a candidate anchor.
289      * @return <code>true</code> if the anchor is valid; <code>false</code>
290      * otherwise.
291      */

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     /**
313      * Returns <code>true</code> if and only if this legend is anchored to top.
314      *
315      * @return <code>true</code> if and only if this legend is anchored to top.
316      */

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     /**
331      * Returns <code>true</code> if and only if this legend is anchored to
332      * middle.
333      *
334      * @return <code>true</code> if and only if this legend is anchored to
335      * middle.
336      */

337     protected boolean isAnchoredToMiddle() {
338         return this.anchor == EAST || this.anchor == WEST;
339     }
340     
341     /**
342      * Returns <code>true</code> if and only if this legend is anchored to
343      * bottom.
344      *
345      * @return <code>true</code> if and only if this legend is anchored to
346      * bottom.
347      */

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     /**
362      * Returns <code>true</code> if and only if this legend is anchored to left.
363      *
364      * @return <code>true</code> if and only if this legend is anchored to left.
365      */

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     /**
380      * Returns <code>true</code> if and only if this legend is anchored to
381      * right.
382      *
383      * @return <code>true</code> if and only if this legend is anchored to
384      * right.
385      */

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     /**
400      * Returns <code>true</code> if and only if this legend is anchored to
401      * center.
402      *
403      * @return <code>true</code> if and only if this legend is anchored to
404      * center.
405      */

406     protected boolean isAnchoredToCenter() {
407         return this.anchor == NORTH || this.anchor == SOUTH;
408     }
409     
410     /**
411      * Tests this legend for equality with another object.
412      *
413      * @param obj the object.
414      *
415      * @return <code>true</code> or <code>false</code>.
416      */

417     public boolean equals(Object JavaDoc 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     /**
431      * Provides serialization support.
432      *
433      * @param stream the output stream.
434      *
435      * @throws IOException if there is an I/O error.
436      */

437     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
438         stream.defaultWriteObject();
439     }
440
441     /**
442      * Provides serialization support.
443      *
444      * @param stream the input stream.
445      *
446      * @throws IOException if there is an I/O error.
447      * @throws ClassNotFoundException if there is a classpath problem.
448      */

449     private void readObject(ObjectInputStream JavaDoc stream)
450         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
451         stream.defaultReadObject();
452         this.listenerList = new EventListenerList JavaDoc();
453         // TODO: make sure this is populated.
454
}
455
456     /**
457      * Clones the legend, and takes care of listeners.
458      * Note: the cloned legend refer to the same chart as the original one.
459      * JFreeChart clone() takes care of setting the references correctly.
460      *
461      * @return A clone.
462      *
463      * @throws CloneNotSupportedException if the object cannot be cloned.
464      */

465     protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
466         OldLegend ret = (OldLegend) super.clone();
467         this.listenerList = new EventListenerList JavaDoc();
468         return ret;
469     }
470
471 }
472
Popular Tags