KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > experimental > chart > plot > dial > AbstractDialLayer


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, 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, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: AbstractDialLayer.java,v 1.1.2.2 2006/11/17 11:06:44 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 06-Nov-2006 : Version 1 (DG);
40  * 17-Nov-2007 : Added visible flag (DG);
41  *
42  */

43
44 package org.jfree.experimental.chart.plot.dial;
45
46 import java.io.IOException JavaDoc;
47 import java.io.ObjectInputStream JavaDoc;
48 import java.util.Arrays JavaDoc;
49 import java.util.EventListener JavaDoc;
50 import java.util.List JavaDoc;
51
52 import javax.swing.event.EventListenerList JavaDoc;
53
54 /**
55  * A base class that can be used to implement a {@link DialLayer}. It includes
56  * an event notification mechanism.
57  */

58 public abstract class AbstractDialLayer implements DialLayer {
59
60     /** A flag that controls whether or not the layer is visible. */
61     private boolean visible;
62     
63     /** Storage for registered listeners. */
64     private transient EventListenerList JavaDoc listenerList;
65
66     /**
67      * Creates a new instance.
68      */

69     protected AbstractDialLayer() {
70         this.visible = true;
71         this.listenerList = new EventListenerList JavaDoc();
72     }
73     
74     /**
75      * Returns <code>true</code> if this layer is visible (should be displayed),
76      * and <code>false</code> otherwise.
77      *
78      * @return A boolean.
79      */

80     public boolean isVisible() {
81         return this.visible;
82     }
83     
84     /**
85      * Sets the flag that determines whether or not this layer is drawn by
86      * the plot, and sends a {@link DialLayerChangeEvent} to all registered
87      * listeners.
88      *
89      * @param visible the flag.
90      */

91     public void setVisible(boolean visible) {
92         this.visible = visible;
93         notifyListeners(new DialLayerChangeEvent(this));
94     }
95     
96     /**
97      * Registers an object for notification of changes to the dial layer.
98      *
99      * @param listener the object that is being registered.
100      *
101      * @see #removeChangeListener(DialLayerChangeListener)
102      */

103     public void addChangeListener(DialLayerChangeListener listener) {
104         this.listenerList.add(DialLayerChangeListener.class, listener);
105     }
106
107     /**
108      * Deregisters an object for notification of changes to the dial layer.
109      *
110      * @param listener the object to deregister.
111      *
112      * @see #addChangeListener(DialLayerChangeListener)
113      */

114     public void removeChangeListener(DialLayerChangeListener listener) {
115         this.listenerList.remove(DialLayerChangeListener.class, listener);
116     }
117
118     /**
119      * Returns <code>true</code> if the specified object is registered with
120      * the dataset as a listener. Most applications won't need to call this
121      * method, it exists mainly for use by unit testing code.
122      *
123      * @param listener the listener.
124      *
125      * @return A boolean.
126      */

127     public boolean hasListener(EventListener JavaDoc listener) {
128         List JavaDoc list = Arrays.asList(this.listenerList.getListenerList());
129         return list.contains(listener);
130     }
131     
132     /**
133      * Notifies all registered listeners that the dial layer has changed.
134      * The {@link DialLayerChangeEvent} provides information about the change.
135      *
136      * @param event information about the change to the axis.
137      */

138     protected void notifyListeners(DialLayerChangeEvent event) {
139         Object JavaDoc[] listeners = this.listenerList.getListenerList();
140         for (int i = listeners.length - 2; i >= 0; i -= 2) {
141             if (listeners[i] == DialLayerChangeListener.class) {
142                 ((DialLayerChangeListener) listeners[i + 1]).dialLayerChanged(
143                         event);
144             }
145         }
146     }
147     
148     /**
149      * Provides serialization support.
150      *
151      * @param stream the input stream.
152      */

153     private void readObject(ObjectInputStream JavaDoc stream)
154         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
155         stream.defaultReadObject();
156         this.listenerList = new EventListenerList JavaDoc();
157     }
158     
159 }
160
Popular Tags