/// <summary> /// Calls <see cref="UndoableCommand.Unexecute"/> in order to 'undo' the current command. /// </summary> /// <remarks> /// Decrements the <see cref="CurrentCommandIndex"/>, unless it is already -1. A <see cref="CurrentCommandIndex"/> of -1 /// indicates that the entire command history has been undone and only <see cref="Redo"/> operations can occur. /// </remarks> public void Undo() { if (NumCommands == 0) { return; } if (_currentCommandIndex == -1) { return; } EventsHelper.Fire(_currentCommandChangingEvent, this, EventArgs.Empty); UndoableCommand cmd = _history[_currentCommandIndex]; try { cmd.Unexecute(); } catch (Exception e) { Platform.Log(LogLevel.Error, e); } _currentCommandIndex--; EventsHelper.Fire(_currentCommandChangedEvent, this, EventArgs.Empty); }
/// <summary> /// Adds a command to the command history. /// </summary> /// <remarks> /// When a command is added, all commands after the <see cref="CurrentCommandIndex"/> will be removed /// in order to keep the state of the application consistent. The added command will then become /// the last command (<see cref="LastCommandIndex"/>). /// </remarks> public void AddCommand(UndoableCommand command) { Platform.CheckForNullReference(command, "command"); EventsHelper.Fire(_currentCommandChangingEvent, this, EventArgs.Empty); if (_currentCommandIndex < _lastCommandIndex) { int numCommandsToRemove = _lastCommandIndex - _currentCommandIndex; _history.RemoveRange(_currentCommandIndex + 1, numCommandsToRemove); _lastCommandIndex -= numCommandsToRemove; } _history.Add(command); if (NumCommands > _maxSize) { _history.RemoveAt(0); if (_currentCommandIndex == _lastCommandIndex) { _currentCommandIndex--; } _lastCommandIndex--; } _currentCommandIndex++; _lastCommandIndex++; EventsHelper.Fire(_currentCommandChangedEvent, this, EventArgs.Empty); }
//private bool _isCurrent = false; private CommandInfo(UndoableCommand command) { _name = command.Name; _hashcode = command.GetHashCode(); Type type = command.GetType(); _type = type.FullName; _assembly = type.Assembly.FullName; }
/// <summary> /// Adds/Enqueues an <see cref="UndoableCommand"/>. /// </summary> public void Enqueue(UndoableCommand command) { _commands.Add(command); }
/// <summary> /// Adds a command to the command history. /// </summary> /// <remarks> /// When a command is added, all commands after the <see cref="CurrentCommandIndex"/> will be removed /// in order to keep the state of the application consistent. The added command will then become /// the last command (<see cref="LastCommandIndex"/>). /// </remarks> public void AddCommand(UndoableCommand command) { Platform.CheckForNullReference(command, "command"); EventsHelper.Fire(_currentCommandChangingEvent, this, EventArgs.Empty); if (_currentCommandIndex < _lastCommandIndex) { int numCommandsToRemove = _lastCommandIndex - _currentCommandIndex; _history.RemoveRange(_currentCommandIndex + 1, numCommandsToRemove); _lastCommandIndex -= numCommandsToRemove; } _history.Add(command); if (NumCommands > _maxSize) { _history.RemoveAt(0); if (_currentCommandIndex == _lastCommandIndex) _currentCommandIndex--; _lastCommandIndex--; } _currentCommandIndex++; _lastCommandIndex++; EventsHelper.Fire(_currentCommandChangedEvent, this, EventArgs.Empty); }
internal static CommandInfo CreateCommandInfo(UndoableCommand command) { if (command is MemorableUndoableCommand) return new MemorableCommandInfo((MemorableUndoableCommand) command); else if (command is CompositeUndoableCommand) return new CompositeCommandInfo((CompositeUndoableCommand) command); return new CommandInfo(command); }