public void ExecuteCombinedCommands(string name, Action combinedCommandContext) { lock (_SessionLock) { _InCombinedContext = true; var list = _CombinedCommands = new List <XTMFCommand>(); combinedCommandContext(); // only add to the undo list if a command was added successfully if (list.Count > 0) { // create a command to undo everything in a single shot [do is empty] _UndoStack.Add(XTMFCommand.CreateCommand(name, (ref string error) => { return(true); }, (ref string error) => { foreach (var command in ((IEnumerable <XTMFCommand>)list).Reverse()) { if (command.CanUndo()) { if (!command.Undo(ref error)) { return(false); } } } return(true); }, (ref string error) => { foreach (var command in list) { if (command.CanUndo()) { if (!command.Redo(ref error)) { return(false); } } } return(true); })); CommandExecuted?.Invoke(this, new EventArgs()); } _InCombinedContext = false; _CombinedCommands = null; } }
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 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 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)) { HasChanged = true; if (command.CanUndo()) { UndoStack.Add(command); } // if we do something new, redo no long is available RedoStack.Clear(); return(true); } return(false); } }
/// <summary> /// Undo the last command /// </summary> /// <param name="error">The reason it failed</param> public bool Undo(ref string error) { lock (SessionLock) { XTMFCommand command; if (UndoStack.TryPop(out command)) { if (command != null) { if (command.Undo(ref error)) { HasChanged = true; RedoStack.Add(command); return(true); } return(false); } } error = "There was nothing to undo."; return(false); } }
/// <summary> /// Undo the last command /// </summary> /// <param name="error">The reason it failed</param> public bool Undo(ref string error) { lock (_SessionLock) { if (_UndoStack.TryPop(out var command)) { if (command != null) { if (command.Undo(ref error)) { HasChanged = true; _RedoStack.Add(command); CommandExecuted?.Invoke(this, new EventArgs()); return(true); } return(false); } } error = "There was nothing to undo."; return(false); } }