4873f752f3df786dbd710565db52aed273ca0ffd
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / EditingScopeUndoUnit.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation
6 {
7     using System.Activities.Presentation.Model;
8     using System.Runtime;
9
10     internal class EditingScopeUndoUnit : UndoUnit
11     {
12         private ModelTreeManager modelTreeManager;
13         private EditingScope editingScope;
14         private EditingContext context;
15
16         public EditingScopeUndoUnit(EditingContext context, ModelTreeManager modelTreeManager, EditingScope editingScope)
17             : base(context)
18         {
19             Fx.Assert(context != null, "context cannot be null");
20             Fx.Assert(modelTreeManager != null, "modelTreeManager cannot be null");
21             Fx.Assert(editingScope != null, "editingScope cannot be null");
22
23             this.context = context;
24             this.modelTreeManager = modelTreeManager;
25             this.editingScope = editingScope;
26             this.Description = this.editingScope.Description;
27
28             SaveGlobalState();
29         }
30
31         public override void Redo()
32         {
33             this.modelTreeManager.StopTracking();
34             try
35             {
36                 EditingScope redoEditingScope = this.modelTreeManager.CreateEditingScope(this.editingScope.Description);
37                 redoEditingScope.Changes.AddRange(editingScope.Changes);
38                 redoEditingScope.Complete();
39             }
40             finally
41             {
42                 this.modelTreeManager.StartTracking();
43             }
44             ApplyGlobalState();
45         }
46
47         public override void Undo()
48         {
49             this.modelTreeManager.StopTracking();
50             try
51             {
52                 EditingScope undoEditingScope = this.modelTreeManager.CreateEditingScope(this.editingScope.Description);
53                 foreach (Change change in editingScope.Changes)
54                 {
55                     Change inverseChange = change.GetInverse();
56                     if (inverseChange != null)
57                     {
58                         undoEditingScope.Changes.Add(inverseChange);
59                     }
60                 }
61                 undoEditingScope.Changes.Reverse();
62                 undoEditingScope.Complete();
63             }
64             finally
65             {
66                 this.modelTreeManager.StartTracking();
67             }
68             ApplyGlobalState();
69         }
70     }
71 }