dasher
Class CEventHandler

java.lang.Object
  extended by dasher.CEventHandler

public class CEventHandler
extends java.lang.Object

The EventHandler class is a fairly simple device whose responsibilities are to

a) Allow DasherComponents to register themselves as event listeners,

b) Accept events from DasherComponents and notify all other Components using their HandleEvent method, and

c) Allow Components to unregister themselves when they are being destroyed.

The mode of action is very simple indeed; we have a list of Listeners to which new listeners are added and old ones removed, and which we iterate through when notified of an event, notifying all other Components by calling their HandleEvent methods in turn.

There is however one special case: In the event that a listener tries to register itself whilst an event is being handled (for example, a parameter change caused a Component to be created which immediately tried to register itself), the component is added to a queue of listeners who are waiting to be registered.

This is because otherwise there would be some ambiguity as to when a given component would start receiving events, particularly as to whether it would receive that which was being handled when it registered itself.

The result is that new listeners will begin receiving events as soon as all those which were in progress when it registered have finished.

Because events are dispatched immediately and not added to a queue, there cannot be any certainty as to in what order a given component will hear about multiple events inserted without waiting for the first to end.

It is also possible to cause the program to enter a tight loop by creating a direct or indirect loop of dispatched events; Components should be careful to avoid causing this situation by avoiding creating events during their HandleEvent procedures which are likely to cause the same event to be re-raised unless they can be certain of stopping the loop at some future point.


Field Summary
protected  int m_iInHandler
          Integer indicating how many times we are 'in' the event handler (for example, we might be in twice if a component responded to an event by issuing an event of its own).
protected  CDasherInterfaceBase m_pInterface
          Interface to whom this Handler belongs, and which will have its InterfaceEventHandler method called for all events.
protected  java.util.ArrayList<CDasherComponent> m_vListenerQueue
          List of Components waiting to be added as listeners when we finish handling events.
protected  java.util.ArrayList<CDasherComponent> m_vListeners
          List of currently active listeners
 
Constructor Summary
CEventHandler(CDasherInterfaceBase pInterface)
          Creates a new EventHandler with no listeners and attached to a specified interface.
 
Method Summary
 void InsertEvent(CEvent Event)
          Informs all registered listeners of a specified Event.
 void RegisterListener(CDasherComponent pListener)
          Registers a given component as an event listener.
 void UnregisterListener(CDasherComponent pListener)
          Removes a given Component from the list of listeners.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_vListeners

protected java.util.ArrayList<CDasherComponent> m_vListeners
List of currently active listeners


m_vListenerQueue

protected java.util.ArrayList<CDasherComponent> m_vListenerQueue
List of Components waiting to be added as listeners when we finish handling events.


m_iInHandler

protected int m_iInHandler
Integer indicating how many times we are 'in' the event handler (for example, we might be in twice if a component responded to an event by issuing an event of its own).


m_pInterface

protected CDasherInterfaceBase m_pInterface
Interface to whom this Handler belongs, and which will have its InterfaceEventHandler method called for all events.

Constructor Detail

CEventHandler

public CEventHandler(CDasherInterfaceBase pInterface)
Creates a new EventHandler with no listeners and attached to a specified interface.

Parameters:
pInterface - Parent interface
Method Detail

InsertEvent

public void InsertEvent(CEvent Event)
Informs all registered listeners of a specified Event.

Before beginning, m_iInHandler is incremented to indicate that one event is currently in progress; when finished, it is decremented and, if zero, listeners which are queued up for registration will be added to the list of active listeners.

The last components to be notified of a given event will always be the parent Interface and then the external event handler (by way of the interface's ExternalEventHandler method.

Parameters:
Event - Event to dispatch to all registered listeners.

RegisterListener

public void RegisterListener(CDasherComponent pListener)
Registers a given component as an event listener.

In the event that one or more events are currently in progress, it will be added to a queue of pending listeners and will be added when InsertEvent finishes for the last time.

Parameters:
pListener - Component to add as a listener

UnregisterListener

public void UnregisterListener(CDasherComponent pListener)
Removes a given Component from the list of listeners.

This is necessary before a given Component can be garbage collected.

Parameters:
pListener - Component to remove