Class 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);
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(Object listener) Add a listener to the dispatcher that would receive the events when they occursvoidFires the event safely on the EDT without risk of concurrency errorsvoidfireBindTargetChange(Component source, String propertyName, Object oldValue, Object newValue) Fired when a property of the component changes to a new valuevoidfireDataChangeEvent(int index, int type) Fires the event safely on the EDT without risk of concurrency errorsvoidFires the event safely on the EDT without risk of concurrency errorsvoidfireScrollEvent(int scrollX, int scrollY, int oldscrollX, int oldscrollY) Fires the event safely on the EDT without risk of concurrency errorsvoidfireSelectionEvent(int oldSelection, int newSelection) Fires the event safely on the EDT without risk of concurrency errorsvoidfireStyleChangeEvent(String property, Style source) Fires the style change even to the listenersReturns the collection of the listenersReturns the vector of the listenersbooleanReturns true if the event dispatcher has registered listenersbooleanIndicates 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.voidremoveListener(Object listener) Remove the listener from the dispatchervoidsetBlocking(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.static voidsetFireStyleEventsOnNonEDT(boolean fire) When set to true, style events will be dispatched even from non-EDT threads.
-
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: true to fire on non-EDT, false otherwise
-
addListener
Add a listener to the dispatcher that would receive the events when they occurs
Parameters
listener: a dispatcher listener to add
-
getListenerVector
Returns the vector of the listeners
Returns
the vector of listeners attached to the event dispatcher
Deprecated
use getListenerCollection instead, this method will now be VERY SLOW
-
getListenerCollection
Returns the collection of the listeners
Returns
the collection of listeners attached to the event dispatcher
-
removeListener
Remove the listener from the dispatcher
Parameters
listener: 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: the index of the event -
type: 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: the source component -
propertyName: the name of the property -
oldValue: the old value of the property -
newValue: the new value for the property
-
-
fireStyleChangeEvent
-
fireActionEvent
Fires the event safely on the EDT without risk of concurrency errors
Parameters
ev: 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: old selection -
newSelection: 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
Fires the event safely on the EDT without risk of concurrency errors
Parameters
c: 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: the blocking value
-