001/*
002 * $Id: AbstractSerializableBean.java 4088 2011-11-17 19:53:49Z kschaefe $
003 *
004 * Copyright 2008 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 * 
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 * 
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 */
021package org.jdesktop.beans;
022
023import java.beans.PropertyChangeListener;
024import java.beans.PropertyChangeSupport;
025import java.beans.VetoableChangeListener;
026import java.beans.VetoableChangeSupport;
027import java.io.IOException;
028import java.io.ObjectInputStream;
029import java.io.ObjectOutputStream;
030import java.io.Serializable;
031
032/**
033 * This subclass enhances {@code AbstractBean} by implementing the
034 * {@code Serializable} interface. {@code AbstractSerializableBean} correctly
035 * serializes all {@code Serializable} listeners that it contains. Implementors
036 * that need to extends {@code AbstractBean} or one of its subclasses and
037 * require serialization should use this class if possible. If it is not
038 * possible to extend this class, the implementation can guide implementors on
039 * how to properly serialize the listeners.
040 * 
041 * @author Karl George Schaefer
042 * 
043 * @see Serializable
044 * @see ObjectInputStream
045 * @see ObjectOutputStream
046 */
047@SuppressWarnings("serial")
048public abstract class AbstractSerializableBean extends AbstractBean implements
049        Serializable {
050    /**
051     * Creates a new instance of {@code AbstractSerializableBean}.
052     */
053    protected AbstractSerializableBean() {
054        super();
055    }
056
057    /**
058     * Creates a new instance of {@code AbstractSerializableBean}, using the
059     * supplied support delegates. Neither of these may be {@code null}.
060     * 
061     * @param pcs
062     *            the property change support class to use
063     * @param vcs
064     *            the vetoable change support class to use
065     * @throws NullPointerException
066     *             if any parameter is {@code null}
067     */
068    protected AbstractSerializableBean(PropertyChangeSupport pcs,
069            VetoableChangeSupport vcs) {
070        super(pcs, vcs);
071    }
072
073    private void writeObject(ObjectOutputStream s) throws IOException {
074        s.defaultWriteObject();
075
076        for (PropertyChangeListener l : getPropertyChangeListeners()) {
077            if (l instanceof Serializable) {
078                s.writeObject(l);
079            }
080        }
081
082        for (VetoableChangeListener l : getVetoableChangeListeners()) {
083            if (l instanceof Serializable) {
084                s.writeObject(l);
085            }
086        }
087
088        s.writeObject(null);
089    }
090
091    private void readObject(ObjectInputStream s) throws ClassNotFoundException,
092            IOException {
093        s.defaultReadObject();
094
095        Object listenerOrNull;
096        while (null != (listenerOrNull = s.readObject())) {
097            if (listenerOrNull instanceof PropertyChangeListener) {
098                addPropertyChangeListener((PropertyChangeListener) listenerOrNull);
099            } else if (listenerOrNull instanceof VetoableChangeListener) {
100                addVetoableChangeListener((VetoableChangeListener) listenerOrNull);
101            }
102        }
103    }
104}