aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/UndoRedoCommands/UndoRedoManager.cs
blob: 3b926f4ebff891b0f1ec5004a3ce5b7c0e2b39a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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<IUndoRedoCommand> _UndoCommands;
        private Stack<IUndoRedoCommand> _RedoCommands;
        private bool _isBusy;

        private UndoRedoManager()
        {
            _UndoCommands = new Stack<IUndoRedoCommand>();
            _RedoCommands = new Stack<IUndoRedoCommand>();
            _isBusy = false;
        }
        private static readonly Lazy<UndoRedoManager> lazy = new Lazy<UndoRedoManager>(() => 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();
        }
    }
}