Source for org.jfree.chart.plot.dial.AbstractDialLayer

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ----------------------
  28:  * AbstractDialLayer.java
  29:  * ----------------------
  30:  * (C) Copyright 2006-2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 06-Nov-2006 : Version 1 (DG);
  38:  * 17-Nov-2006 : Added visible flag (DG);
  39:  * 16-Oct-2007 : Implemented equals() and clone() (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.chart.plot.dial;
  44: 
  45: import java.io.IOException;
  46: import java.io.ObjectInputStream;
  47: import java.util.Arrays;
  48: import java.util.EventListener;
  49: import java.util.List;
  50: 
  51: import javax.swing.event.EventListenerList;
  52: 
  53: import org.jfree.chart.HashUtilities;
  54: 
  55: /**
  56:  * A base class that can be used to implement a {@link DialLayer}.  It includes
  57:  * an event notification mechanism.
  58:  */
  59: public abstract class AbstractDialLayer implements DialLayer {
  60: 
  61:     /** A flag that controls whether or not the layer is visible. */
  62:     private boolean visible;
  63:     
  64:     /** Storage for registered listeners. */
  65:     private transient EventListenerList listenerList;
  66: 
  67:     /**
  68:      * Creates a new instance.
  69:      */
  70:     protected AbstractDialLayer() {
  71:         this.visible = true;
  72:         this.listenerList = new EventListenerList();
  73:     }
  74:     
  75:     /**
  76:      * Returns <code>true</code> if this layer is visible (should be displayed),
  77:      * and <code>false</code> otherwise.
  78:      * 
  79:      * @return A boolean.
  80:      * 
  81:      * @see #setVisible(boolean)
  82:      */
  83:     public boolean isVisible() {
  84:         return this.visible;
  85:     }
  86:     
  87:     /**
  88:      * Sets the flag that determines whether or not this layer is drawn by
  89:      * the plot, and sends a {@link DialLayerChangeEvent} to all registered
  90:      * listeners.
  91:      * 
  92:      * @param visible  the flag.
  93:      * 
  94:      * @see #isVisible()
  95:      */
  96:     public void setVisible(boolean visible) {
  97:         this.visible = visible;
  98:         notifyListeners(new DialLayerChangeEvent(this));
  99:     }
 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: }