add ISafeSerializationData
[mcs.git] / class / System / System.Collections.Specialized / NotifyCollectionChangedEventArgs.cs
blobe3ff98d77a09cea35eaa917d67b1c2866694e7f6
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.
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
22 // Authors:
23 // Chris Toshok (toshok@ximian.com)
24 // Brian O'Keefe (zer0keefie@gmail.com)
26 #if NET_4_0
27 using System.Runtime.CompilerServices;
29 namespace System.Collections.Specialized
31 [TypeForwardedFrom (Consts.WindowsBase_3_0)]
32 public class NotifyCollectionChangedEventArgs : EventArgs
34 private NotifyCollectionChangedAction action;
35 private IList oldItems, newItems;
36 private int oldIndex = -1, newIndex = -1;
38 #region Constructors
40 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action)
42 this.action = action;
44 if (action != NotifyCollectionChangedAction.Reset)
45 throw new ArgumentException ("This constructor can only be used with the Reset action.", "action");
48 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems)
49 : this (action, changedItems, -1)
53 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem)
54 : this (action, changedItem, -1)
58 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList newItems, IList oldItems)
59 : this (action, newItems, oldItems, -1)
63 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems, int startingIndex)
65 this.action = action;
67 if (action == NotifyCollectionChangedAction.Add || action == NotifyCollectionChangedAction.Remove) {
68 if (changedItems == null)
69 throw new ArgumentNullException ("changedItems");
71 if (startingIndex < -1)
72 throw new ArgumentException ("The value of startingIndex must be -1 or greater.", "startingIndex");
74 if (action == NotifyCollectionChangedAction.Add)
75 InitializeAdd (changedItems, startingIndex);
76 else
77 InitializeRemove (changedItems, startingIndex);
78 } else if (action == NotifyCollectionChangedAction.Reset) {
79 if (changedItems != null)
80 throw new ArgumentException ("This constructor can only be used with the Reset action if changedItems is null", "changedItems");
82 if (startingIndex != -1)
83 throw new ArgumentException ("This constructor can only be used with the Reset action if startingIndex is -1", "startingIndex");
84 } else {
85 throw new ArgumentException ("This constructor can only be used with the Reset, Add, or Remove actions.", "action");
89 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem, int index)
91 IList changedItems = new object [] { changedItem };
92 this.action = action;
94 if (action == NotifyCollectionChangedAction.Add)
95 InitializeAdd (changedItems, index);
96 else if (action == NotifyCollectionChangedAction.Remove)
97 InitializeRemove (changedItems, index);
98 else if (action == NotifyCollectionChangedAction.Reset) {
99 if (changedItem != null)
100 throw new ArgumentException ("This constructor can only be used with the Reset action if changedItem is null", "changedItem");
102 if (index != -1)
103 throw new ArgumentException ("This constructor can only be used with the Reset action if index is -1", "index");
104 } else {
105 throw new ArgumentException ("This constructor can only be used with the Reset, Add, or Remove actions.", "action");
109 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object newItem, object oldItem)
110 : this (action, newItem, oldItem, -1)
114 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList newItems, IList oldItems, int index)
116 this.action = action;
118 if (action != NotifyCollectionChangedAction.Replace)
119 throw new ArgumentException ("This constructor can only be used with the Replace action.", "action");
121 if (newItems == null)
122 throw new ArgumentNullException ("newItems");
124 if (oldItems == null)
125 throw new ArgumentNullException ("oldItems");
127 this.oldItems = oldItems;
128 this.newItems = newItems;
130 oldIndex = index;
131 newIndex = index;
134 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems, int index, int oldIndex)
136 this.action = action;
138 if (action != NotifyCollectionChangedAction.Move)
139 throw new ArgumentException ("This constructor can only be used with the Move action.", "action");
141 if (index < -1)
142 throw new ArgumentException ("The value of index must be -1 or greater.", "index");
144 InitializeMove (changedItems, index, oldIndex);
147 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem, int index, int oldIndex)
148 : this (action, new object [] { changedItem }, index, oldIndex)
152 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object newItem, object oldItem, int index)
154 this.action = action;
156 if (action != NotifyCollectionChangedAction.Replace)
157 throw new ArgumentException ("This constructor can only be used with the Replace action.", "action");
159 InitializeReplace (new object [] { newItem }, new object [] { oldItem }, index);
162 #endregion
164 #region Accessor Properties
166 public NotifyCollectionChangedAction Action {
167 get { return action; }
170 public IList NewItems {
171 get { return newItems; }
174 public int NewStartingIndex {
175 get { return newIndex; }
178 public IList OldItems {
179 get { return oldItems; }
182 public int OldStartingIndex {
183 get { return oldIndex; }
186 #endregion
188 #region Initialize Methods
190 private void InitializeAdd(IList items, int index)
192 this.newItems = ArrayList.ReadOnly (items);
193 this.newIndex = index;
196 private void InitializeRemove(IList items, int index)
198 this.oldItems = ArrayList.ReadOnly (items);
199 this.oldIndex = index;
202 private void InitializeMove(IList changedItems, int newItemIndex, int oldItemIndex)
204 InitializeAdd (changedItems, newItemIndex);
205 InitializeRemove (changedItems, oldItemIndex);
208 private void InitializeReplace(IList addedItems, IList removedItems, int index)
210 InitializeAdd (addedItems, index);
211 InitializeRemove (removedItems, index);
214 #endregion
217 #endif