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)
27 using System.Runtime.CompilerServices;
29 namespace System.Collections.Specialized
32 [TypeForwardedFrom (Consts.WindowsBase_3_0)]
34 public class NotifyCollectionChangedEventArgs : EventArgs
36 private NotifyCollectionChangedAction action;
37 private IList oldItems, newItems;
38 private int oldIndex = -1, newIndex = -1;
42 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action)
46 if (action != NotifyCollectionChangedAction.Reset)
47 throw new ArgumentException ("This constructor can only be used with the Reset action.", "action");
50 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems)
51 : this (action, changedItems, -1)
55 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem)
56 : this (action, changedItem, -1)
60 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList newItems, IList oldItems)
61 : this (action, newItems, oldItems, -1)
65 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems, int startingIndex)
69 if (action == NotifyCollectionChangedAction.Add || action == NotifyCollectionChangedAction.Remove) {
70 if (changedItems == null)
71 throw new ArgumentNullException ("changedItems");
73 if (startingIndex < -1)
74 throw new ArgumentException ("The value of startingIndex must be -1 or greater.", "startingIndex");
76 if (action == NotifyCollectionChangedAction.Add)
77 InitializeAdd (changedItems, startingIndex);
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");
84 if (startingIndex != -1)
85 throw new ArgumentException ("This constructor can only be used with the Reset action if startingIndex is -1", "startingIndex");
87 throw new ArgumentException ("This constructor can only be used with the Reset, Add, or Remove actions.", "action");
91 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem, int index)
93 IList changedItems = new object [] { changedItem };
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");
105 throw new ArgumentException ("This constructor can only be used with the Reset action if index is -1", "index");
107 throw new ArgumentException ("This constructor can only be used with the Reset, Add, or Remove actions.", "action");
111 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object newItem, object oldItem)
112 : this (action, newItem, oldItem, -1)
116 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList newItems, IList oldItems, int startingIndex)
118 this.action = action;
120 if (action != NotifyCollectionChangedAction.Replace)
121 throw new ArgumentException ("This constructor can only be used with the Replace action.", "action");
123 if (newItems == null)
124 throw new ArgumentNullException ("newItems");
126 if (oldItems == null)
127 throw new ArgumentNullException ("oldItems");
129 this.oldItems = oldItems;
130 this.newItems = newItems;
132 oldIndex = startingIndex;
133 newIndex = startingIndex;
136 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems, int index, int oldIndex)
138 this.action = action;
140 if (action != NotifyCollectionChangedAction.Move)
141 throw new ArgumentException ("This constructor can only be used with the Move action.", "action");
144 throw new ArgumentException ("The value of index must be -1 or greater.", "index");
146 InitializeMove (changedItems, index, oldIndex);
149 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem, int index, int oldIndex)
150 : this (action, new object [] { changedItem }, index, oldIndex)
154 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object newItem, object oldItem, int index)
156 this.action = action;
158 if (action != NotifyCollectionChangedAction.Replace)
159 throw new ArgumentException ("This constructor can only be used with the Replace action.", "action");
161 InitializeReplace (new object [] { newItem }, new object [] { oldItem }, index);
166 #region Accessor Properties
168 public NotifyCollectionChangedAction Action {
169 get { return action; }
172 public IList NewItems {
173 get { return newItems; }
176 public int NewStartingIndex {
177 get { return newIndex; }
180 public IList OldItems {
181 get { return oldItems; }
184 public int OldStartingIndex {
185 get { return oldIndex; }
190 #region Initialize Methods
192 private void InitializeAdd(IList items, int index)
194 this.newItems = ArrayList.ReadOnly (items);
195 this.newIndex = index;
198 private void InitializeRemove(IList items, int index)
200 this.oldItems = ArrayList.ReadOnly (items);
201 this.oldIndex = index;
204 private void InitializeMove(IList changedItems, int newItemIndex, int oldItemIndex)
206 InitializeAdd (changedItems, newItemIndex);
207 InitializeRemove (changedItems, oldItemIndex);
210 private void InitializeReplace(IList addedItems, IList removedItems, int index)
212 InitializeAdd (addedItems, index);
213 InitializeRemove (removedItems, index);