KickJava   Java API By Example, From Geeks To Geeks.

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


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

48
49 package org.jfree.chart;
50
51 import java.awt.Graphics2D JavaDoc;
52 import java.awt.geom.Rectangle2D JavaDoc;
53 import java.io.IOException JavaDoc;
54 import java.io.ObjectInputStream JavaDoc;
55 import java.io.ObjectOutputStream JavaDoc;
56 import java.io.Serializable JavaDoc;
57
58 import javax.swing.event.EventListenerList JavaDoc;
59
60 import org.jfree.chart.event.LegendChangeEvent;
61 import org.jfree.chart.event.LegendChangeListener;
62
63 /**
64  * A chart legend shows the names and visual representations of the series that
65  * are plotted in a chart.
66  *
67  * @see StandardLegend
68  *
69  * @author David Gilbert
70  */

71 public abstract class Legend implements Serializable JavaDoc, Cloneable JavaDoc {
72
73     /** Constant anchor value for legend position WEST. */
74     public static final int WEST = 0x00;
75
76     /** Constant anchor value for legend position NORTH. */
77     public static final int NORTH = 0x01;
78
79     /** Constant anchor value for legend position EAST. */
80     public static final int EAST = 0x02;
81
82     /** Constant anchor value for legend position SOUTH. */
83     public static final int SOUTH = 0x03;
84
85     /** Internal value indicating the bit holding the value of interest in the anchor value. */
86     protected static final int INVERTED = 1 << 1;
87
88     /** Internal value indicating the bit holding the value of interest in the anchor value. */
89     protected static final int HORIZONTAL = 1 << 0;
90
91     /** The current location anchor of the legend. */
92     private int anchor = SOUTH;
93
94     /** A reference to the chart that the legend belongs to (used for access to the dataset). */
95     protected JFreeChart chart;
96
97     /** Storage for registered change listeners. */
98     private transient EventListenerList JavaDoc listenerList;
99
100     /**
101      * Static factory method that returns a concrete subclass of Legend.
102      *
103      * @param chart the chart that the legend belongs to.
104      *
105      * @return a StandardLegend.
106      */

107     public static Legend createInstance(JFreeChart chart) {
108         return new StandardLegend(chart);
109     }
110
111     /**
112      * Default constructor.
113      */

114     public Legend() {
115         this(null);
116     }
117
118     /**
119      * Creates a new legend.
120      *
121      * @param chart the chart that the legend belongs to.
122      */

123     protected Legend(JFreeChart chart) {
124         this.chart = chart;
125         this.listenerList = new EventListenerList JavaDoc();
126     }
127
128     /**
129      * Returns the chart that this legend belongs to.
130      *
131      * @return the chart.
132      */

133     public JFreeChart getChart() {
134         return this.chart;
135     }
136
137     /**
138      * Draws the legend on a Java 2D graphics device (such as the screen or a printer).
139      *
140      * @param g2 the graphics device.
141      * @param available the area within which the legend (and plot) should be drawn.
142      * @param info a carrier for returning information about the entities in the legend.
143      *
144      * @return the area remaining after the legend has drawn itself.
145      */

146     public abstract Rectangle2D JavaDoc draw(Graphics2D JavaDoc g2, Rectangle2D JavaDoc available, ChartRenderingInfo info);
147
148     /**
149      * Registers an object for notification of changes to the legend.
150      *
151      * @param listener the object that is being registered.
152      */

153     public void addChangeListener(LegendChangeListener listener) {
154         this.listenerList.add(LegendChangeListener.class, listener);
155     }
156
157     /**
158      * Deregisters an object for notification of changes to the legend.
159      *
160      * @param listener the object that is being deregistered.
161      */

162     public void removeChangeListener(LegendChangeListener listener) {
163         this.listenerList.remove(LegendChangeListener.class, listener);
164     }
165
166     /**
167      * Notifies all registered listeners that the chart legend has changed in some way.
168      *
169      * @param event information about the change to the legend.
170      */

171     protected void notifyListeners(LegendChangeEvent event) {
172
173         Object JavaDoc[] 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     /**
183      * Returns the current anchor of this legend.
184      * <p>
185      * The default anchor for this legend is <code>SOUTH</code>.
186      *
187      * @return the current anchor.
188      */

189     public int getAnchor() {
190         return this.anchor;
191     }
192
193     /**
194      * Sets the current anchor of this legend.
195      * <P>
196      * The anchor can be one of: <code>NORTH</code>, <code>SOUTH</code>, <code>EAST</code>,
197      * <code>WEST</code>. If a valid anchor value is provided, the current anchor is set and an
198      * update event is triggered. Otherwise, no change is made.
199      *
200      * @param anchor the new anchor value.
201      */

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     /**
216      * Tests this legend for equality with another object.
217      *
218      * @param obj the object.
219      *
220      * @return <code>true</code> or <code>false</code>.
221      */

222     public boolean equals(Object JavaDoc 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     /**
242      * Provides serialization support.
243      *
244      * @param stream the output stream.
245      *
246      * @throws IOException if there is an I/O error.
247      */

248     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
249         stream.defaultWriteObject();
250     }
251
252     /**
253      * Provides serialization support.
254      *
255      * @param stream the input stream.
256      *
257      * @throws IOException if there is an I/O error.
258      * @throws ClassNotFoundException if there is a classpath problem.
259      */

260     private void readObject(ObjectInputStream JavaDoc stream) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
261         stream.defaultReadObject();
262         this.listenerList = new EventListenerList JavaDoc(); // todo: make sure this is populated.
263
}
264
265     /**
266      * Clones the legend, and takes care of listeners.
267      * Note: the cloned legend refer to the same chart as the original one.
268      * JFreeChart clone() takes care of setting the references correctly.
269      *
270      * @return A clone.
271      *
272      * @throws CloneNotSupportedException if the object cannot be cloned.
273      */

274     protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
275         Legend ret = (Legend) super.clone();
276         this.listenerList = new EventListenerList JavaDoc();
277         return ret;
278     }
279
280 }
281
Popular Tags