using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Tango.PPC.Jobs.UndoRedoCommands { public sealed class UndoRedoManager { private Stack _UndoCommands; private Stack _RedoCommands; private bool _isBusy; private UndoRedoManager() { _UndoCommands = new Stack(); _RedoCommands = new Stack(); _isBusy = false; } private static readonly Lazy lazy = new Lazy(() => new UndoRedoManager()); public static UndoRedoManager Instance { get { return lazy.Value; } } public void InsertAndExecuteCommand(IUndoRedoCommand command) { _UndoCommands.Push(command); command.Execute(); } public void Redo() { if (!_isBusy) { _isBusy = true; if (_RedoCommands.Count != 0) { IUndoRedoCommand command = _RedoCommands.Pop(); command.Execute(); _UndoCommands.Push(command); } _isBusy = false; } } public void Undo() { if (!_isBusy) { _isBusy = true; if (_UndoCommands.Count != 0) { IUndoRedoCommand command = _UndoCommands.Pop(); command.UnExecute(); _RedoCommands.Push(command); } _isBusy = false; } } public bool IsEnableUndoOperation() { return _UndoCommands.Count() > 0; } public bool IsEnableRedoOperation() { return _RedoCommands.Count() > 0; } public void ClearAll() { _UndoCommands.Clear(); _RedoCommands.Clear(); } } }