00001 import gugga.common.ICommand;
00002 import gugga.common.UIComponentEx;
00003 import gugga.utils.DebugUtils;
00004
00005 [Event("commandBufferContentChanged")]
00006 [Event("commandAdded")]
00007
00011 class gugga.commands.CommandHistory extends UIComponentEx {
00012 private var mHistoryBuffer:Array;
00013 private var mCurrent:Number;
00014
00015 public function CommandHistory()
00016 {
00017 clear();
00018 }
00019
00020 public function hasUndoableActions(): Boolean
00021 {
00022 return mCurrent >= 0;
00023 }
00024 public function hasRedoableActions(): Boolean
00025 {
00026 return ( mCurrent+1 < mHistoryBuffer.length);
00027 }
00028
00029 public function undo()
00030 {
00031 if (hasUndoableActions())
00032 {
00033 ICommand( mHistoryBuffer[mCurrent]).undo();
00034 mCurrent --;
00035
00036 dispatchEvent({type:"commandBufferContentChanged", currentCommandIndex: mCurrent, target : this });
00037 }
00038 }
00039
00040 public function redo()
00041 {
00042 if (hasRedoableActions())
00043 {
00044 mCurrent ++;
00045 ICommand( mHistoryBuffer[mCurrent]).execute();
00046
00047 dispatchEvent({type:"commandBufferContentChanged", currentCommandIndex: mCurrent, target : this});
00048 }
00049
00050 }
00051
00052 public function add(cmd : ICommand)
00053 {
00054 while (hasRedoableActions())
00055 mHistoryBuffer.pop();
00056
00057 mHistoryBuffer.push(cmd);
00058 mCurrent = mHistoryBuffer.length -1;
00059
00060 dispatchEvent({type:"commandBufferContentChanged", currentCommandIndex: mCurrent, target : this});
00061 dispatchEvent({type:"commandAdded", currentCommandIndex: mCurrent, target : this});
00062 }
00063
00064 public function clear()
00065 {
00066 mHistoryBuffer = new Array();
00067 mCurrent = -1;
00068
00069 dispatchEvent({type:"commandBufferContentChanged", currentCommandIndex: mCurrent, target : this});
00070 }
00071
00072 private function getNextUndoCommand():ICommand
00073 {
00074 return null;
00075 }
00076 private function getNextRedoCommand():ICommand
00077 {
00078 return null;
00079 }
00080
00088 public function navigate(aCommandIndex : Number)
00089 {
00090 if(mCurrent > aCommandIndex)
00091 {
00092 ICommand(mHistoryBuffer[aCommandIndex + 1]).undo();
00093 mCurrent = aCommandIndex;
00094 }
00095 else if(mCurrent < aCommandIndex)
00096 {
00097 ICommand(mHistoryBuffer[aCommandIndex]).execute();
00098 mCurrent = aCommandIndex;
00099 }
00100 else
00101 {
00102 DebugUtils.trace("requesting current navigation state - history buffer takes no action");
00103 }
00104 }
00105 }