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. * AbstractDataset.java
  28. * --------------------
  29. * (C)opyright 2000-2005, by Object Refinery Limited.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): Nicolas Brodu (for Astrium and EADS Corporate Research
  33. * Center);
  34. *
  35. * $Id: AbstractDataset.java,v 1.3 2005/03/04 11:47:16 mungady Exp $
  36. *
  37. * Changes (from 21-Aug-2001)
  38. * --------------------------
  39. * 21-Aug-2001 : Added standard header. Fixed DOS encoding problem (DG);
  40. * 18-Sep-2001 : Updated e-mail address in header (DG);
  41. * 15-Oct-2001 : Moved to new package (com.jrefinery.data.*) (DG);
  42. * 22-Oct-2001 : Renamed DataSource.java --> Dataset.java etc. (DG);
  43. * 17-Nov-2001 : Changed constructor from public to protected, created new
  44. * AbstractSeriesDataset class and transferred series-related
  45. * methods, updated Javadoc comments (DG);
  46. * 04-Mar-2002 : Updated import statements (DG);
  47. * 11-Jun-2002 : Updated for change in the event constructor (DG);
  48. * 07-Aug-2002 : Changed listener list to use
  49. * javax.swing.event.EventListenerList (DG);
  50. * 04-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  51. * 27-Mar-2003 : Implemented Serializable (DG);
  52. * 18-Aug-2003 : Implemented Cloneable (DG);
  53. * 08-Sep-2003 : Serialization fixes (NB);
  54. * 11-Sep-2003 : Cloning Fixes (NB);
  55. */
  56. package org.jfree.data.general;
  57. import java.io.IOException;
  58. import java.io.InvalidObjectException;
  59. import java.io.ObjectInputStream;
  60. import java.io.ObjectInputValidation;
  61. import java.io.ObjectOutputStream;
  62. import java.io.Serializable;
  63. import javax.swing.event.EventListenerList;
  64. /**
  65. * An abstract implementation of the {@link Dataset} interface, containing a
  66. * mechanism for registering change listeners.
  67. */
  68. public abstract class AbstractDataset implements Dataset,
  69. Cloneable,
  70. Serializable,
  71. ObjectInputValidation {
  72. /** The group that the dataset belongs to. */
  73. private DatasetGroup group;
  74. /** Storage for registered change listeners. */
  75. private transient EventListenerList listenerList;
  76. /**
  77. * Constructs a dataset. By default, the dataset is assigned to its own
  78. * group.
  79. */
  80. protected AbstractDataset() {
  81. this.group = new DatasetGroup();
  82. this.listenerList = new EventListenerList();
  83. }
  84. /**
  85. * Returns the dataset group for the dataset.
  86. *
  87. * @return The group.
  88. */
  89. public DatasetGroup getGroup() {
  90. return this.group;
  91. }
  92. /**
  93. * Sets the dataset group for the dataset.
  94. *
  95. * @param group the group (<code>null</code> not permitted).
  96. */
  97. public void setGroup(DatasetGroup group) {
  98. if (group == null) {
  99. throw new IllegalArgumentException("Null 'group' argument.");
  100. }
  101. this.group = group;
  102. }
  103. /**
  104. * Registers an object to receive notification of changes to the dataset.
  105. *
  106. * @param listener the object to register.
  107. */
  108. public void addChangeListener(DatasetChangeListener listener) {
  109. this.listenerList.add(DatasetChangeListener.class, listener);
  110. }
  111. /**
  112. * Deregisters an object so that it no longer receives notification of
  113. * changes to the dataset.
  114. *
  115. * @param listener the object to deregister.
  116. */
  117. public void removeChangeListener(DatasetChangeListener listener) {
  118. this.listenerList.remove(DatasetChangeListener.class, listener);
  119. }
  120. /**
  121. * Notifies all registered listeners that the dataset has changed.
  122. */
  123. protected void fireDatasetChanged() {
  124. notifyListeners(
  125. new DatasetChangeEvent(
  126. this, // source
  127. this // dataset
  128. )
  129. );
  130. }
  131. /**
  132. * Notifies all registered listeners that the dataset has changed.
  133. *
  134. * @param event contains information about the event that triggered the
  135. * notification.
  136. */
  137. protected void notifyListeners(DatasetChangeEvent event) {
  138. Object[] listeners = this.listenerList.getListenerList();
  139. for (int i = listeners.length - 2; i >= 0; i -= 2) {
  140. if (listeners[i] == DatasetChangeListener.class) {
  141. ((DatasetChangeListener) listeners[i + 1]).datasetChanged(
  142. event
  143. );
  144. }
  145. }
  146. }
  147. /**
  148. * Returns a clone of the dataset. The cloned dataset will NOT include the
  149. * {@link DatasetChangeListener} references that have been registered with
  150. * this dataset.
  151. *
  152. * @return A clone.
  153. *
  154. * @throws CloneNotSupportedException if the dataset does not support
  155. * cloning.
  156. */
  157. public Object clone() throws CloneNotSupportedException {
  158. AbstractDataset clone = (AbstractDataset) super.clone();
  159. clone.listenerList = new EventListenerList();
  160. return clone;
  161. }
  162. /**
  163. * Handles serialization.
  164. *
  165. * @param stream the output stream.
  166. *
  167. * @throws IOException if there is an I/O problem.
  168. */
  169. private void writeObject(ObjectOutputStream stream) throws IOException {
  170. stream.defaultWriteObject();
  171. }
  172. /**
  173. * Restores a serialized object.
  174. *
  175. * @param stream the input stream.
  176. *
  177. * @throws IOException if there is an I/O problem.
  178. * @throws ClassNotFoundException if there is a problem loading a class.
  179. */
  180. private void readObject(ObjectInputStream stream)
  181. throws IOException, ClassNotFoundException {
  182. stream.defaultReadObject();
  183. this.listenerList = new EventListenerList();
  184. stream.registerValidation(this, 10); // see comments about priority of
  185. // 10 in validateObject()
  186. }
  187. /**
  188. * Validates the object. We use this opportunity to call listeners who have
  189. * registered during the deserialization process, as listeners are not
  190. * serialized. This method is called by the serialization system after the
  191. * entire graph is read.
  192. *
  193. * This object has registered itself to the system with a priority of 10.
  194. * Other callbacks may register with a higher priority number to be called
  195. * before this object, or with a lower priority number to be called after
  196. * the listeners were notified.
  197. *
  198. * All listeners are supposed to have register by now, either in their
  199. * readObject or validateObject methods. Notify them that this dataset has
  200. * changed.
  201. *
  202. * @exception InvalidObjectException If the object cannot validate itself.
  203. */
  204. public void validateObject() throws InvalidObjectException {
  205. fireDatasetChanged();
  206. }
  207. }