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 * AbstractDialLayer.java
029 * ----------------------
030 * (C) Copyright 2006-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.plot.dial;
038
039import java.io.IOException;
040import java.io.ObjectInputStream;
041import java.util.Arrays;
042import java.util.EventListener;
043import java.util.List;
044
045import javax.swing.event.EventListenerList;
046
047import org.jfree.chart.HashUtils;
048
049/**
050 * A base class that can be used to implement a {@link DialLayer}.  It includes
051 * an event notification mechanism.
052 */
053public abstract class AbstractDialLayer implements DialLayer {
054
055    /** A flag that controls whether or not the layer is visible. */
056    private boolean visible;
057
058    /** Storage for registered listeners. */
059    private transient EventListenerList listenerList;
060
061    /**
062     * Creates a new instance.
063     */
064    protected AbstractDialLayer() {
065        this.visible = true;
066        this.listenerList = new EventListenerList();
067    }
068
069    /**
070     * Returns {@code true} if this layer is visible (should be displayed),
071     * and {@code false} otherwise.
072     *
073     * @return A boolean.
074     *
075     * @see #setVisible(boolean)
076     */
077    @Override
078    public boolean isVisible() {
079        return this.visible;
080    }
081
082    /**
083     * Sets the flag that determines whether or not this layer is drawn by
084     * the plot, and sends a {@link DialLayerChangeEvent} to all registered
085     * listeners.
086     *
087     * @param visible  the flag.
088     *
089     * @see #isVisible()
090     */
091    public void setVisible(boolean visible) {
092        this.visible = visible;
093        notifyListeners(new DialLayerChangeEvent(this));
094    }
095
096    /**
097     * Tests this instance for equality with an arbitrary object.
098     *
099     * @param obj  the object ({@code null} permitted).
100     *
101     * @return A boolean.
102     */
103    @Override
104    public boolean equals(Object obj) {
105        if (obj == this) {
106            return true;
107        }
108        if (!(obj instanceof AbstractDialLayer)) {
109            return false;
110        }
111        AbstractDialLayer that = (AbstractDialLayer) obj;
112        return this.visible == that.visible;
113    }
114
115    /**
116     * Returns a hash code for this instance.
117     *
118     * @return A hash code.
119     */
120    @Override
121    public int hashCode() {
122        int result = 23;
123        result = HashUtils.hashCode(result, this.visible);
124        return result;
125    }
126
127    /**
128     * Returns a clone of this instance.
129     *
130     * @return A clone.
131     *
132     * @throws CloneNotSupportedException if there is a problem cloning this
133     *     instance.
134     */
135    @Override
136    public Object clone() throws CloneNotSupportedException {
137        AbstractDialLayer clone = (AbstractDialLayer) super.clone();
138        // we don't clone the listeners
139        clone.listenerList = new EventListenerList();
140        return clone;
141    }
142
143    /**
144     * Registers an object for notification of changes to the dial layer.
145     *
146     * @param listener  the object that is being registered.
147     *
148     * @see #removeChangeListener(DialLayerChangeListener)
149     */
150    @Override
151    public void addChangeListener(DialLayerChangeListener listener) {
152        this.listenerList.add(DialLayerChangeListener.class, listener);
153    }
154
155    /**
156     * Deregisters an object for notification of changes to the dial layer.
157     *
158     * @param listener  the object to deregister.
159     *
160     * @see #addChangeListener(DialLayerChangeListener)
161     */
162    @Override
163    public void removeChangeListener(DialLayerChangeListener listener) {
164        this.listenerList.remove(DialLayerChangeListener.class, listener);
165    }
166
167    /**
168     * Returns {@code true} if the specified object is registered with
169     * the dataset as a listener.  Most applications won't need to call this
170     * method, it exists mainly for use by unit testing code.
171     *
172     * @param listener  the listener.
173     *
174     * @return A boolean.
175     */
176    @Override
177    public boolean hasListener(EventListener listener) {
178        List list = Arrays.asList(this.listenerList.getListenerList());
179        return list.contains(listener);
180    }
181
182    /**
183     * Notifies all registered listeners that the dial layer has changed.
184     * The {@link DialLayerChangeEvent} provides information about the change.
185     *
186     * @param event  information about the change to the axis.
187     */
188    protected void notifyListeners(DialLayerChangeEvent event) {
189        Object[] listeners = this.listenerList.getListenerList();
190        for (int i = listeners.length - 2; i >= 0; i -= 2) {
191            if (listeners[i] == DialLayerChangeListener.class) {
192                ((DialLayerChangeListener) listeners[i + 1]).dialLayerChanged(
193                        event);
194            }
195        }
196    }
197
198    /**
199     * Provides serialization support.
200     *
201     * @param stream  the input stream.
202     *
203     * @throws IOException  if there is an I/O error.
204     * @throws ClassNotFoundException  if there is a classpath problem.
205     */
206    private void readObject(ObjectInputStream stream)
207        throws IOException, ClassNotFoundException {
208        stream.defaultReadObject();
209        this.listenerList = new EventListenerList();
210    }
211
212}