javax.swing.event

Class EventListenerList

Implemented Interfaces:
Serializable

public class EventListenerList
extends Object
implements Serializable

A utility class for keeping track of EventListeners.

Example for using this class:

 import java.util.EventListener;
 import javax.swing.event.EventListenerList;

 class Foo
 {
   protected final EventListenerList listeners = new EventListenerList();
   protected BarClosedEvent barClosedEvent = null;

   public void addBarListener(BarListener l)
   {
     listeners.add(BarListener.class, l);
   }

   public void removeBarListener(BarListener l)
   {
     listeners.remove(BarListener.class, l);
   }

   protected void fireBarClosedEvent()
   {
     Object[] l = listeners.getListenerList();

     for (int i = l.length - 2; i >= 0; i -= 2)
       if (l[i] == BarListener.class)
         {
           // Create the event on demand, when it is needed the first time.
           if (barClosedEvent == null)
             barClosedEvent = new BarClosedEvent(this);

           ((BarClosedListener) l[i + 1]).barClosed(barClosedEvent);
         }
   }
 }
See Also:
Serialized Form

Field Summary

protected Object[]
listenerList
An array with all currently registered listeners.

Constructor Summary

EventListenerList()
EventListenerList constructor

Method Summary

extends EventListener> T[] getListeners(Class c)
Retrieves the currently subscribed listeners of a particular type.
extends EventListener> void add(Class t, T listener)
Registers a listener of a specific type.
extends EventListener> void remove(Class t, T listener)
Removes a listener of a specific type.
int
getListenerCount()
Determines the number of listeners.
int
getListenerCount(Class t)
Determines the number of listeners of a particular class.
Object[]
getListenerList()
Returns an array containing a sequence of listenerType/listener pairs, one for each listener.
String
toString()
Returns a string representation of this object that may be useful for debugging purposes.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

listenerList

protected Object[] listenerList
An array with all currently registered listeners. The array has twice as many elements as there are listeners. For an even integer i, listenerList[i] indicates the registered class, and listenerList[i + 1] is the listener.

Constructor Details

EventListenerList

public EventListenerList()
EventListenerList constructor

Method Details

extends EventListener> T[] getListeners

public extends EventListener> T[] getListeners(Class c)
Retrieves the currently subscribed listeners of a particular type. For a listener to be returned, it must have been registered with exactly the type c; subclasses are not considered equal.

The returned array can always be cast to c[]. Since it is a newly allocated copy, the caller may arbitrarily modify the array.

Parameters:
c - the class which was passed to EventListenerList.
Returns:
an array of c whose elements are the currently subscribed listeners of the specified type. If there are no such listeners, an empty array is returned.
Throws:
ClassCastException - if c does not implement the EventListener interface.
NullPointerException - if c is null.
Since:
1.3

extends EventListener> void add

public extends EventListener> void add(Class t,
                                          T listener)
Registers a listener of a specific type.
Parameters:
t - the type of the listener.
listener - the listener to add, which must be an instance of t, or of a subclass of t.
Throws:
IllegalArgumentException - if listener is not an instance of t (or a subclass thereof).
NullPointerException - if t is null.

extends EventListener> void remove

public extends EventListener> void remove(Class t,
                                             T listener)
Removes a listener of a specific type.
Parameters:
t - the type of the listener.
listener - the listener to remove, which must be an instance of t, or of a subclass of t.
Throws:
IllegalArgumentException - if listener is not an instance of t (or a subclass thereof).
NullPointerException - if t is null.

getListenerCount

public int getListenerCount()
Determines the number of listeners.

getListenerCount

public int getListenerCount(Class t)
Determines the number of listeners of a particular class.
Parameters:
t - the type of listeners to be counted. In order to get counted, a subscribed listener must be exactly of class t. Thus, subclasses of t will not be counted.

getListenerList

public Object[] getListenerList()
Returns an array containing a sequence of listenerType/listener pairs, one for each listener.
Returns:
An array containing the listener types and references.

toString

public String toString()
Returns a string representation of this object that may be useful for debugging purposes.
Overrides:
toString in interface Object

EventListenerList.java -- Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.