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