Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Common/UndoSystem/UndoProvider.cs @ 13792

Last change on this file since 13792 was 12503, checked in by aballeit, 9 years ago

#2283 added GUI and charts; fixed MCTS

File size: 3.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.ComponentModel;
6using System.Windows;
7
8namespace Microsoft.Research.DynamicDataDisplay.Common.UndoSystem
9{
10  public class UndoProvider : INotifyPropertyChanged
11  {
12    private readonly ActionStack undoStack = new ActionStack();
13    private readonly ActionStack redoStack = new ActionStack();
14
15    public UndoProvider()
16    {
17      undoStack.IsEmptyChanged += OnUndoStackIsEmptyChanged;
18      redoStack.IsEmptyChanged += OnRedoStackIsEmptyChanged;
19    }
20
21    private bool isEnabled = true;
22    public bool IsEnabled
23    {
24      get { return isEnabled; }
25      set { isEnabled = value; }
26    }
27
28    private void OnUndoStackIsEmptyChanged(object sender, EventArgs e)
29    {
30      PropertyChanged.Raise(this, "CanUndo");
31    }
32
33    private void OnRedoStackIsEmptyChanged(object sender, EventArgs e)
34    {
35      PropertyChanged.Raise(this, "CanRedo");
36    }
37
38    public void AddAction(UndoAction action)
39    {
40      if (!isEnabled)
41        return;
42
43      if (state != UndoState.None)
44        return;
45
46      undoStack.Push(action);
47      redoStack.Clear();
48    }
49
50    public void Undo()
51    {
52      var action = undoStack.Pop();
53      redoStack.Push(action);
54
55      state = UndoState.Undoing;
56      try
57      {
58        action.Undo();
59      }
60      finally
61      {
62        state = UndoState.None;
63      }
64    }
65
66    public void Redo()
67    {
68      var action = redoStack.Pop();
69      undoStack.Push(action);
70
71      state = UndoState.Redoing;
72      try
73      {
74        action.Do();
75      }
76      finally
77      {
78        state = UndoState.None;
79      }
80    }
81
82    public bool CanUndo
83    {
84      get { return !undoStack.IsEmpty; }
85    }
86
87    public bool CanRedo
88    {
89      get { return !redoStack.IsEmpty; }
90    }
91
92    private UndoState state = UndoState.None;
93    public UndoState State
94    {
95      get { return state; }
96    }
97
98    #region INotifyPropertyChanged Members
99
100    public event PropertyChangedEventHandler PropertyChanged;
101
102    #endregion
103
104    private readonly Dictionary<CaptureKeyHolder, object> captureHolders = new Dictionary<CaptureKeyHolder, object>();
105    public void CaptureOldValue(DependencyObject target, DependencyProperty property, object oldValue)
106    {
107      captureHolders[new CaptureKeyHolder { Target = target, Property = property }] = oldValue;
108    }
109
110    public void CaptureNewValue(DependencyObject target, DependencyProperty property, object newValue)
111    {
112      var holder = new CaptureKeyHolder { Target = target, Property = property };
113      if (captureHolders.ContainsKey(holder))
114      {
115        object oldValue = captureHolders[holder];
116        captureHolders.Remove(holder);
117
118        if (!Object.Equals(oldValue, newValue))
119        {
120          var action = new DependencyPropertyChangedUndoAction(target, property, oldValue, newValue);
121          AddAction(action);
122        }
123      }
124    }
125
126    private sealed class CaptureKeyHolder
127    {
128      public DependencyObject Target { get; set; }
129      public DependencyProperty Property { get; set; }
130
131      public override int GetHashCode()
132      {
133        return Target.GetHashCode() ^ Property.GetHashCode();
134      }
135
136      public override bool Equals(object obj)
137      {
138        if (obj == null) return false;
139
140        CaptureKeyHolder other = obj as CaptureKeyHolder;
141        if (other == null) return false;
142
143        return Target == other.Target && Property == other.Property;
144      }
145    }
146  }
147
148  public enum UndoState
149  {
150    None,
151    Undoing,
152    Redoing
153  }
154}
Note: See TracBrowser for help on using the repository browser.