/* * 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.commands { import com.bourre.error.UnimplementedVirtualMethodException; import com.bourre.log.PixlibStringifier; import com.bourre.model.AbstractModel; import com.bourre.plugin.Plugin; import com.bourre.plugin.PluginDebug; import com.bourre.view.AbstractView; import flash.events.Event; /** * AbstractCommand provides a skeleton for commands which * might work within plugin's FrontController. Abstract command * provides methods which allow the FrontController to set * the command owner at the command creation. Additionally the * AbstractCommand class provides convenient method to access * all MVC components and logging tools of the plugin owner. *

* LowRA encourage the creation of stateless commands, it means * that commands must realize all their process entirely in * the execute call. The constructor of a stateless * command should always be empty. *

* See the How to use * the Command pattern implementation in LowRA document for more details * on the commands package structure. *

* * @author Francis Bourre */ public class AbstractCommand implements Command { /** * A reference to the plugin owner of this command. */ protected var _owner : Plugin; /** * Empty constructor of a stateless command */ public function AbstractCommand() {} /** * Override the execute virtual method * to create a concret command. Stateless command * may use the passed-in event as data source for * their operation. * * @param e event object that will be used as data source by the command * @throws UnimplementedVirtualMethodException — Concret * command doesn't override the execute method * @throws UnreachableDataException — Stateless command * use the passed-in event as data source for its execution, * so the event must provide the right data for the current * Command object. */ public function execute( e : Event = null ) : void { var msg : String = this + ".execute() must be implemented in concrete class."; getLogger().error( msg ); throw( new UnimplementedVirtualMethodException( msg ) ); } /** * Returns a reference to the owner of this command. * * @return the plugin owner of this command */ public function getOwner() : Plugin { return _owner; } /** * Defines the plugin owner of this command. * Generally the owner is defined by the * FrontController of a plugin when * it instantiate a command. * * @param owner plugin which will own the command */ public function setOwner( owner : Plugin ) : void { _owner = owner; } /** * Returns the exclusive logger object owned by the plugin. * It allow this command to send logging message directly on * its owner logging channel. * * @return logger associated to the owner */ public function getLogger() : PluginDebug { return PluginDebug.getInstance( getOwner() ); } /** * Returns a reference to the model AbstractModel. * It allow this command to locate any model registered to * owner's ModelLocator. * * @param model's key * @return a reference to the model registered with key argument */ public function getModel( key : String ) : AbstractModel { return getOwner().getModel( key ); } /** * Returns a reference to the view AbstractView. * It allow this command to locate any view registered to * owner's ViewLocator. * * @param view's key * @return a reference to registered view */ public function getView( key : String ) : AbstractView { return getOwner().getView( key ); } /** * Check if a model AbstractModel is registered * with passed key in owner's ModelLocator. * * @param model's key * @return true if model a model AbstractModel is registered. */ public function isModelRegistered( key : String ) : Boolean { return getOwner().isModelRegistered( key ); } /** * Check if a view AbstractView is registered * with passed key in owner's ViewLocator. * * @param view's key * @return true if model a view AbstractView is registered. */ public function isViewRegistered( key : String ) : Boolean { return getOwner().isViewRegistered( key ); } /** * Returns the string representation of this instance. * * @return the string representation of this instance */ public function toString() : String { return PixlibStringifier.stringify( this ); } /** * Fires a private event directly on this command's owner. */ protected function firePrivateEvent( e : Event ) : void { getOwner().firePrivateEvent( e ); } } }