97a84caf35d40ef6daf4051b04b66c5bc8f6d3b6
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Model / DictionaryChange.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Model
6 {
7     using System;
8     using System.Activities.Presentation.Services;
9     using System.Collections.Generic;
10     using System.Runtime;
11     using System.Text;
12
13     class DictionaryChange : ModelChange
14     {
15         public ModelItemDictionary Dictionary { get; set; }
16
17         public OperationType Operation { get; set; }
18
19         public ModelItem Key { get; set; }
20
21         public ModelItem Value { get; set; }
22
23         public ModelTreeManager ModelTreeManager { get; set; }
24
25         public override string Description
26         {
27             get
28             {
29                 return this.Operation == OperationType.Insert ? SR.DictionaryAddEditingScopeDescription : SR.DictionaryRemoveEditingScopeDescription;
30             }
31         }
32
33         public override bool Apply()
34         {
35             switch (this.Operation)
36             {
37                 case OperationType.Insert:
38                     ApplyInsert();
39                     break;
40                 case OperationType.Delete:
41                     ApplyDelete();
42                     break;
43                 default:
44                     Fx.Assert("Operation should be Insert or Delete");
45                     break;
46             }
47             return true;
48         }
49
50         private void ApplyDelete()
51         {
52             ((ModelItemDictionaryImpl)this.Dictionary).RemoveCore(this.Key);
53
54             ModelChangeInfo changeInfo = ModelChangeInfoImpl.CreateDictionaryKeyValueRemoved(this.Dictionary, this.Key, this.Value);
55
56             if (this.Key != null)
57             {
58                 this.ModelTreeManager.modelService.OnModelItemRemoved(this.Key, changeInfo);
59                 changeInfo = null;
60             }
61
62             if (this.Value != null)
63             {
64                 this.ModelTreeManager.modelService.OnModelItemRemoved(this.Value, changeInfo);
65                 changeInfo = null;
66             }
67
68             if (changeInfo != null)
69             {
70                 this.ModelTreeManager.modelService.EmitModelChangeInfo(changeInfo);
71             }
72         }
73
74         private void ApplyInsert()
75         {
76             ((ModelItemDictionaryImpl)this.Dictionary).AddCore(this.Key, this.Value);
77
78             ModelChangeInfo changeInfo = ModelChangeInfoImpl.CreateDictionaryKeyValueAdded(this.Dictionary, this.Key, this.Value);
79
80             if (this.Key != null)
81             {
82                 this.ModelTreeManager.modelService.OnModelItemAdded(this.Key, changeInfo);
83                 changeInfo = null;
84             }
85
86             if (this.Value != null)
87             {
88                 this.ModelTreeManager.modelService.OnModelItemAdded(this.Value, changeInfo);
89                 changeInfo = null;
90             }
91
92             if (changeInfo != null)
93             {
94                 this.ModelTreeManager.modelService.EmitModelChangeInfo(changeInfo);
95             }
96         }
97
98         public override Change GetInverse()
99         {
100             OperationType reverseOperation = this.Operation == OperationType.Insert ? OperationType.Delete : OperationType.Insert;
101             return new DictionaryChange()
102                 {
103                     Dictionary = this.Dictionary,
104                     Operation = reverseOperation,
105                     Key = this.Key,
106                     Value = this.Value,
107                     ModelTreeManager = this.ModelTreeManager,
108                 };
109         }
110
111         public enum OperationType 
112         { 
113             Insert, Delete 
114         }
115     }
116 }