public class EventDispatcher

  1. Object
  2. EventDispatcher

Handles event dispatching while guaranteeing that all events would be fired properly on the EDT regardless of their source. This class handles listener registration/removal in a safe and uniform way.

To integrate this into your code you can use something like:

private final EventDispatcher listeners = new EventDispatcher();

public void addActionListener(ActionListener a) {
    listeners.addListener(a);
}
public void removeActionListener(ActionListener a) {
    listeners.removeListener(a);
}
private void fireEvent(ActionEvent ev) {
    listeners.fireActionEvent(ev);
}

Constructors

public EventDispatcher()

Methods

public static void setFireStyleEventsOnNonEDT(boolean fire)When set to true, style events will be dispatched even from non-EDT threads.
public synchronized void addListener(Object listener)Add a listener to the dispatcher that would receive the events when they occurs
public Vector getListenerVector()Deprecated Returns the vector of the listeners
public Collection getListenerCollection()Returns the collection of the listeners
public synchronized void removeListener(Object listener)Remove the listener from the dispatcher
public void fireDataChangeEvent(int index, int type)Fires the event safely on the EDT without risk of concurrency errors
public void fireBindTargetChange(Component source, String propertyName, Object oldValue, Object newValue)Fired when a property of the component changes to a new value
public void fireStyleChangeEvent(String property, Style source)Fires the style change even to the listeners
public void fireActionEvent(ActionEvent ev)Fires the event safely on the EDT without risk of concurrency errors
public void fireSelectionEvent(int oldSelection, int newSelection)Fires the event safely on the EDT without risk of concurrency errors
public void fireScrollEvent(int scrollX, int scrollY, int oldscrollX, int oldscrollY)Fires the event safely on the EDT without risk of concurrency errors
public void fireFocus(Component c)Fires the event safely on the EDT without risk of concurrency errors
public boolean hasListeners()Returns true if the event dispatcher has registered listeners
public boolean isBlocking()Indicates whether this dispatcher blocks when firing events or not, normally a dispatcher uses callSeriallyAndWait() to be 100% synchronous with event delivery however this method is very slow.
public void setBlocking(boolean blocking)Indicates whether this dispatcher blocks when firing events or not, normally a dispatcher uses callSeriallyAndWait() to be 100% synchronous with event delivery however this method is very slow.

Inherited methods

Constructor details

EventDispatcher

public EventDispatcher()

Method details

setFireStyleEventsOnNonEDT

public static void setFireStyleEventsOnNonEDT(boolean fire)

When set to true, style events will be dispatched even from non-EDT threads. When set to false, when in non-EDT threads, style events will not be dispatched at all (And developer has to make sure changes will be reflected by calling revalidate after all the changes)

Default is false. Setting this to true results in a performance penalty, and it is better instead to simply aggregate events performed on non-EDT threads and when all are over - call revalidate on the relevant container.

Parameters

fire boolean
true to fire on non-EDT, false otherwise

addListener

public synchronized void addListener(Object listener)
Add a listener to the dispatcher that would receive the events when they occurs

Parameters

listener Object
a dispatcher listener to add

getListenerVector

public Vector getListenerVector()
Deprecated. use getListenerCollection instead, this method will now be VERY SLOW
Returns the vector of the listeners

Returns

the vector of listeners attached to the event dispatcher

getListenerCollection

public Collection getListenerCollection()
Returns the collection of the listeners

Returns

the collection of listeners attached to the event dispatcher

removeListener

public synchronized void removeListener(Object listener)
Remove the listener from the dispatcher

Parameters

listener Object
a dispatcher listener to remove

fireDataChangeEvent

public void fireDataChangeEvent(int index, int type)
Fires the event safely on the EDT without risk of concurrency errors

Parameters

index int
the index of the event
type int
the type of the event

fireBindTargetChange

public void fireBindTargetChange(Component source, String propertyName, Object oldValue, Object newValue)
Fired when a property of the component changes to a new value

Parameters

source Component
the source component
propertyName String
the name of the property
oldValue Object
the old value of the property
newValue Object
the new value for the property

fireStyleChangeEvent

public void fireStyleChangeEvent(String property, Style source)
Fires the style change even to the listeners

Parameters

property String
the property name for the event
source Style
the style firing the event

fireActionEvent

public void fireActionEvent(ActionEvent ev)
Fires the event safely on the EDT without risk of concurrency errors

Parameters

ev ActionEvent
the ActionEvent to fire to the listeners

fireSelectionEvent

public void fireSelectionEvent(int oldSelection, int newSelection)
Fires the event safely on the EDT without risk of concurrency errors

Parameters

oldSelection int
old selection
newSelection int
new selection

fireScrollEvent

public void fireScrollEvent(int scrollX, int scrollY, int oldscrollX, int oldscrollY)
Fires the event safely on the EDT without risk of concurrency errors

fireFocus

public void fireFocus(Component c)
Fires the event safely on the EDT without risk of concurrency errors

Parameters

c Component
the Component that gets the focus event

hasListeners

public boolean hasListeners()
Returns true if the event dispatcher has registered listeners

Returns

true if the event dispatcher has registered listeners

isBlocking

public boolean isBlocking()
Indicates whether this dispatcher blocks when firing events or not, normally a dispatcher uses callSeriallyAndWait() to be 100% synchronous with event delivery however this method is very slow. By setting blocking to false the callSerially method is used which allows much faster execution for IO heavy operations.

Returns

the blocking state

setBlocking

public void setBlocking(boolean blocking)
Indicates whether this dispatcher blocks when firing events or not, normally a dispatcher uses callSeriallyAndWait() to be 100% synchronous with event delivery however this method is very slow. By setting blocking to false the callSerially method is used which allows much faster execution for IO heavy operations.

Parameters

blocking boolean
the blocking value