001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -----------------------
028 * AbstractAnnotation.java
029 * -----------------------
030 * (C) Copyright 2009-2013, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  Peter Kolb (see patch 2809117);
033 * Contributor(s):   -;
034 *
035 * Changes:
036 * --------
037 * 20-Jun-2009 : Version 1 (PK);
038 *
039 */
040
041package org.jfree.chart.annotations;
042
043import java.io.IOException;
044import java.io.ObjectInputStream;
045import java.io.ObjectOutputStream;
046import java.io.Serializable;
047import java.util.Arrays;
048import java.util.EventListener;
049import java.util.List;
050
051import javax.swing.event.EventListenerList;
052
053import org.jfree.chart.event.AnnotationChangeEvent;
054import org.jfree.chart.event.AnnotationChangeListener;
055
056/**
057 * An abstract implementation of the {@link Annotation} interface, containing a
058 * mechanism for registering change listeners.
059 *
060 * @since 1.0.14
061 */
062public abstract class AbstractAnnotation implements Annotation, Cloneable,
063        Serializable {
064
065    /** Storage for registered change listeners. */
066    private transient EventListenerList listenerList;
067
068    /**
069     * A flag that indicates whether listeners should be notified
070     * about changes of the annotation.
071     */
072    private boolean notify = true;
073
074    /**
075     * Constructs an annotation.
076     */
077    protected AbstractAnnotation() {
078        this.listenerList = new EventListenerList();
079    }
080
081    /**
082     * Registers an object to receive notification of changes to the
083     * annotation.
084     *
085     * @param listener  the object to register.
086     *
087     * @see #removeChangeListener(AnnotationChangeListener)
088     */
089    @Override
090    public void addChangeListener(AnnotationChangeListener listener) {
091        this.listenerList.add(AnnotationChangeListener.class, listener);
092    }
093
094    /**
095     * Deregisters an object so that it no longer receives notification of
096     * changes to the annotation.
097     *
098     * @param listener  the object to deregister.
099     *
100     * @see #addChangeListener(AnnotationChangeListener)
101     */
102    @Override
103    public void removeChangeListener(AnnotationChangeListener listener) {
104        this.listenerList.remove(AnnotationChangeListener.class, listener);
105    }
106
107    /**
108     * Returns <code>true</code> if the specified object is registered with
109     * the annotation as a listener.  Most applications won't need to call this
110     * method, it exists mainly for use by unit testing code.
111     *
112     * @param listener  the listener.
113     *
114     * @return A boolean.
115     *
116     * @see #addChangeListener(AnnotationChangeListener)
117     * @see #removeChangeListener(AnnotationChangeListener)
118     */
119    public boolean hasListener(EventListener listener) {
120        List list = Arrays.asList(this.listenerList.getListenerList());
121        return list.contains(listener);
122    }
123
124    /**
125     * Notifies all registered listeners that the annotation has changed.
126     *
127     * @see #addChangeListener(AnnotationChangeListener)
128     */
129    protected void fireAnnotationChanged() {
130        if (notify) {
131            notifyListeners(new AnnotationChangeEvent(this, this));
132        }
133    }
134
135    /**
136     * Notifies all registered listeners that the annotation has changed.
137     *
138     * @param event  contains information about the event that triggered the
139     *               notification.
140     *
141     * @see #addChangeListener(AnnotationChangeListener)
142     * @see #removeChangeListener(AnnotationChangeListener)
143     */
144    protected void notifyListeners(AnnotationChangeEvent event) {
145
146        Object[] listeners = this.listenerList.getListenerList();
147        for (int i = listeners.length - 2; i >= 0; i -= 2) {
148            if (listeners[i] == AnnotationChangeListener.class) {
149                ((AnnotationChangeListener) listeners[i + 1]).annotationChanged(
150                        event);
151            }
152        }
153
154    }
155
156    /**
157     * Returns a flag that indicates whether listeners should be 
158     * notified about changes to the annotation.
159     *
160     * @return  the flag.
161     *
162     * @see #setNotify(boolean)
163     */
164    public boolean getNotify(){
165        return this.notify;
166    }
167
168    /**
169     * Sets a flag that indicates whether listeners should be notified about
170     * changes of an annotation.
171     *
172     * @param flag  the flag
173     *
174     * @see #getNotify()
175     */
176    public void setNotify(boolean flag){
177        this.notify = flag;
178        if (notify) {
179            fireAnnotationChanged();
180        }
181    }
182
183    /**
184     * Returns a clone of the annotation. The cloned annotation will NOT 
185     * include the {@link AnnotationChangeListener} references that have been
186     * registered with this annotation.
187     *
188     * @return A clone.
189     *
190     * @throws CloneNotSupportedException  if the annotation does not support
191     *                                     cloning.
192     */
193    @Override
194    public Object clone() throws CloneNotSupportedException {
195        AbstractAnnotation clone = (AbstractAnnotation) super.clone();
196        clone.listenerList = new EventListenerList();
197        return clone;
198    }
199
200    /**
201     * Handles serialization.
202     *
203     * @param stream  the output stream.
204     *
205     * @throws IOException if there is an I/O problem.
206     */
207    private void writeObject(ObjectOutputStream stream) throws IOException {
208        stream.defaultWriteObject();
209    }
210
211    /**
212     * Restores a serialized object.
213     *
214     * @param stream  the input stream.
215     *
216     * @throws IOException if there is an I/O problem.
217     * @throws ClassNotFoundException if there is a problem loading a class.
218     */
219    private void readObject(ObjectInputStream stream)
220        throws IOException, ClassNotFoundException {
221        stream.defaultReadObject();
222        this.listenerList = new EventListenerList();
223    }
224
225}