PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/AODL/Document/Collection/CollectionWithEvents.cs

https://bitbucket.org/chrisc/aodl
C# | 189 lines | 84 code | 23 blank | 82 comment | 12 complexity | b35fb4df97520eb1e7e95ca3609331d0 MD5 | raw file
  1. /*************************************************************************
  2. *
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
  4. *
  5. * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
  6. *
  7. * Use is subject to license terms.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  10. * use this file except in compliance with the License. You may obtain a copy
  11. * of the License at http://www.apache.org/licenses/LICENSE-2.0. You can also
  12. * obtain a copy of the License at http://odftoolkit.org/docs/license.txt
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. *
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. ************************************************************************/
  22. using AODL.Document.Content;
  23. using System.Collections;
  24. using System.Collections.Generic;
  25. namespace AODL.Document.Collections
  26. {
  27. // Declare the event signatures
  28. /// <summary>
  29. /// The collection clear delegate
  30. /// </summary>
  31. public delegate void CollectionClear();
  32. /// <summary>
  33. /// The collection change event.
  34. /// </summary>
  35. public delegate void CollectionChange<T>(int index, T value);
  36. /// <summary>
  37. /// The events for all collections used within AODL
  38. /// </summary>
  39. public class CollectionWithEvents<T> : List<T>
  40. {
  41. // Collection change events
  42. /// <summary>
  43. /// The clearing event
  44. /// </summary>
  45. public event CollectionClear Clearing;
  46. /// <summary>
  47. /// The cleared event
  48. /// </summary>
  49. public event CollectionClear Cleared;
  50. /// <summary>
  51. /// The inserting event
  52. /// </summary>
  53. public event CollectionChange<T> Inserting;
  54. /// <summary>
  55. /// The inserted event
  56. /// </summary>
  57. public event CollectionChange<T> Inserted;
  58. /// <summary>
  59. /// The removing event
  60. /// </summary>
  61. public event CollectionChange<T> Removing;
  62. /// <summary>
  63. /// The removed event
  64. /// </summary>
  65. public event CollectionChange<T> Removed;
  66. protected virtual void OnClearing()
  67. {
  68. if (Clearing != null) {
  69. Clearing();
  70. }
  71. }
  72. protected virtual void OnCleared()
  73. {
  74. if (Cleared != null) {
  75. Cleared();
  76. }
  77. }
  78. protected virtual void OnInserting(int index, T value)
  79. {
  80. if (Inserting != null) {
  81. Inserting(index, value);
  82. }
  83. }
  84. protected virtual void OnInserted(int index, T value)
  85. {
  86. if (Inserted != null) {
  87. Inserted(index, value);
  88. }
  89. }
  90. protected virtual void OnRemoving(int index, T value)
  91. {
  92. if (Removing != null) {
  93. Removing(index, value);
  94. }
  95. }
  96. protected virtual void OnRemoved(int index, T value)
  97. {
  98. if (Removed != null) {
  99. Removed(index, value);
  100. }
  101. }
  102. public virtual new CollectionWithEvents<T> Add(T value)
  103. {
  104. int index = this.Count - 1;
  105. OnInserting(index, value);
  106. base.Add(value);
  107. OnInserted(index, value);
  108. return this;
  109. }
  110. /// <summary>
  111. /// Executes additional processes while deleting <see cref="T:System.Collections.CollectionBase"/>
  112. /// </summary>
  113. public virtual new CollectionWithEvents<T> Clear()
  114. {
  115. OnClearing();
  116. base.Clear();
  117. OnCleared();
  118. return this;
  119. }
  120. /// <summary>
  121. /// Executes additional processes before deleting an object.<see cref="T:System.Collections.CollectionBase"/>
  122. /// </summary>
  123. /// <param name="index">Zero based index <paramref name="value"/> which should be inserted</param>
  124. /// <param name="value">The new value at <paramref name="index"/>.</param>
  125. public virtual new CollectionWithEvents<T> Insert(int index, T value)
  126. {
  127. OnInserting(index, value);
  128. base.Insert(index, value);
  129. OnInserted(index, value);
  130. return this;
  131. }
  132. /// <summary>
  133. /// Executes additional processes while deleting an object<see cref="T:System.Collections.CollectionBase"/>
  134. /// </summary>
  135. /// <param name="index">The zero based index of the object <paramref name="value"/> to delete</param>
  136. /// <param name="value">The object to <paramref name="index"/> delete.</param>
  137. public virtual new CollectionWithEvents<T> Remove(T value)
  138. {
  139. int index = this.IndexOf(value);
  140. OnRemoving(index, value);
  141. base.Remove(value);
  142. OnRemoved(index, value);
  143. return this;
  144. }
  145. }
  146. }
  147. /*
  148. * $Log: CollectionWithEvents.cs,v $
  149. * Revision 1.2 2008/04/29 15:39:42 mt
  150. * new copyright header
  151. *
  152. * Revision 1.1 2007/02/25 08:58:32 larsbehr
  153. * initial checkin, import from Sourceforge.net to OpenOffice.org
  154. *
  155. * Revision 1.1 2006/01/29 11:29:45 larsbm
  156. * *** empty log message ***
  157. *
  158. * Revision 1.3 2005/11/20 17:31:20 larsbm
  159. * - added suport for XLinks, TabStopStyles
  160. * - First experimental of loading dcuments
  161. * - load and save via importer and exporter interfaces
  162. *
  163. * Revision 1.2 2005/10/08 08:19:25 larsbm
  164. * - added cvs tags
  165. *
  166. */