18d2d3d91f6648a3a2baa7a18cdc27c0f74423d4
[mono.git] / mcs / class / System / System.Collections.Specialized / NotifyCollectionChangedEventArgs.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Chris Toshok (toshok@ximian.com)
24 //      Brian O'Keefe (zer0keefie@gmail.com)
25 //
26 #if NET_4_0 || MOBILE
27 using System.Runtime.CompilerServices;
28
29 namespace System.Collections.Specialized
30 {
31 #if !MOBILE
32         [TypeForwardedFrom (Consts.WindowsBase_3_0)]
33 #endif
34         public class NotifyCollectionChangedEventArgs : EventArgs
35         {
36                 private NotifyCollectionChangedAction action;
37                 private IList oldItems, newItems;
38                 private int oldIndex = -1, newIndex = -1;
39
40                 #region Constructors
41
42                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action)
43                 {
44                         this.action = action;
45
46                         if (action != NotifyCollectionChangedAction.Reset)
47                                 throw new ArgumentException ("This constructor can only be used with the Reset action.", "action");
48                 }
49
50                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems)
51                         : this (action, changedItems, -1)
52                 {
53                 }
54
55                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem)
56                         : this (action, changedItem, -1)
57                 {
58                 }
59
60                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList newItems, IList oldItems)
61                         : this (action, newItems, oldItems, -1)
62                 {
63                 }
64
65                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems, int startingIndex)
66                 {
67                         this.action = action;
68
69                         if (action == NotifyCollectionChangedAction.Add || action == NotifyCollectionChangedAction.Remove) {
70                                 if (changedItems == null)
71                                         throw new ArgumentNullException ("changedItems");
72
73                                 if (startingIndex < -1)
74                                         throw new ArgumentException ("The value of startingIndex must be -1 or greater.", "startingIndex");
75
76                                 if (action == NotifyCollectionChangedAction.Add)
77                                         InitializeAdd (changedItems, startingIndex);
78                                 else
79                                         InitializeRemove (changedItems, startingIndex);
80                         } else if (action == NotifyCollectionChangedAction.Reset) {
81                                 if (changedItems != null)
82                                         throw new ArgumentException ("This constructor can only be used with the Reset action if changedItems is null", "changedItems");
83
84                                 if (startingIndex != -1)
85                                         throw new ArgumentException ("This constructor can only be used with the Reset action if startingIndex is -1", "startingIndex");
86                         } else {
87                                 throw new ArgumentException ("This constructor can only be used with the Reset, Add, or Remove actions.", "action");
88                         }
89                 }
90
91                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem, int index)
92                 {
93                         IList changedItems = new object [] { changedItem };
94                         this.action = action;
95
96                         if (action == NotifyCollectionChangedAction.Add)
97                                 InitializeAdd (changedItems, index);
98                         else if (action == NotifyCollectionChangedAction.Remove)
99                                 InitializeRemove (changedItems, index);
100                         else if (action == NotifyCollectionChangedAction.Reset) {
101                                 if (changedItem != null)
102                                         throw new ArgumentException ("This constructor can only be used with the Reset action if changedItem is null", "changedItem");
103
104                                 if (index != -1)
105                                         throw new ArgumentException ("This constructor can only be used with the Reset action if index is -1", "index");
106                         } else {
107                                 throw new ArgumentException ("This constructor can only be used with the Reset, Add, or Remove actions.", "action");
108                         }
109                 }
110
111                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object newItem, object oldItem)
112                         : this (action, newItem, oldItem, -1)
113                 {
114                 }
115
116                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList newItems, IList oldItems, int startingIndex)
117                 {
118                         this.action = action;
119
120                         if (action != NotifyCollectionChangedAction.Replace)
121                                 throw new ArgumentException ("This constructor can only be used with the Replace action.", "action");
122
123                         if (newItems == null)
124                                 throw new ArgumentNullException ("newItems");
125
126                         if (oldItems == null)
127                                 throw new ArgumentNullException ("oldItems");
128
129                         this.oldItems = oldItems;
130                         this.newItems = newItems;
131
132                         oldIndex = startingIndex;
133                         newIndex = startingIndex;
134                 }
135
136                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems, int index, int oldIndex)
137                 {
138                         this.action = action;
139
140                         if (action != NotifyCollectionChangedAction.Move)
141                                 throw new ArgumentException ("This constructor can only be used with the Move action.", "action");
142
143                         if (index < -1)
144                                 throw new ArgumentException ("The value of index must be -1 or greater.", "index");
145
146                         InitializeMove (changedItems, index, oldIndex);
147                 }
148
149                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem, int index, int oldIndex)
150                         : this (action, new object [] { changedItem }, index, oldIndex)
151                 {
152                 }
153
154                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object newItem, object oldItem, int index)
155                 {
156                         this.action = action;
157
158                         if (action != NotifyCollectionChangedAction.Replace)
159                                 throw new ArgumentException ("This constructor can only be used with the Replace action.", "action");
160
161                         InitializeReplace (new object [] { newItem }, new object [] { oldItem }, index);
162                 }
163
164                 #endregion
165
166                 #region Accessor Properties
167
168                 public NotifyCollectionChangedAction Action {
169                         get { return action; }
170                 }
171
172                 public IList NewItems {
173                         get { return newItems; }
174                 }
175
176                 public int NewStartingIndex {
177                         get { return newIndex; }
178                 }
179
180                 public IList OldItems {
181                         get { return oldItems; }
182                 }
183
184                 public int OldStartingIndex {
185                         get { return oldIndex; }
186                 }
187
188                 #endregion
189
190                 #region Initialize Methods
191
192                 private void InitializeAdd(IList items, int index)
193                 {
194                         this.newItems = ArrayList.ReadOnly (items);
195                         this.newIndex = index;
196                 }
197
198                 private void InitializeRemove(IList items, int index)
199                 {
200                         this.oldItems = ArrayList.ReadOnly (items);
201                         this.oldIndex = index;
202                 }
203
204                 private void InitializeMove(IList changedItems, int newItemIndex, int oldItemIndex)
205                 {
206                         InitializeAdd (changedItems, newItemIndex);
207                         InitializeRemove (changedItems, oldItemIndex);
208                 }
209
210                 private void InitializeReplace(IList addedItems, IList removedItems, int index)
211                 {
212                         InitializeAdd (addedItems, index);
213                         InitializeRemove (removedItems, index);
214                 }
215
216                 #endregion
217         }
218 }
219 #endif