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