001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  Peter Kolb (see patch 2809117);
033 * Contributor(s):   Tracy Hiltbrand (added equals/canEqual/hashCode);
034 *
035 */
036
037package org.jfree.chart.annotations;
038
039import java.io.IOException;
040import java.io.ObjectInputStream;
041import java.io.ObjectOutputStream;
042import java.io.Serializable;
043import java.util.Arrays;
044import java.util.EventListener;
045import java.util.List;
046
047import javax.swing.event.EventListenerList;
048
049import org.jfree.chart.event.AnnotationChangeEvent;
050import org.jfree.chart.event.AnnotationChangeListener;
051
052/**
053 * An abstract implementation of the {@link Annotation} interface, containing a
054 * mechanism for registering change listeners.
055 */
056public abstract class AbstractAnnotation implements Annotation, Cloneable,
057        Serializable {
058
059    /** Storage for registered change listeners. */
060    private transient EventListenerList listenerList;
061
062    /**
063     * A flag that indicates whether listeners should be notified
064     * about changes of the annotation.
065     */
066    private boolean notify = true;
067
068    /**
069     * Constructs an annotation.
070     */
071    protected AbstractAnnotation() {
072        this.listenerList = new EventListenerList();
073    }
074
075    /**
076     * Registers an object to receive notification of changes to the
077     * annotation.
078     *
079     * @param listener  the object to register.
080     *
081     * @see #removeChangeListener(AnnotationChangeListener)
082     */
083    @Override
084    public void addChangeListener(AnnotationChangeListener listener) {
085        this.listenerList.add(AnnotationChangeListener.class, listener);
086    }
087
088    /**
089     * Deregisters an object so that it no longer receives notification of
090     * changes to the annotation.
091     *
092     * @param listener  the object to deregister.
093     *
094     * @see #addChangeListener(AnnotationChangeListener)
095     */
096    @Override
097    public void removeChangeListener(AnnotationChangeListener listener) {
098        this.listenerList.remove(AnnotationChangeListener.class, listener);
099    }
100
101    /**
102     * Returns {@code true} if the specified object is registered with
103     * the annotation as a listener.  Most applications won't need to call this
104     * method, it exists mainly for use by unit testing code.
105     *
106     * @param listener  the listener.
107     *
108     * @return A boolean.
109     *
110     * @see #addChangeListener(AnnotationChangeListener)
111     * @see #removeChangeListener(AnnotationChangeListener)
112     */
113    public boolean hasListener(EventListener listener) {
114        List list = Arrays.asList(this.listenerList.getListenerList());
115        return list.contains(listener);
116    }
117
118    /**
119     * Notifies all registered listeners that the annotation has changed.
120     *
121     * @see #addChangeListener(AnnotationChangeListener)
122     */
123    protected void fireAnnotationChanged() {
124        if (notify) {
125            notifyListeners(new AnnotationChangeEvent(this, this));
126        }
127    }
128
129    /**
130     * Notifies all registered listeners that the annotation has changed.
131     *
132     * @param event  contains information about the event that triggered the
133     *               notification.
134     *
135     * @see #addChangeListener(AnnotationChangeListener)
136     * @see #removeChangeListener(AnnotationChangeListener)
137     */
138    protected void notifyListeners(AnnotationChangeEvent event) {
139
140        Object[] listeners = this.listenerList.getListenerList();
141        for (int i = listeners.length - 2; i >= 0; i -= 2) {
142            if (listeners[i] == AnnotationChangeListener.class) {
143                ((AnnotationChangeListener) listeners[i + 1]).annotationChanged(
144                        event);
145            }
146        }
147
148    }
149
150    /**
151     * Returns a flag that indicates whether listeners should be 
152     * notified about changes to the annotation.
153     *
154     * @return  the flag.
155     *
156     * @see #setNotify(boolean)
157     */
158    public boolean getNotify(){
159        return this.notify;
160    }
161
162    /**
163     * Sets a flag that indicates whether listeners should be notified about
164     * changes of an annotation.
165     *
166     * @param flag  the flag
167     *
168     * @see #getNotify()
169     */
170    public void setNotify(boolean flag){
171        this.notify = flag;
172        if (notify) {
173            fireAnnotationChanged();
174        }
175    }
176
177    /**
178     * Returns a clone of the annotation. The cloned annotation will NOT 
179     * include the {@link AnnotationChangeListener} references that have been
180     * registered with this annotation.
181     *
182     * @return A clone.
183     *
184     * @throws CloneNotSupportedException  if the annotation does not support
185     *                                     cloning.
186     */
187    @Override
188    public Object clone() throws CloneNotSupportedException {
189        AbstractAnnotation clone = (AbstractAnnotation) super.clone();
190        clone.listenerList = new EventListenerList();
191        return clone;
192    }
193
194    /**
195     * Handles serialization.
196     *
197     * @param stream  the output stream.
198     *
199     * @throws IOException if there is an I/O problem.
200     */
201    private void writeObject(ObjectOutputStream stream) throws IOException {
202        stream.defaultWriteObject();
203    }
204
205    /**
206     * Restores a serialized object.
207     *
208     * @param stream  the input stream.
209     *
210     * @throws IOException if there is an I/O problem.
211     * @throws ClassNotFoundException if there is a problem loading a class.
212     */
213    private void readObject(ObjectInputStream stream)
214        throws IOException, ClassNotFoundException {
215        stream.defaultReadObject();
216        this.listenerList = new EventListenerList();
217    }
218
219    @Override
220    public int hashCode() {
221        int hash = 7;
222        hash = 71 * hash + (this.notify ? 1 : 0);
223        return hash;
224    }
225
226    @Override
227    public boolean equals(Object obj) {
228        if (this == obj) {
229            return true;
230        }
231        if (!(obj instanceof AbstractAnnotation)) {
232            return false;
233        }
234        AbstractAnnotation that = (AbstractAnnotation) obj;
235        if (this.notify != that.notify) {
236            return false;
237        }
238
239        // fix the "equals not symmetric" problem
240        if (!that.canEqual(this)) {
241            return false;
242        }
243        return true;
244    }
245
246    /**
247     * Ensures symmetry between super/subclass implementations of equals. For
248     * more detail, see http://jqno.nl/equalsverifier/manual/inheritance.
249     *
250     * @param other Object
251     * 
252     * @return true ONLY if the parameter is THIS class type
253     */
254    public boolean canEqual(Object other) {
255        // fix the "equals not symmetric" problem
256        return (other instanceof AbstractAnnotation);
257    }
258}