public void TestEditingStackOperations() { var commands = new XTMFCommand[20]; for ( int i = 0; i < commands.Length; i++ ) { commands[i] = new TestCommand(); } EditingStack stack = new EditingStack( 10 ); Assert.AreEqual( 0, stack.Count, "The stack's count is incorrect!" ); // fill the stack for ( int i = 0; i < 10; i++ ) { stack.Add( commands[i] ); Assert.AreEqual( i + 1, stack.Count, "The stack's count is incorrect!" ); } // over fill the stack for ( int i = 10; i < 20; i++ ) { stack.Add( commands[i] ); Assert.AreEqual( 10, stack.Count, "The stack's count is incorrect!" ); } // Make sure the first don't exist anymore for ( int i = 0; i < 10; i++ ) { Assert.AreEqual( false, stack.Contains( commands[i] ), "The stack retained a command it should have lost!" ); } // Make sure the newer ones still exist for ( int i = 10; i < 20; i++ ) { Assert.AreEqual( true, stack.Contains( commands[i] ), "The stack lost a command it should have retained!" ); } XTMFCommand command; for ( int i = 19; i >= 10; i-- ) { if ( stack.TryPop( out command ) ) { Assert.AreEqual( commands[i], command, "While popping we popped an unexpected command!" ); } else { Assert.Fail( "A pop failed that should have succeeded!" ); } } }
public XTMFCommand GetCombinedCommand() { // Generate a command the combines all of the contained commands return(XTMFCommand.CreateCommand("Batch command", (ref string error) => { foreach (var command in Commands) { if (!command.Do(ref error)) { return false; } } return true; }, (ref string error) => { foreach (var command in ((IEnumerable <XTMFCommand>)Commands).Reverse()) { if (!command.Undo(ref error)) { return false; } } return true; }, (ref string error) => { foreach (var command in Commands) { if (!command.Redo(ref error)) { return false; } } return true; })); }
public void AddCommand(XTMFCommand command) { Commands.Add(command); }
public bool RunCommand(XTMFCommand command, ref string error) { lock (SessionLock) { if(_IsRunning) { error = "You can not edit a model system while it is running."; return false; } if(command.Do(ref error)) { if(command.CanUndo()) { UndoStack.Add(command); } // if we do something new, redo no long is available RedoStack.Clear(); return true; } return false; } }