EventBroadcaster

Kind of class:public class
Package:com.bourre.events
Inherits from:none
Implements:
Known subclasses:
Author:Francis Bourre
Classpath:com.bourre.events.EventBroadcaster
File last modified:Monday, 24 November 2008, 11:36:53
The EventBroadcaster class is the cornerstone of
the Lowra event system. The main reason which explains the presence
of this object whereas the existence of the native EventDispatcherclass is the lack of flexibility of that class :
  • It's not possible to add an object as listener for many events in a single call.
  • It's not possible to force the presence of specific function in a listener object.
  • It's not possible to reuse an event previously dispatched without having to create
    a custom event object which overrides the clone function.

With the EventBroadcaster class it's possible to work with many kinds
of listeners, as listed below :

  • Method closure, or delegate method. You simply have to pass a reference to the
    function in function for register/unregister listeners, then that method is called
    with the event object as argument.
  • Objects implementing specific listener interface (as with Swing in java), in that
    case the function with the same name that the event type is called with the event
    object as argument.
  • An object which implement the handleEvent function, in that case,
    if the object doesn't have a function with a name corresponding to the event type
    the handleEvent function is called with the event object as argument.

The Lowra's event broadcaster also offer many methods to dispatch event from
different type of events object. More formally, it's possible to dispatch
an event object, or an anonymous object which will be converted into a

DynBasicEvent before the broadcast, all of its properties are
then copied into this event object.

Another big difference with the native dispatcher is the fact that listeners
are systematically stored using weak references, restricting the usage of
anonymous listeners which can't be unregistered due to the lack of reference
to it.

Composition is privileged over inheritance by the use of a source object
parameter for the broadcaster. That property, if set, result in that all
event objects dispatched by the EventBroadcaster which have
their target set to null will have that object
as source instead of the dispatcher itself.

Optionally the EventBroadcaster can restrict the type
of listeners to a specific type, in that case the broadcaster only
accept the registration of objects which implements or extends the
specified Class.

Summary


Constructor
  • EventBroadcaster (source:Object = null, type:Class = null)
    • Creates an new EventBroadcaster object with the passed-in
Class methods
Instance methods
  • hasListenerCollection (type:String) : Boolean
    • Returns true if this event broadcaster has
  • setListenerType (type:Class) : void
    • Defines the type of listeners this event broadcaster support.
  • getListenerCollection (type:String = null) : Collection
    • Returns a Collection view of listeners for the passed-in
  • removeListenerCollection (type:String) : void
    • Removes the complete collection of listeners for a specified
  • isRegistered (listener:Object, type:String = null) : Boolean
    • Returns true if the passed-in listener object is registered
  • addListener (listener:Object) : Boolean
    • Adds the passed-in listener as listener for all events dispatched
  • removeListener (listener:Object) : Boolean
    • Removes the passed-in listener object from this event
  • addEventListener (type:String, listener:Object, rest) : Boolean
    • Adds an event listener for the specified event type.
  • removeEventListener (type:String, listener:Object) : Boolean
    • Removes the passed-in listener for listening the specified event.
  • removeAllListeners : void
    • Removes all listeners registered in this event broadcaster.
  • isEmpty : Boolean
    • Returns true if this event broadcaster contains
  • dispatchEvent (o:Object) : void
    • Broadcast an event using an anonymous object.
  • broadcastEvent (e:Event) : void
    • Broadcast the passed-in event object to listeners
  • toString : String
    • Returns the String representation of

Constructor

EventBroadcaster

public function EventBroadcaster (
source:Object = null, type:Class = null)

Creates an new EventBroadcaster object with the passed-in

source object as source for events. If the sourceparameter is omitted the source of events will be this event broadcaster.

Optionnaly the type of listeners objects can be restricted, in that
case you just have to pass the class of listener in the typeparameter.

Parameters:
source:
an object used as target instead of this event broadcaster
for event object which have a null target
type :
an optional class for listener

Class methods

getInstance

public static function getInstance (

Returns a static instance of the class.

The presence of a getInstance method in the

EventBroadcaster class doesn't mean that the class
is a singleton, it's simply a convenient way to let application
developers broadcasting events that correspond to user gestures
and requests over the whole application.

Returns:
  • a static instance of the class

Instance methods

addEventListener

public function addEventListener (
type:String, listener:Object, rest) : Boolean

Adds an event listener for the specified event type.
There is two behaviors for the addEventListenerfunction :
  1. The passed-in listener is an object :
    The object is added as listener only for the specified event, the object must
    have a function with the same name than type or at least a

    handleEvent function.
  2. The passed-in listener is a function :
    A Delegate object is created and then
    added as listener for the event type. There is no restriction on the name of
    the function. If the rest is not empty, all elements in it is
    used as additional arguments into the delegate object.
Parameters:
type :
name of the event for which register the listener
listener:
object or function which will receive this event
rest :
additional arguments for the function listener
Returns:
  • true if the function have been succesfully added as
    listener fot the passed-in event
Throws:
  • {VISDOC_LINK_0}UnsupportedOperationException — If the listener is an object
    which have neither a function with the same name than the event type nor
    a function called handleEvent

addListener

public function addListener (
listener:Object) : Boolean

Adds the passed-in listener as listener for all events dispatched
by this event broadcaster. The function returns trueif the listener have been added at the end of the call. If the
listener is already registered in this event broadcaster the function
returns false.

Note : The addListener function doesn't accept functions
as listener, functions could only register for a single event.

Parameters:
listener:
the listener object to add as global listener
Returns:
  • true if the listener have been added during this call
Throws:
  • {VISDOC_LINK_0}IllegalArgumentException — If the passed-in listener
    listener doesn't match the listener type supported by this event
    broadcaster
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in listener
    is a function

broadcastEvent

public function broadcastEvent (
e:Event) : void

Broadcast the passed-in event object to listeners
according to the event's type. The event is broadcasted
to both listeners registered specifically for this event
type and global listeners in the broadcaster.

If the target property of the passed-in event
is null, it will be set using the value of the
source property of this event broadcaster.

Parameters:
e:
event object to broadcast
Throws:
  • {VISDOC_LINK_0}UnsupportedOperationException — If one listener is an object
    which have neither a function with the same name than the event type nor
    a function called handleEvent

dispatchEvent

public function dispatchEvent (
o:Object) : void

Broadcast an event using an anonymous object.
The only requirement for objects passed as argument in
this function is that they must have a typeproperty. The target property will be set
with this event broadcaster's source if there is no source
specified.

The concret event object broadcasted to listener is a

DynBasicEvent decorated with the property
of the passed-in object.

Parameters:
o:
an anonymous object used to decorate a
DynBasicEvent
Throws:
  • {VISDOC_LINK_0}UnsupportedOperationException — If one listener is an object
    which have neither a function with the same name than the event type nor
    a function called handleEvent

getListenerCollection

public function getListenerCollection (
type:String = null) : Collection

Returns a Collection view of listeners for the passed-in
event type. The returned collection is a reference to the internal collection
of this event broadcaster, resulting that there's no guarantee that collection
cannot be altered by another object. If the type parameter is
omitted, the function returns the collection of global listeners objects (all
objects that haven't register for a specific event).
Parameters:
type:
the event name for which get a collection, if not
defined, the collection of global listeners is returned
Returns:
  • Collection of listeners corresponding to the
    passed-in event type

hasListenerCollection

public function hasListenerCollection (
type:String) : Boolean

Returns true if this event broadcaster has
listeners for the passed-in event type.
Parameters:
type:
name of the event for which look for listener
Returns:
  • true if this event broadcaster has
    listeners for the passed-in event type

isEmpty

public function isEmpty (
) : Boolean

Returns true if this event broadcaster contains
no listeners for any event.
Returns:
  • true if this event broadcaster contains
    no listeners for any event.

isRegistered

public function isRegistered (
listener:Object, type:String = null) : Boolean

Returns true if the passed-in listener object is registered
as listener for the passed-in event type. If the type parameter
is omitted, the function returns true only if the listener is
registered as global listener.

Note : the listener could be either an object or a function.

Parameters:
listener:
object to look for registration
type :
event type to look at
Returns:
  • true if the passed-in listener should receive notification
    of the passed-in event type

removeAllListeners

public function removeAllListeners (
) : void

Removes all listeners registered in this event broadcaster.

removeEventListener

public function removeEventListener (
type:String, listener:Object) : Boolean

Removes the passed-in listener for listening the specified event. The
listener could be either an object or a function.
Parameters:
type :
name of the event for which unregister the listener
listener:
object or function to be unregistered
Returns:
  • true if the listener have been successfully removed
    as listener for the passed-in event

removeListener

public function removeListener (
listener:Object) : Boolean

Removes the passed-in listener object from this event
broadcaster. The object is removed as listener for all
events the broadcaster may dispatch.
Parameters:
listener:
the listener object to remove from
this event broadcaster object
Returns:
  • true if the object have been successfully
    removed from this event broadcaster
Throws:
  • {VISDOC_LINK_0}IllegalArgumentException — If the passed-in listener
    is a function

removeListenerCollection

public function removeListenerCollection (
type:String) : void

Removes the complete collection of listeners for a specified
event type.
Parameters:
type:
the event type for which remove all listeners

setListenerType

public function setListenerType (
type:Class) : void

Defines the type of listeners this event broadcaster support.
Functions are not concerned by this restriction.
Parameters:
type:
the type of the listener this event broadcaster support
Throws:
  • {VISDOC_LINK_0}IllegalArgumentException — If one of the listener
    already contained in this event broadcaster doesn't match the
    passed-ni type

toString

public function toString (
) : String

Returns the String representation of
this object.

The function return a string like

com.bourre.events::EventBroadcaster<ListenerType>for a typed broadcaster. The string between the <
and > is the name of the type of the broadcaster's
global listeners. If the broadcaster is an untyped broadcaster
the function will simply return the result of the

PixlibStringifier.stringify call.

Returns:
  • String representation of
    this object.