KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AbstractDataset.java
24  * --------------------
25  * (C)opyright 2000-2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): Nicolas Brodu (for Astrium and EADS Corporate Research Center);
29  *
30  * $Id: AbstractDataset.java,v 1.8 2003/11/03 14:20:04 mungady Exp $
31  *
32  * Changes (from 21-Aug-2001)
33  * --------------------------
34  * 21-Aug-2001 : Added standard header. Fixed DOS encoding problem (DG);
35  * 18-Sep-2001 : Updated e-mail address in header (DG);
36  * 15-Oct-2001 : Moved to new package (com.jrefinery.data.*) (DG);
37  * 22-Oct-2001 : Renamed DataSource.java --> Dataset.java etc. (DG);
38  * 17-Nov-2001 : Changed constructor from public to protected, created new AbstractSeriesDataset
39  * class and transferred series-related methods, updated Javadoc comments (DG);
40  * 04-Mar-2002 : Updated import statements (DG);
41  * 11-Jun-2002 : Updated for change in the event constructor (DG);
42  * 07-Aug-2002 : Changed listener list to use javax.swing.event.EventListenerList (DG);
43  * 04-Oct-2002 : Fixed errors reported by Checkstyle (DG);
44  * 27-Mar-2003 : Implemented Serializable (DG);
45  * 18-Aug-2003 : Implemented Cloneable (DG);
46  * 08-Sep-2003 : Serialization fixes (NB);
47  * 11-Sep-2003 : Cloning Fixes (NB);
48  */

49
50 package org.jfree.data;
51
52 import java.io.IOException JavaDoc;
53 import java.io.InvalidObjectException JavaDoc;
54 import java.io.ObjectInputStream JavaDoc;
55 import java.io.ObjectInputValidation JavaDoc;
56 import java.io.ObjectOutputStream JavaDoc;
57 import java.io.Serializable JavaDoc;
58
59 import javax.swing.event.EventListenerList JavaDoc;
60
61 /**
62  * An abstract implementation of the {@link Dataset} interface, containing a mechanism
63  * for registering change listeners.
64  *
65  * @author David Gilbert
66  */

67 public abstract class AbstractDataset implements Dataset,
68                                                  Cloneable JavaDoc,
69                                                  Serializable JavaDoc,
70                                                  ObjectInputValidation JavaDoc {
71
72     /** The group that the dataset belongs to. */
73     private DatasetGroup group;
74
75     /** Storage for registered change listeners. */
76     private transient EventListenerList JavaDoc listenerList;
77
78     /**
79      * Constructs a dataset.
80      * <P>
81      * By default, the dataset is assigned to its own group.
82      */

83     protected AbstractDataset() {
84         this.group = new DatasetGroup();
85         this.listenerList = new EventListenerList JavaDoc();
86     }
87
88     /**
89      * Returns the dataset group for the dataset.
90      *
91      * @return the dataset group.
92      */

93     public DatasetGroup getGroup() {
94         return this.group;
95     }
96
97     /**
98      * Sets the dataset group for the dataset.
99      *
100      * @param group the dataset group.
101      */

102     public void setGroup(DatasetGroup group) {
103         this.group = group;
104     }
105
106     /**
107      * Registers an object to receive notification of changes to the dataset.
108      *
109      * @param listener the object to register.
110      */

111     public void addChangeListener(DatasetChangeListener listener) {
112         listenerList.add(DatasetChangeListener.class, listener);
113     }
114
115     /**
116      * Deregisters an object so that it no longer receives notification of changes to the dataset.
117      *
118      * @param listener the object to deregister.
119      */

120     public void removeChangeListener(DatasetChangeListener listener) {
121         listenerList.remove(DatasetChangeListener.class, listener);
122     }
123
124     /**
125      * Notifies all registered listeners that the dataset has changed.
126      */

127     protected void fireDatasetChanged() {
128         notifyListeners(new DatasetChangeEvent(this, // source
129
this // dataset
130
));
131     }
132
133     /**
134      * Notifies all registered listeners that the dataset has changed.
135      *
136      * @param event contains information about the event that triggered the notification.
137      */

138     protected void notifyListeners(DatasetChangeEvent event) {
139
140         Object JavaDoc[] listeners = listenerList.getListenerList();
141         for (int i = listeners.length - 2; i >= 0; i -= 2) {
142             if (listeners[i] == DatasetChangeListener.class) {
143                 ((DatasetChangeListener) listeners[i + 1]).datasetChanged(event);
144             }
145         }
146
147     }
148
149     /**
150      * Returns a clone of the dataset.
151      * <p>
152      * The cloned dataset will NOT include the {@link DatasetChangeListener} references that have
153      * been registered with this dataset.
154      *
155      * @return A clone.
156      *
157      * @throws CloneNotSupportedException if the dataset does not support cloning.
158      */

159     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
160         AbstractDataset clone = (AbstractDataset) super.clone();
161         clone.listenerList = new EventListenerList JavaDoc();
162         return clone;
163     }
164     
165     /**
166      * Handles serialization.
167      *
168      * @param stream the output stream.
169      *
170      * @throws IOException if there is an I/O problem.
171      */

172     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
173         stream.defaultWriteObject();
174     }
175
176     /**
177      * Restores a serialized object.
178      *
179      * @param stream the input stream.
180      *
181      * @throws IOException if there is an I/O problem.
182      * @throws ClassNotFoundException if there is a problem loading a class.
183      */

184     private void readObject(ObjectInputStream JavaDoc stream) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
185         stream.defaultReadObject();
186         this.listenerList = new EventListenerList JavaDoc();
187         stream.registerValidation(this, 10); // see comments about priority of 10 in
188
// validateObject()
189
}
190  
191     /**
192      * Validates the object. We use this opportunity to call listeners who have registered during
193      * the deserialization process, as listeners are not serialized. This method is called by the
194      * serialization system after the entire graph is read.
195      *
196      * This object has registered itself to the system with a priority of 10. Other callbacks may
197      * register with a higher priority number to be called before this object, or with a lower
198      * priority number to be called after the listeners were notified.
199      *
200      * All listeners are supposed to have register by now, either in their readObject or
201      * validateObject methods. Notify them that this dataset has changed.
202      *
203      * @exception InvalidObjectException If the object cannot validate itself.
204      */

205     public void validateObject() throws InvalidObjectException JavaDoc {
206        fireDatasetChanged();
207     }
208    
209 }
210
211
212
213
214
215
216
Popular Tags