/* 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.interfaces.*; import org.puremvc.as3.patterns.observer.Observer; /** * A Singleton IView implementation. * *

* In PureMVC, the View class assumes these responsibilities: *

* * @see org.puremvc.as3.patterns.mediator.Mediator Mediator * @see org.puremvc.as3.patterns.observer.Observer Observer * @see org.puremvc.as3.patterns.observer.Notification Notification */ public class View implements IView { /** * Constructor. * *

* This IView implementation is a Singleton, * so you should not call the constructor * directly, but instead call the static Singleton * Factory method View.getInstance() * * @throws Error Error if Singleton instance has already been constructed * */ public function View( ) { if (instance != null) throw Error(SINGLETON_MSG); instance = this; mediatorMap = new Array(); observerMap = new Array(); initializeView(); } /** * Initialize the Singleton View instance. * *

* Called automatically by the constructor, this * is your opportunity to initialize the Singleton * instance in your subclass without overriding the * constructor.

* * @return void */ protected function initializeView( ) : void { } /** * View Singleton Factory method. * * @return the Singleton instance of View */ public static function getInstance() : IView { if ( instance == null ) instance = new View( ); return instance; } /** * Register an IObserver to be notified * of INotifications with a given name. * * @param notificationName the name of the INotifications to notify this IObserver of * @param observer the IObserver to register */ public function registerObserver ( notificationName:String, observer:IObserver ) : void { var observers:Array = observerMap[ notificationName ]; if( observers ) { observers.push( observer ); } else { observerMap[ notificationName ] = [ observer ]; } } /** * Notify the IObservers for a particular INotification. * *

* All previously attached IObservers for this INotification's * list are notified and are passed a reference to the INotification in * the order in which they were registered.

* * @param notification the INotification to notify IObservers of. */ public function notifyObservers( notification:INotification ) : void { if( observerMap[ notification.getName() ] != null ) { // Get a reference to the observers list for this notification name var observers_ref:Array = observerMap[ notification.getName() ] as Array; // Copy observers from reference array to working array, // since the reference array may change during the notification loop var observers:Array = new Array(); var observer:IObserver; for (var i:Number = 0; i < observers_ref.length; i++) { observer = observers_ref[ i ] as IObserver; observers.push( observer ); } // Notify Observers from the working array for (i = 0; i < observers.length; i++) { observer = observers[ i ] as IObserver; observer.notifyObserver( notification ); } } } /** * Remove the observer for a given notifyContext from an observer list for a given Notification name. *

* @param notificationName which observer list to remove from * @param notifyContext remove the observer with this object as its notifyContext */ public function removeObserver( notificationName:String, notifyContext:Object ):void { // the observer list for the notification under inspection var observers:Array = observerMap[ notificationName ] as Array; // find the observer for the notifyContext for ( var i:int=0; iIMediator instance with the View. * *

* Registers the IMediator so that it can be retrieved by name, * and further interrogates the IMediator for its * INotification interests.

*

* If the IMediator returns any INotification * names to be notified about, an Observer is created encapsulating * the IMediator instance's handleNotification method * and registering it as an Observer for all INotifications the * IMediator is interested in.

* * @param mediatorName the name to associate with this IMediator instance * @param mediator a reference to the IMediator instance */ public function registerMediator( mediator:IMediator ) : void { // do not allow re-registration (you must to removeMediator fist) if ( mediatorMap[ mediator.getMediatorName() ] != null ) return; // Register the Mediator for retrieval by name mediatorMap[ mediator.getMediatorName() ] = mediator; // Get Notification interests, if any. var interests:Array = mediator.listNotificationInterests(); // Register Mediator as an observer for each of its notification interests if ( interests.length > 0 ) { // Create Observer referencing this mediator's handlNotification method var observer:Observer = new Observer( mediator.handleNotification, mediator ); // Register Mediator as Observer for its list of Notification interests for ( var i:Number=0; iIMediator from the View. * * @param mediatorName the name of the IMediator instance to retrieve. * @return the IMediator instance previously registered with the given mediatorName. */ public function retrieveMediator( mediatorName:String ) : IMediator { return mediatorMap[ mediatorName ]; } /** * Remove an IMediator from the View. * * @param mediatorName name of the IMediator instance to be removed. * @return the IMediator that was removed from the View */ public function removeMediator( mediatorName:String ) : IMediator { // Retrieve the named mediator var mediator:IMediator = mediatorMap[ mediatorName ] as IMediator; if ( mediator ) { // for every notification this mediator is interested in... var interests:Array = mediator.listNotificationInterests(); for ( var i:Number=0; imediatorName. */ public function hasMediator( mediatorName:String ) : Boolean { return mediatorMap[ mediatorName ] != null; } // Mapping of Mediator names to Mediator instances protected var mediatorMap : Array; // Mapping of Notification names to Observer lists protected var observerMap : Array; // Singleton instance protected static var instance : IView; // Message Constants protected const SINGLETON_MSG : String = "View Singleton already constructed!"; } }