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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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)
23 // Chris Toshok (toshok@ximian.com)
24 // Brian O'Keefe (zer0keefie@gmail.com)
26 using System.Runtime.CompilerServices;
28 namespace System.Collections.Specialized
31 [TypeForwardedFrom (Consts.WindowsBase_3_0)]
33 public class NotifyCollectionChangedEventArgs : EventArgs
35 private NotifyCollectionChangedAction action;
36 private IList oldItems, newItems;
37 private int oldIndex = -1, newIndex = -1;
41 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action)
45 if (action != NotifyCollectionChangedAction.Reset)
46 throw new ArgumentException ("This constructor can only be used with the Reset action.", "action");
49 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems)
50 : this (action, changedItems, -1)
54 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem)
55 : this (action, changedItem, -1)
59 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList newItems, IList oldItems)
60 : this (action, newItems, oldItems, -1)
64 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems, int startingIndex)
68 if (action == NotifyCollectionChangedAction.Add || action == NotifyCollectionChangedAction.Remove) {
69 if (changedItems == null)
70 throw new ArgumentNullException ("changedItems");
72 if (startingIndex < -1)
73 throw new ArgumentException ("The value of startingIndex must be -1 or greater.", "startingIndex");
75 if (action == NotifyCollectionChangedAction.Add)
76 InitializeAdd (changedItems, startingIndex);
78 InitializeRemove (changedItems, startingIndex);
79 } else if (action == NotifyCollectionChangedAction.Reset) {
80 if (changedItems != null)
81 throw new ArgumentException ("This constructor can only be used with the Reset action if changedItems is null", "changedItems");
83 if (startingIndex != -1)
84 throw new ArgumentException ("This constructor can only be used with the Reset action if startingIndex is -1", "startingIndex");
86 throw new ArgumentException ("This constructor can only be used with the Reset, Add, or Remove actions.", "action");
90 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem, int index)
92 IList changedItems = new object [] { changedItem };
95 if (action == NotifyCollectionChangedAction.Add)
96 InitializeAdd (changedItems, index);
97 else if (action == NotifyCollectionChangedAction.Remove)
98 InitializeRemove (changedItems, index);
99 else if (action == NotifyCollectionChangedAction.Reset) {
100 if (changedItem != null)
101 throw new ArgumentException ("This constructor can only be used with the Reset action if changedItem is null", "changedItem");
104 throw new ArgumentException ("This constructor can only be used with the Reset action if index is -1", "index");
106 throw new ArgumentException ("This constructor can only be used with the Reset, Add, or Remove actions.", "action");
110 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object newItem, object oldItem)
111 : this (action, newItem, oldItem, -1)
115 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList newItems, IList oldItems, int startingIndex)
117 this.action = action;
119 if (action != NotifyCollectionChangedAction.Replace)
120 throw new ArgumentException ("This constructor can only be used with the Replace action.", "action");
122 if (newItems == null)
123 throw new ArgumentNullException ("newItems");
125 if (oldItems == null)
126 throw new ArgumentNullException ("oldItems");
128 this.oldItems = oldItems;
129 this.newItems = newItems;
131 oldIndex = startingIndex;
132 newIndex = startingIndex;
135 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems, int index, int oldIndex)
137 this.action = action;
139 if (action != NotifyCollectionChangedAction.Move)
140 throw new ArgumentException ("This constructor can only be used with the Move action.", "action");
143 throw new ArgumentException ("The value of index must be -1 or greater.", "index");
145 InitializeMove (changedItems, index, oldIndex);
148 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem, int index, int oldIndex)
149 : this (action, new object [] { changedItem }, index, oldIndex)
153 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object newItem, object oldItem, int index)
155 this.action = action;
157 if (action != NotifyCollectionChangedAction.Replace)
158 throw new ArgumentException ("This constructor can only be used with the Replace action.", "action");
160 InitializeReplace (new object [] { newItem }, new object [] { oldItem }, index);
165 #region Accessor Properties
167 public NotifyCollectionChangedAction Action {
168 get { return action; }
171 public IList NewItems {
172 get { return newItems; }
175 public int NewStartingIndex {
176 get { return newIndex; }
179 public IList OldItems {
180 get { return oldItems; }
183 public int OldStartingIndex {
184 get { return oldIndex; }
189 #region Initialize Methods
191 private void InitializeAdd(IList items, int index)
193 this.newItems = ArrayList.ReadOnly (items);
194 this.newIndex = index;
197 private void InitializeRemove(IList items, int index)
199 this.oldItems = ArrayList.ReadOnly (items);
200 this.oldIndex = index;
203 private void InitializeMove(IList changedItems, int newItemIndex, int oldItemIndex)
205 InitializeAdd (changedItems, newItemIndex);
206 InitializeRemove (changedItems, oldItemIndex);
209 private void InitializeReplace(IList addedItems, IList removedItems, int index)
211 InitializeAdd (addedItems, index);
212 InitializeRemove (removedItems, index);