Over 10 years of community discussion and knowledge are maintained here as a read-only archive.
public class CommandHistoryProxy extends Proxy implements IProxy { private var _commands:Array; private var _index:uint; public static const NAME:String = 'CommandHistoryProxy'; public function CommandHistoryProxy() { super(NAME); init(); } private function init():void { _commands = new Array(); _index = 0; } public function putCommand(command : IUndoableCommand) : void { _commands[_index++] = command; _commands.splice(_index, _commands.length - _index); } public function previous():IUndoableCommand { return _commands[--_index]; } public function next():IUndoableCommand { return _commands[_index++]; } public function hasPreviousCommands():Boolean { return _index > 0; } public function hasNextCommands():Boolean { return _index < _commands.length; } }}
undo_item = INode(note.getBody()); var command_history_proxy : CommandHistoryProxy = facade.retrieveProxy(CommandHistoryProxy.NAME) as CommandHistoryProxy; command_history_proxy.putCommand(this);
var command:IUndoableCommand = command_history_proxy.previous()
command_history_proxy.putCommand(this, ApplicationFacade.APPROPRIATE_STATE
/** * Registers the undo command * @param cmdClass The class to be executed on undo */ public function registerUndoCommand( cmdClass:Class ):void { undoCmdClass = cmdClass; }
/** * This method must be overridden in the super class. * Post here the code the be executed by the command. */ public function executeCommand():void { throw new Error("The undoable command does not have 'executeCommand' method implemented."); }
public function redo():void { executeCommand(); }
public function undo():void { if ( undoCmdClass == null ) throw new Error("Undo command not set. Could not undo. Use 'registerUndoCommand' to register an undo command"); /** The type of the notification is used as a flag, * indicating whether to save the command into the history, or not. * The undo command, should not be recorded into the history, * and its notification type is set to <code>UndoableCommandEnum.NON_RECORDABLE_COMMAND</code> **/ var oldType:String = _note.getType(); _note.setType( UndoableCommandEnum.NON_RECORDABLE_COMMAND ); try { var commandInstance : ICommand = new undoCmdClass(); commandInstance.execute( _note ); } catch ( err:Error ) { trace("Could not call undo on " + this + ". " + err.getStackTrace() ); } _note.setType( oldType ); }
/** * Saves the command into the CommandHistoryProxy class * ( if <code>note.getType() == UndoableCommandEnum.RECORDABLE_COMMAND</code> ) * and calls the <code>executeCommand</code> method. * * @param note The Notification instance */ override public function execute(note:INotification):void { _note = note; executeCommand(); if ( note.getType() == UndoableCommandEnum.RECORDABLE_COMMAND ) { var historyProxy:CommandHistoryProxy = facade.retrieveProxy( CommandHistoryProxy.NAME ) as CommandHistoryProxy; historyProxy.putCommand( this ); } }
public class AddItemCommand extends UndoableCommandBase { override public function execute(note:INotification):void { super.execute( note ); registerUndoCommand( RemoveItemCommand ); } override public function executeCommand():void { var itemsProxy:ItemsProxy = facade.retrieveProxy( ItemsProxy.NAME ) as ItemsProxy; itemsProxy.addItem( _note.getBody() as ItemVO ); } }
var commandInstance : ICommand = new undoCmdClass();commandInstance.initializeNotifier(multitonKey);commandInstance.execute( _note );