using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Tango.Editors { /// /// Represents an undo/redo states provider base class. /// /// public abstract class UndoRedoStatesProviderBase : IUndoRedoStatesProvider { private IUndoRedoState _uncommittedState; //Holds the current undo state about to be committed. /// /// Occurs when an Undo/Redo state was executed. /// public event EventHandler StateExecuted; /// /// Gets the undo states. /// public Stack UndoStates { get; protected set; } /// /// Gets the redo states. /// public Stack RedoStates { get; protected set; } /// /// Gets a value indicating whether this instance can undo. /// public bool CanUndo { get { return UndoStates.Count > 0; } } /// /// Gets a value indicating whether this instance can redo. /// public bool CanRedo { get { return RedoStates.Count > 0; } } /// /// Initializes a new instance of the class. /// public UndoRedoStatesProviderBase() { UndoStates = new Stack(); RedoStates = new Stack(); } /// /// Prepares the state of the undo. /// public virtual void PrepareUndoState() { RedoStates.Clear(); _uncommittedState = CreateUndoRedoState(); } /// /// Commits the state created by . /// public void CommitUndoState() { if (_uncommittedState != null) { UndoStates.Push(_uncommittedState); _uncommittedState = null; } } /// /// Creates the a new undo/redo state. /// /// public abstract IUndoRedoState CreateUndoRedoState(); /// /// Executes the an undo/redo state. /// /// The state. public abstract void ExecuteState(IUndoRedoState state); /// /// Performs an undo operation. /// public virtual void Undo() { if (UndoStates.Count > 0) { var state = UndoStates.Pop(); var redoState = CreateUndoRedoState(); ExecuteState(state); OnStateExecuted(new UndoRedoStateExecutedEventArgs() { Mode = UndoRedoStateMode.Undo, State = state }); RedoStates.Push(redoState); } } /// /// Performs a redo operation. /// public virtual void Redo() { if (RedoStates.Count > 0) { var state = RedoStates.Pop(); var undoState = CreateUndoRedoState(); ExecuteState(state); OnStateExecuted(new UndoRedoStateExecutedEventArgs() { Mode = UndoRedoStateMode.Redo, State = state }); UndoStates.Push(undoState); } } /// /// Raises the event. /// /// The instance containing the event data. public virtual void OnStateExecuted(UndoRedoStateExecutedEventArgs e) { if (StateExecuted != null) StateExecuted(this, e); } /// /// Resets undo and redo states. /// public void Reset() { UndoStates.Clear(); RedoStates.Clear(); } } }