[runtime] Fix corlib out of date error with disabled COM
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Model / CollectionChange.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Model
6 {
7     using System.Runtime;
8     using System.Activities.Presentation.Services;
9
10     class CollectionChange : ModelChange
11     {
12         public ModelItemCollection Collection { get; set; }
13
14         public int Index { get; set; }
15
16         public ModelItem Item { get; set; }
17
18         public OperationType Operation { get; set; }
19
20         public ModelTreeManager ModelTreeManager { get; set; }
21
22         public override string Description
23         {
24             get
25             {
26                 return this.Operation == OperationType.Insert ? SR.CollectionAddEditingScopeDescription : SR.CollectionRemoveEditingScopeDescription;
27             }
28         }
29
30         public override bool Apply()
31         {
32             switch (this.Operation)
33             {
34                 case OperationType.Insert:
35                     ApplyInsert();
36                     break;
37                 case OperationType.Delete:
38                     ApplyDelete();
39                     break;
40                 default:
41                     Fx.Assert("Operation should be Insert or Delete");
42                     break;
43
44             }
45             return true;
46         }
47
48         private void ApplyDelete()
49         {
50             Fx.Assert(this.ModelTreeManager != null, "ModelTreeManager cannot be null.");
51             Fx.Assert(this.Collection != null, "this.Collection cannot be null.");
52
53             if (this.Index >= 0)
54             {
55                 ((ModelItemCollectionImpl)this.Collection).RemoveAtCore(this.Index);
56             }
57             else
58             {
59                 Fx.Assert(this.Index == -1, "-1 must be used to indicate Remove(item)");
60                 this.Index = this.Collection.IndexOf(this.Item);
61                 ((ModelItemCollectionImpl)this.Collection).RemoveCore(this.Item);
62             }
63
64             ModelChangeInfo changeInfo = ModelChangeInfoImpl.CreateCollectionItemRemoved(this.Collection, this.Item);
65             this.ModelTreeManager.NotifyCollectionRemove(this.Item, changeInfo);
66         }
67
68         private void ApplyInsert()
69         {
70             Fx.Assert(this.ModelTreeManager != null, "ModelTreeManager cannot be null.");
71             Fx.Assert(this.Collection != null, "this.Collection cannot be null.");
72
73             if (this.Index >= 0)
74             {
75                 ((ModelItemCollectionImpl)this.Collection).InsertCore(this.Index, this.Item);
76             }
77             else
78             {
79                 Fx.Assert(this.Index == -1, "-1 must be used to indicate Add(item)");
80                 this.Index = this.Collection.Count;
81                 ((ModelItemCollectionImpl)this.Collection).AddCore(this.Item);
82             }
83
84             ModelChangeInfo changeInfo = ModelChangeInfoImpl.CreateCollectionItemAdded(this.Collection, this.Item);
85             this.ModelTreeManager.NotifyCollectionInsert(this.Item, changeInfo);
86         }
87
88         public override Change GetInverse()
89         {
90             OperationType reverseOperation = this.Operation == OperationType.Insert ? OperationType.Delete : OperationType.Insert;
91             return new CollectionChange()
92                 {
93                     Collection = this.Collection,
94                     Operation = reverseOperation,
95                     Item = this.Item,
96                     ModelTreeManager = this.ModelTreeManager,
97                     Index = this.Index
98                 };
99
100         }
101
102         public enum OperationType 
103         { 
104             Insert, Delete
105         }
106     }
107 }