NotifyCollectionChangedEventArgs.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 ([email protected])
  24. // Brian O'Keefe ([email protected])
  25. //
  26. namespace System.Collections.Specialized {
  27. public class NotifyCollectionChangedEventArgs : EventArgs {
  28. private NotifyCollectionChangedAction action;
  29. private IList oldItems, newItems;
  30. private int oldIndex = -1, newIndex = -1;
  31. #region Constructors
  32. public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action)
  33. {
  34. this.action = action;
  35. if (action != NotifyCollectionChangedAction.Reset)
  36. throw new ArgumentException ("This constructor can only be used with the Reset action.", "action");
  37. }
  38. public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems)
  39. : this (action, changedItems, -1)
  40. {
  41. }
  42. public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem)
  43. : this (action, changedItem, -1)
  44. {
  45. }
  46. public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList newItems, IList oldItems)
  47. : this (action, newItems, oldItems, -1)
  48. {
  49. }
  50. public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems, int startingIndex)
  51. {
  52. this.action = action;
  53. if (action == NotifyCollectionChangedAction.Add || action == NotifyCollectionChangedAction.Remove) {
  54. if (changedItems == null)
  55. throw new ArgumentNullException ("changedItems");
  56. if (startingIndex < -1)
  57. throw new ArgumentException ("The value of startingIndex must be -1 or greater.", "startingIndex");
  58. if (action == NotifyCollectionChangedAction.Add)
  59. InitializeAdd (changedItems, startingIndex);
  60. else
  61. InitializeRemove (changedItems, startingIndex);
  62. } else if (action == NotifyCollectionChangedAction.Reset) {
  63. if (changedItems != null)
  64. throw new ArgumentException ("This constructor can only be used with the Reset action if changedItems is null", "changedItems");
  65. if (startingIndex != -1)
  66. throw new ArgumentException ("This constructor can only be used with the Reset action if startingIndex is -1", "startingIndex");
  67. } else {
  68. throw new ArgumentException ("This constructor can only be used with the Reset, Add, or Remove actions.", "action");
  69. }
  70. }
  71. public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem, int index)
  72. {
  73. IList changedItems = new object [] { changedItem };
  74. this.action = action;
  75. if (action == NotifyCollectionChangedAction.Add)
  76. InitializeAdd (changedItems, index);
  77. else if (action == NotifyCollectionChangedAction.Remove)
  78. InitializeRemove (changedItems, index);
  79. else if (action == NotifyCollectionChangedAction.Reset) {
  80. if (changedItem != null)
  81. throw new ArgumentException ("This constructor can only be used with the Reset action if changedItem is null", "changedItem");
  82. if (index != -1)
  83. throw new ArgumentException ("This constructor can only be used with the Reset action if index is -1", "index");
  84. } else {
  85. throw new ArgumentException ("This constructor can only be used with the Reset, Add, or Remove actions.", "action");
  86. }
  87. }
  88. public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object newItem, object oldItem)
  89. : this (action, newItem, oldItem, -1)
  90. {
  91. }
  92. public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList newItems, IList oldItems, int index)
  93. {
  94. this.action = action;
  95. if (action != NotifyCollectionChangedAction.Replace)
  96. throw new ArgumentException ("This constructor can only be used with the Replace action.", "action");
  97. if (newItems == null)
  98. throw new ArgumentNullException ("newItems");
  99. if (oldItems == null)
  100. throw new ArgumentNullException ("oldItems");
  101. this.oldItems = oldItems;
  102. this.newItems = newItems;
  103. oldIndex = index;
  104. newIndex = index;
  105. }
  106. public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems, int index, int oldIndex)
  107. {
  108. this.action = action;
  109. if (action != NotifyCollectionChangedAction.Move)
  110. throw new ArgumentException ("This constructor can only be used with the Move action.", "action");
  111. if (index < -1)
  112. throw new ArgumentException ("The value of index must be -1 or greater.", "index");
  113. InitializeMove (changedItems, index, oldIndex);
  114. }
  115. public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem, int index, int oldIndex)
  116. : this (action, new object [] { changedItem }, index, oldIndex)
  117. {
  118. }
  119. public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object newItem, object oldItem, int index)
  120. {
  121. this.action = action;
  122. if (action != NotifyCollectionChangedAction.Replace)
  123. throw new ArgumentException ("This constructor can only be used with the Replace action.", "action");
  124. InitializeReplace (new object [] { newItem }, new object [] { oldItem }, index);
  125. }
  126. #endregion
  127. #region Accessor Properties
  128. public NotifyCollectionChangedAction Action {
  129. get { return action; }
  130. }
  131. public IList NewItems {
  132. get { return newItems; }
  133. }
  134. public int NewStartingIndex {
  135. get { return newIndex; }
  136. }
  137. public IList OldItems {
  138. get { return oldItems; }
  139. }
  140. public int OldStartingIndex {
  141. get { return oldIndex; }
  142. }
  143. #endregion
  144. #region Initialize Methods
  145. private void InitializeAdd(IList items, int index)
  146. {
  147. this.newItems = ArrayList.ReadOnly (items);
  148. this.newIndex = index;
  149. }
  150. private void InitializeRemove(IList items, int index)
  151. {
  152. this.oldItems = ArrayList.ReadOnly (items);
  153. this.oldIndex = index;
  154. }
  155. private void InitializeMove(IList changedItems, int newItemIndex, int oldItemIndex)
  156. {
  157. InitializeAdd (changedItems, newItemIndex);
  158. InitializeRemove (changedItems, oldItemIndex);
  159. }
  160. private void InitializeReplace(IList addedItems, IList removedItems, int index)
  161. {
  162. InitializeAdd (addedItems, index);
  163. InitializeRemove (removedItems, index);
  164. }
  165. #endregion
  166. }
  167. }