KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > Series


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  * Series.java
24  * -----------
25  * (C) Copyright 2001-2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: Series.java,v 1.7 2003/10/15 08:15:04 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 15-Nov-2001 : Version 1 (DG);
35  * 29-Nov-2001 : Added cloning and property change support (DG);
36  * 30-Jan-2002 : Added a description attribute and changed the constructors to protected (DG);
37  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
38  * 13-Mar-2003 : Implemented Serializable (DG);
39  * 01-May-2003 : Added equals(...) method (DG);
40  * 26-Jun-2003 : Changed listener list to use EventListenerList - see bug 757027 (DG);
41  * 15-Oct-2003 : Added a flag to control whether or not change events are sent to registered
42  * listeners (DG);
43  *
44  */

45
46 package org.jfree.data;
47
48 import java.beans.PropertyChangeListener JavaDoc;
49 import java.beans.PropertyChangeSupport JavaDoc;
50 import java.io.Serializable JavaDoc;
51
52 import javax.swing.event.EventListenerList JavaDoc;
53
54 import org.jfree.util.ObjectUtils;
55
56 /**
57  * Base class representing a data series. Subclasses are left to implement the
58  * actual data structures.
59  * <P>
60  * The series has two properties ("Name" and "Description") for which you can
61  * register a {@link PropertyChangeListener}.
62  * <P>
63  * You can also register a {@link SeriesChangeListener} to receive notification of
64  * changes to the series data.
65  *
66  * @author David Gilbert
67  */

68 public class Series implements Cloneable JavaDoc, Serializable JavaDoc {
69
70     /** The name of the series. */
71     private String JavaDoc name;
72
73     /** A description of the series. */
74     private String JavaDoc description;
75
76     /** Storage for registered change listeners. */
77     private EventListenerList JavaDoc listeners;
78
79     /** Object to support property change notification. */
80     private PropertyChangeSupport JavaDoc propertyChangeSupport;
81
82     /** A flag that controls whether or not changes are notified. */
83     private boolean notify;
84
85     /**
86      * Constructs a series.
87      *
88      * @param name The series name.
89      */

90     protected Series(String JavaDoc name) {
91         this(name, null);
92     }
93
94     /**
95      * Constructs a series.
96      *
97      * @param name the series name.
98      * @param description the series description (<code>null</code> permitted).
99      */

100     protected Series(String JavaDoc name, String JavaDoc description) {
101
102         this.name = name;
103         this.description = description;
104         this.listeners = new EventListenerList JavaDoc();
105         this.propertyChangeSupport = new PropertyChangeSupport JavaDoc(this);
106         this.notify = true;
107         
108     }
109
110     /**
111      * Returns the name of the series.
112      *
113      * @return the name of the series.
114      */

115     public String JavaDoc getName() {
116         return this.name;
117     }
118
119     /**
120      * Sets the name of the series.
121      *
122      * @param name the name.
123      */

124     public void setName(String JavaDoc name) {
125
126         String JavaDoc old = this.name;
127         this.name = name;
128         propertyChangeSupport.firePropertyChange("Name", old, name);
129
130     }
131
132     /**
133      * Returns a description of the series.
134      *
135      * @return A description of the series (possibly <code>null</code>).
136      */

137     public String JavaDoc getDescription() {
138         return this.description;
139     }
140
141     /**
142      * Sets the description of the series.
143      *
144      * @param description the description (<code>null</code> permitted).
145      */

146     public void setDescription(String JavaDoc description) {
147
148         String JavaDoc old = this.description;
149         this.description = description;
150         this.propertyChangeSupport.firePropertyChange("Description", old, description);
151
152     }
153
154     /**
155      * Returns the flag that controls whether or not change events are sent to registered
156      * listeners.
157      *
158      * @return A boolean.
159      */

160     public boolean getNotify() {
161         return this.notify;
162     }
163     
164     /**
165      * Sets the flag that controls whether or not change events are sent to registered
166      * listeners.
167      *
168      * @param notify the new value of the flag.
169      */

170     public void setNotify(boolean notify) {
171         if (this.notify != notify) {
172             this.notify = notify;
173             fireSeriesChanged();
174         }
175     }
176     
177     /**
178      * Returns a clone of the series.
179      * <P>
180      * Notes:
181      * 1. No need to clone the name or description, since String object is immutable.
182      * 2. We set the listener list to empty, since the listeners did not register with the clone.
183      * 3. Same applies to the PropertyChangeSupport instance.
184      *
185      * @return a clone of the series.
186      */

187     public Object JavaDoc clone() {
188
189         Object JavaDoc obj = null;
190
191         try {
192             obj = super.clone();
193         }
194         catch (CloneNotSupportedException JavaDoc e) { // won't get here...
195
System.err.println("Series.clone(): unexpected exception.");
196         }
197
198         Series clone = (Series) obj;
199         clone.listeners = new EventListenerList JavaDoc();
200         clone.propertyChangeSupport = new PropertyChangeSupport JavaDoc(clone);
201
202         return clone;
203
204     }
205
206     /**
207      * Tests the series for equality with another object.
208      *
209      * @param object the object.
210      *
211      * @return <code>true</code> or <code>false</code>.
212      */

213     public boolean equals(Object JavaDoc object) {
214
215         if (object == null) {
216             return false;
217         }
218
219         if (object == this) {
220             return true;
221         }
222
223         boolean result = false;
224
225         if (object instanceof Series) {
226             Series s = (Series) object;
227             boolean b1 = getName().equals(s.getName());
228             boolean b2 = ObjectUtils.equal(getDescription(), s.getDescription());
229             result = b1 && b2;
230         }
231
232         return result;
233
234     }
235
236     /**
237      * Registers an object with this series, to receive notification whenever the series changes.
238      * <P>
239      * Objects being registered must implement the {@link SeriesChangeListener} interface.
240      *
241      * @param listener the listener to register.
242      */

243     public void addChangeListener(SeriesChangeListener listener) {
244         this.listeners.add(SeriesChangeListener.class, listener);
245     }
246
247     /**
248      * Deregisters an object, so that it not longer receives notification whenever the series
249      * changes.
250      *
251      * @param listener the listener to deregister.
252      */

253     public void removeChangeListener(SeriesChangeListener listener) {
254         this.listeners.remove(SeriesChangeListener.class, listener);
255     }
256
257     /**
258      * General method for signalling to registered listeners that the series
259      * has been changed.
260      */

261     public void fireSeriesChanged() {
262         if (this.notify) {
263             notifyListeners(new SeriesChangeEvent(this));
264         }
265     }
266
267     /**
268      * Sends a change event to all registered listeners.
269      *
270      * @param event Contains information about the event that triggered the notification.
271      */

272     protected void notifyListeners(SeriesChangeEvent event) {
273
274         Object JavaDoc[] listenerList = this.listeners.getListenerList();
275         for (int i = listenerList.length - 2; i >= 0; i -= 2) {
276             if (listenerList[i] == SeriesChangeListener.class) {
277                 ((SeriesChangeListener) listenerList[i + 1]).seriesChanged(event);
278             }
279         }
280
281     }
282
283     /**
284      * Adds a property change listener to the series.
285      *
286      * @param listener The listener.
287      */

288     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
289         this.propertyChangeSupport.addPropertyChangeListener(listener);
290     }
291
292     /**
293      * Removes a property change listener from the series.
294      *
295      * @param listener The listener.
296      */

297     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
298         this.propertyChangeSupport.removePropertyChangeListener(listener);
299     }
300
301     /**
302      * Fires a property change event.
303      *
304      * @param property the property key.
305      * @param oldValue the old value.
306      * @param newValue the new value.
307      */

308     protected void firePropertyChange(String JavaDoc property, Object JavaDoc oldValue, Object JavaDoc newValue) {
309        this.propertyChangeSupport.firePropertyChange(property, oldValue, newValue);
310     }
311
312 }
313
Popular Tags