Building a Redoable Application


As is true with making commands undoable, making commands redoable is a matter of implementing the IRedoableCommand interface. Next we'll make the commands in our example application redoable. Remember that the redo() method effectively redoes whatever was undone by the undo() method. In the simplest cases, the redo() method can simply call the execute() method. For more complex operations, calling the execute() method won't work correctly. In those cases, it is necessary to work out what steps are necessary to redo the state changes and then implement the redo() method accordingly.

To modify our application to support redoable commands, we'll do the following:

  • Modify the command classes so they implement the IRedoableCommand.

  • Add a redo button to the main class.

Implementing IRedoableCommand

First we'll implement IRedoableCommand in all the command classes. So that all our commands are both redoable and undoable, the commands must implement both IUndoableCommand and IRedoableCommand. Here's RotateClockwiseCommand:

package com.peachpit.aas3wdp.commandexample.commands {    import com.peachpit.aas3wdp.commands.IUndoableCommand;    import com.peachpit.aas3wdp.commands.IRedoableCommand;    import flash.display.DisplayObject;    public class RotateClockwiseCommand implements IUndoableCommand,   IRedoableCommand       private var _receiver:DisplayObject;         public function RotateClockwiseCommand(receiver:DisplayObject) {          _receiver = receiver;       }       public function execute():void {          _receiver.rotation += 20;       }       public function undo():void {          _receiver.rotation -= 20;       }       public function redo():void {           execute();       }     } }


The remaining command classes follow suit. In each case, we import IRedoableCommand, add it to the implements list, and define a redo() method that simply calls execute().

Adding the Redo Button

Next we'll add a redo button to the main class. We do this by first adding the following code to the constructor:

var redoButton:BasicButton = new BasicButton("redo"); addChild(redoButton); redoButton.y = 310; redoButton.addEventListener(MouseEvent.CLICK, onRedo);


This code adds the redo button. We still have to define the onRedo() method to handle the click event. This is our onRedo() method:

private function onRedo(event:MouseEvent):void {    var stack:CommandStack = CommandStack.getInstance();    if(stack.hasNextCommands()) {       var command:ICommand = stack.next();       if(command is IRedoableCommand) {          IRedoableCommand(command).redo();       }    } }


This code is almost identical to the onUndo() method except that it tests that the class passes the IRedoableCommand test. Then it calls the redo() method.

With those few edits, the application now implements redoable commands. Clicking the redo button will re-apply the next command in the command stack.




Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net