Created
August 24, 2010 15:17
-
-
Save bclinkinbeard/547716 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.swizframework.extensions.commands | |
{ | |
import flash.events.Event; | |
import flash.utils.Dictionary; | |
import org.swizframework.core.Bean; | |
import org.swizframework.core.ISwiz; | |
import org.swizframework.core.ISwizAware; | |
import org.swizframework.core.Prototype; | |
public class CommandController implements ISwizAware | |
{ | |
protected var _swiz:ISwiz; | |
public function set swiz( swiz:ISwiz ):void | |
{ | |
_swiz = swiz; | |
} | |
protected var commandMap:Dictionary = new Dictionary(); | |
public function CommandController() | |
{ | |
} | |
public function mapCommand( eventType:String, commandClass:Class, eventClass:Class = null, oneTime:Boolean = false ):void | |
{ | |
if( commandMap[ eventType ] != null ) | |
throw new Error( "Duplicate mappings are not allowed." ); | |
commandMap[ eventType ] = new CommandMapping( eventType, commandClass, eventClass, oneTime ); | |
_swiz.dispatcher.addEventListener( eventType, catchEvents, false, 0, true ); | |
} | |
public function catchEvents( event:Event ):void | |
{ | |
if( commandMap[ event.type ] != null ) | |
{ | |
var commandMapping:CommandMapping = CommandMapping( commandMap[ event.type ] ); | |
if( !( event is commandMapping.eventClass ) ) | |
return; | |
var commandPrototype:Bean = _swiz.beanFactory.getBeanByType( commandMapping.commandClass ); | |
if( commandPrototype == null ) | |
throw new Error( "Command bean not found for mapped event type." ); | |
if( commandPrototype is Prototype ) | |
{ | |
var command:Object = Prototype( commandPrototype ).source; | |
if( command is IEventAwareCommand ) | |
IEventAwareCommand( command ).event = event; | |
if( command is ICommand ) | |
ICommand( command ).execute(); | |
} | |
else | |
{ | |
throw new Error( "Commands must be provided as Prototype beans." ); | |
} | |
if( commandMapping.oneTime ) | |
delete commandMap[ commandMapping.eventType ]; | |
} | |
} | |
} | |
} | |
import flash.events.Event; | |
class CommandMapping | |
{ | |
public var eventType:String; | |
public var commandClass:Class; | |
public var eventClass:Class; | |
public var oneTime:Boolean; | |
public function CommandMapping( eventType:String, commandClass:Class, eventClass:Class = null, oneTime:Boolean = false ) | |
{ | |
this.eventType = eventType; | |
this.commandClass = commandClass; | |
this.eventClass = eventClass || Event; | |
this.oneTime = oneTime; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mapCommand( DocumentTypesEvent.LOAD_DOCUMENT_TYPES, LoadDocumentTypesCommand, DocumentTypesEvent, true ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment