/*
PureMVC - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved.
Your reuse is governed by the Creative Commons Attribution 3.0 United States License
*/
package org.puremvc.as3.core
{
import org.puremvc.as3.core.*;
import org.puremvc.as3.interfaces.*;
import org.puremvc.as3.patterns.observer.*;
/**
* A Singleton IController
implementation.
*
*
* In PureMVC, the Controller
class follows the
* 'Command and Controller' strategy, and assumes these
* responsibilities:
*
ICommand
s
* are intended to handle which INotifications
.IObserver
with
* the View
for each INotification
* that it has an ICommand
mapping for.ICommand
* to handle a given INotification
when notified by the View
.ICommand
's execute
* method, passing in the INotification
.
* Your application must register ICommands
with the
* Controller.
*
* The simplest way is to subclass Facade,
* and use its initializeController
method to add your
* registrations.
*
* @see org.puremvc.as3.core.view.View View
* @see org.puremvc.as3.patterns.observer.Observer Observer
* @see org.puremvc.as3.patterns.observer.Notification Notification
* @see org.puremvc.as3.patterns.command.SimpleCommand SimpleCommand
* @see org.puremvc.as3.patterns.command.MacroCommand MacroCommand
*/
public class Controller implements IController
{
/**
* Constructor.
*
*
* This IController
implementation is a Singleton,
* so you should not call the constructor
* directly, but instead call the static Singleton
* Factory method Controller.getInstance()
*
* @throws Error Error if Singleton instance has already been constructed
*
*/
public function Controller( )
{
if (instance != null) throw Error(SINGLETON_MSG);
instance = this;
commandMap = new Array();
initializeController();
}
/**
* Initialize the Singleton Controller
instance.
*
*
Called automatically by the constructor.
* *Note that if you are using a subclass of View
* in your application, you should also subclass Controller
* and override the initializeController
method in the
* following way:
Controller
Singleton Factory method.
*
* @return the Singleton instance of Controller
*/
public static function getInstance() : IController
{
if ( instance == null ) instance = new Controller( );
return instance;
}
/**
* If an ICommand
has previously been registered
* to handle a the given INotification
, then it is executed.
*
* @param note an INotification
*/
public function executeCommand( note : INotification ) : void
{
var commandClassRef : Class = commandMap[ note.getName() ];
if ( commandClassRef == null ) return;
var commandInstance : ICommand = new commandClassRef();
commandInstance.execute( note );
}
/**
* Register a particular ICommand
class as the handler
* for a particular INotification
.
*
*
* If an ICommand
has already been registered to
* handle INotification
s with this name, it is no longer
* used, the new ICommand
is used instead.
INotification
* @param commandClassRef the Class
of the ICommand
*/
public function registerCommand( notificationName : String, commandClassRef : Class ) : void
{
if ( commandMap[ notificationName ] == null ) {
view.registerObserver( notificationName, new Observer( executeCommand, this ) );
}
commandMap[ notificationName ] = commandClassRef;
}
/**
* Check if a Command is registered for a given Notification
*
* @param notificationName
* @return whether a Command is currently registered for the given notificationName
.
*/
public function hasCommand( notificationName:String ) : Boolean
{
return commandMap[ notificationName ] != null;
}
/**
* Remove a previously registered ICommand
to INotification
mapping.
*
* @param notificationName the name of the INotification
to remove the ICommand
mapping for
*/
public function removeCommand( notificationName : String ) : void
{
// if the Command is registered...
if ( hasCommand( notificationName ) )
{
// remove the observer
view.removeObserver( notificationName, this );
// remove the command
commandMap[ notificationName ] = null;
}
}
// Local reference to View
protected var view : IView;
// Mapping of Notification names to Command Class references
protected var commandMap : Array;
// Singleton instance
protected static var instance : IController;
// Message Constants
protected const SINGLETON_MSG : String = "Controller Singleton already constructed!";
}
}