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 |
EventBroadcaster class is the cornerstone ofthe 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 theclonefunction.
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
handleEventfunction, in that case,
if the object doesn't have a function with a name corresponding to the event type
thehandleEventfunction 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
- EventBroadcaster (source:Object = null, type:Class = null)
- Creates an new EventBroadcaster object with the passed-in
- getInstance : EventBroadcaster
- Returns a static instance of the class.
- 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
EventBroadcaster object with the passed-insource 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.
for event object which have a
null targetClass methods
getInstance
The presence of a getInstance method in theEventBroadcaster 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.
- a static instance of the class
Instance methods
addEventListener
There is two behaviors for the
addEventListenerfunction :- 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 thantypeor at least ahandleEventfunction. - The passed-in listener is a function :
ADelegateobject is created and then
added as listener for the event type. There is no restriction on the name of
the function. If therestis not empty, all elements in it is
used as additional arguments into the delegate object.
-
trueif the function have been succesfully added as
listener fot the passed-in event
- {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 calledhandleEvent
addListener
by this event broadcaster. The function returns
trueif the listener have been added at the end of the call. If thelistener 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.
-
trueif the listener have been added during this call
- {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
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.
- {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 calledhandleEvent
dispatchEvent
The only requirement for objects passed as argument in
this function is that they must have a
typeproperty. The target property will be setwith this event broadcaster's source if there is no source
specified.
The concret event object broadcasted to listener is aDynBasicEvent decorated with the property
of the passed-in object.
DynBasicEvent- {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 calledhandleEvent
getListenerCollection
Collection view of listeners for the passed-inevent 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 isomitted, the function returns the collection of global listeners objects (all
objects that haven't register for a specific event).
defined, the collection of global listeners is returned
-
Collectionof listeners corresponding to the
passed-in event type
hasListenerCollection
true if this event broadcaster has listeners for the passed-in event type.
-
trueif this event broadcaster has
listeners for the passed-in event type
isEmpty
true if this event broadcaster containsno listeners for any event.
-
trueif this event broadcaster contains
no listeners for any event.
isRegistered
true if the passed-in listener object is registeredas listener for the passed-in event type. If the
type parameteris 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.
-
trueif the passed-in listener should receive notification
of the passed-in event type
removeAllListeners
removeEventListener
listener could be either an object or a function.
-
trueif the listener have been successfully removed
as listener for the passed-in event
removeListener
broadcaster. The object is removed as listener for all
events the broadcaster may dispatch.
this event broadcaster object
-
trueif the object have been successfully
removed from this event broadcaster
- {VISDOC_LINK_0}
IllegalArgumentException— If the passed-in listener
is a function
removeListenerCollection
event type.
setListenerType
Functions are not concerned by this restriction.
- {VISDOC_LINK_0}
IllegalArgumentException— If one of the listener
already contained in this event broadcaster doesn't match the
passed-ni type
toString
String representation ofthis object.
The function return a string likecom.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 thePixlibStringifier.stringify call.
-
Stringrepresentation of
this object.