org.netbeans.modules.editor.util/1 1.26

org.netbeans.lib.editor.util
Class PriorityListenerList<T extends EventListener>

java.lang.Object
  extended by org.netbeans.lib.editor.util.PriorityListenerList<T>
All Implemented Interfaces:
Serializable

public class PriorityListenerList<T extends EventListener>
extends Object
implements Serializable

Listener list that layers the maintained listeners according to the given priority index.
Simply said it's an array of listener arrays. The priority index defines the event listeners array holding all the listeners with the given priority index.

Since:
1.4
See Also:
Serialized Form

Constructor Summary
PriorityListenerList()
           
 
Method Summary
 void add(T listener, int priority)
          Add listener with the given priority.
 int getListenerCount()
          Get total count of listeners contained in this list at all priority levels.
 T[][] getListenersArray()
          Return the actual array of listeners arrays maintained by this listeners list.
 void remove(T listener, int priority)
          Remove listener with the given priority index.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PriorityListenerList

public PriorityListenerList()
Method Detail

add

public void add(T listener,
                int priority)
Add listener with the given priority.

Parameters:
listener - listener to be added. If null is passed it is ignored (nothing gets added).
priority - >=0 index defining priority with which the listener should be fired.
The higher the priority the sooner the listener will be fired.
It's guaranteed that all the listeners with higher priority index will be fired sooner than listeners with lower priority.
The number of priority levels should be limited to reasonably low number.
Throws:
IndexOutOfBoundsException - when priority < 0

remove

public void remove(T listener,
                   int priority)
Remove listener with the given priority index.

Parameters:
listener - listener to be removed. If null is passed it is ignored (nothing gets removed).
priority - >=0 index defining priority with which the listener was originally added.
If the listener was not added or it was added with different priority then no action happens.
Throws:
IndexOutOfBoundsException - when priority < 0

getListenersArray

public T[][] getListenersArray()
Return the actual array of listeners arrays maintained by this listeners list.
WARNING! Absolutely NO modification should be done on the contents of the returned data.

The higher index means sooner firing. Listeners with the same priority are ordered so that the one added sooner has higher index than the one added later. So the following firing mechanism should be used:

  private void fireMyEvent(MyEvent evt) {
      MyListener[][] listenersArray = priorityListenerList.getListenersArray();
      for (int priority = listenersArray.length - 1; priority >= 0; priority--) {
          MyListener[] listeners = listenersArray[priority];
          for (int i = listeners.length - 1; i >= 0; i--) {
              listeners[i].notify(evt);
          }
      } 
  }
 


getListenerCount

public int getListenerCount()
Get total count of listeners contained in this list at all priority levels.


org.netbeans.modules.editor.util/1 1.26

Built on December 3 2008.  |  Portions Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.