/*
* Copyright the original author or authors.
*
* Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.mozilla.org/MPL/MPL-1.1.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bourre.plugin
{
import com.bourre.commands.FrontController;
import com.bourre.error.IllegalArgumentException;
import com.bourre.events.ApplicationBroadcaster;
import com.bourre.events.Broadcaster;
import com.bourre.events.EventChannel;
import com.bourre.ioc.bean.BeanFactory;
import com.bourre.log.PixlibStringifier;
import com.bourre.model.AbstractModel;
import com.bourre.model.ModelLocator;
import com.bourre.view.AbstractView;
import com.bourre.view.ViewLocator;
import flash.events.Event;
/**
* Dispatched when plugin is initialized.
*
* @eventType com.bourre.plugin.PluginEvent.onInitPluginEVENT
*/
[Event(name="onInitPlugin", type="com.bourre.plugin.PluginEvent")]
/**
* Dispatched when plugin is released.
*
* @eventType com.bourre.plugin.PluginEvent.onReleasePluginEVENT
*/
[Event(name="onReleasePlugin", type="com.bourre.plugin.PluginEvent")]
/**
* Abstract implementation of Plugin.
*
* @author Francis Bourre
*/
public class AbstractPlugin implements Plugin
{
//--------------------------------------------------------------------
// Protected properties
//--------------------------------------------------------------------
/**Public Braodcaster */
protected var _oEBPublic : Broadcaster;
/** Plugin FronController */
protected var _oController : FrontController;
/** Locator for models */
protected var _oModelLocator : ModelLocator;
/** Locator for views */
protected var _oViewLocator : ViewLocator;
//--------------------------------------------------------------------
// Public API
//--------------------------------------------------------------------
/**
* Creates new AbstractPlugin instance.
*/
public function AbstractPlugin()
{
_initialize();
}
/**
* Starts plugin initialisation.
*
*
builds all locator, and event channel.
*/ protected function _initialize() : void { _oController = new FrontController( this ); _oModelLocator = ModelLocator.getInstance( this ); _oViewLocator = ViewLocator.getInstance( this ); _oEBPublic = ApplicationBroadcaster.getInstance().getChannelDispatcher( getChannel(), this ); if( _oEBPublic ) _oEBPublic.addListener( this ); } /** * Returns plugin's FrontController. */ public function getController() : FrontController { return _oController; } /** * Returns plugin's model locator. */ protected function getModelLocator() : ModelLocator { return _oModelLocator; } /** * Returns plugin's view locator. */ protected function getViewLocator() : ViewLocator { return _oViewLocator; } /** * FiresonInitPlugin event type.
*
* @event onInitPlugin com.bourre.plugin.PluginEvent When plugin is initialized
*/
protected function fireOnInitPlugin() : void
{
firePublicEvent( new PluginEvent( PluginEvent.onInitPluginEVENT, this ) );
}
/**
* Fires onReleasePlugin event type.
*
* @event onReleasePlugin com.bourre.plugin.PluginEvent When plugin is released
*/
protected function fireOnReleasePlugin() : void
{
firePublicEvent( new PluginEvent( PluginEvent.onReleasePluginEVENT, this ) );
}
/**
* @inheritDoc
*/
public function getChannel() : EventChannel
{
return ChannelExpert.getInstance().getChannel( this );
}
/**
* @inheritDoc
*/
public function isModelRegistered( name : String ) : Boolean
{
return _oModelLocator.isRegistered( name );
}
/**
* @inheritDoc
*/
public function getModel( key : String ) : AbstractModel
{
return _oModelLocator.getModel( key );
}
/**
* @inheritDoc
*/
public function isViewRegistered( name : String ) : Boolean
{
return _oViewLocator.isRegistered( name );
}
/**
* @inheritDoc
*/
public function getView( key : String ) : AbstractView
{
return _oViewLocator.getView( key );
}
/**
* @inheritDoc
*/
public function getLogger() : PluginDebug
{
return PluginDebug.getInstance( this );
}
/**
* @inheritDoc
*/
public function fireExternalEvent( e : Event, externalChannel : EventChannel ) : void
{
if ( externalChannel != getChannel() )
{
ApplicationBroadcaster.getInstance().broadcastEvent( e, externalChannel );
} else
{
var msg : String = this + ".fireExternalEvent() failed, '" + externalChannel + "' is its public channel.";
getLogger().error( msg );
throw new IllegalArgumentException( msg );
}
}
/**
* @private
*/
public function handleEvent ( e : Event = null ):void
{
}
/**
* @inheritDoc
*/
public function firePublicEvent( e : Event ) : void
{
if( _oEBPublic ) ( _oEBPublic as PluginBroadcaster ).firePublicEvent( e, this );
else getLogger().warn( this + " doesn't have public dispatcher" );
}
/**
* @inheritDoc
*/
public function firePrivateEvent( e : Event ) : void
{
if ( _oController.isRegistered( e.type ) )
{
_oController.handleEvent( e );
} else
{
getLogger().debug( this + ".firePrivateEvent() fails to retrieve command associated with '" + e.type + "' event type." );
}
}
/**
* @inheritDoc
*/
public function onApplicationInit( ) : void
{
fireOnInitPlugin();
}
/**
* Releases instance.
*
* All locators are releases.
* Event controller and channel too.