EventDescriptorCollection.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // System.ComponentModel.EventDescriptorCollection.cs
  3. //
  4. // Authors:
  5. // Rodrigo Moya ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Ximian, Inc.
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Collections;
  32. using System.Runtime.InteropServices;
  33. namespace System.ComponentModel
  34. {
  35. [ComVisible (true)]
  36. public class EventDescriptorCollection : IList, ICollection, IEnumerable
  37. {
  38. private ArrayList eventList = new ArrayList ();
  39. public static readonly EventDescriptorCollection Empty = new EventDescriptorCollection ();
  40. private EventDescriptorCollection ()
  41. {
  42. }
  43. internal EventDescriptorCollection (ArrayList list)
  44. {
  45. eventList = list;
  46. }
  47. public EventDescriptorCollection (EventDescriptor[] events)
  48. {
  49. if (events == null)
  50. return;
  51. for (int i = 0; i < events.Length; i++)
  52. this.Add (events[i]);
  53. }
  54. public int Add (EventDescriptor value) {
  55. return eventList.Add (value);
  56. }
  57. public void Clear () {
  58. eventList.Clear ();
  59. }
  60. public bool Contains (EventDescriptor value) {
  61. return eventList.Contains (value);
  62. }
  63. public virtual EventDescriptor Find (string name, bool ignoreCase)
  64. {
  65. foreach (EventDescriptor e in eventList) {
  66. if (0 == String.Compare (name, e.Name, ignoreCase))
  67. return e;
  68. }
  69. return null;
  70. }
  71. public IEnumerator GetEnumerator () {
  72. return eventList.GetEnumerator ();
  73. }
  74. public int IndexOf (EventDescriptor value) {
  75. return eventList.IndexOf (value);
  76. }
  77. public void Insert (int index, EventDescriptor value) {
  78. eventList.Insert (index, value);
  79. }
  80. public void Remove (EventDescriptor value) {
  81. eventList.Remove (value);
  82. }
  83. public void RemoveAt (int index) {
  84. eventList.RemoveAt (index);
  85. }
  86. public virtual EventDescriptorCollection Sort () {
  87. EventDescriptorCollection col = CloneCollection ();
  88. col.InternalSort ((IComparer) null);
  89. return col;
  90. }
  91. public virtual EventDescriptorCollection Sort (IComparer comparer) {
  92. EventDescriptorCollection col = CloneCollection ();
  93. col.InternalSort (comparer);
  94. return col;
  95. }
  96. public virtual EventDescriptorCollection Sort (string[] order) {
  97. EventDescriptorCollection col = CloneCollection ();
  98. col.InternalSort (order);
  99. return col;
  100. }
  101. public virtual EventDescriptorCollection Sort (string[] order, IComparer comparer) {
  102. EventDescriptorCollection col = CloneCollection ();
  103. ArrayList sorted = col.ExtractItems (order);
  104. col.InternalSort (comparer);
  105. sorted.AddRange (col.eventList);
  106. col.eventList = sorted;
  107. return col;
  108. }
  109. protected void InternalSort (IComparer comparer) {
  110. eventList.Sort (comparer);
  111. }
  112. protected void InternalSort (string[] order) {
  113. ArrayList sorted = ExtractItems (order);
  114. InternalSort ((IComparer) null);
  115. sorted.AddRange (eventList);
  116. eventList = sorted;
  117. }
  118. ArrayList ExtractItems (string[] names)
  119. {
  120. ArrayList sorted = new ArrayList (eventList.Count);
  121. object[] ext = new object [names.Length];
  122. for (int n=0; n<eventList.Count; n++)
  123. {
  124. EventDescriptor ed = (EventDescriptor) eventList[n];
  125. int i = Array.IndexOf (names, ed.Name);
  126. if (i != -1) {
  127. ext[i] = ed;
  128. eventList.RemoveAt (n);
  129. n--;
  130. }
  131. }
  132. foreach (object ob in ext)
  133. if (ob != null) sorted.Add (ob);
  134. return sorted;
  135. }
  136. private EventDescriptorCollection CloneCollection ()
  137. {
  138. EventDescriptorCollection col = new EventDescriptorCollection ();
  139. col.eventList = (ArrayList) eventList.Clone ();
  140. return col;
  141. }
  142. internal EventDescriptorCollection Filter (Attribute[] attributes)
  143. {
  144. EventDescriptorCollection col = new EventDescriptorCollection ();
  145. foreach (EventDescriptor ed in eventList)
  146. if (ed.Attributes.Contains (attributes))
  147. col.eventList.Add (ed);
  148. return col;
  149. }
  150. public int Count {
  151. get {
  152. return eventList.Count;
  153. }
  154. }
  155. public virtual EventDescriptor this[string name] {
  156. get { return Find (name, false); }
  157. }
  158. public virtual EventDescriptor this[int index] {
  159. get {
  160. return (EventDescriptor) eventList[index];
  161. }
  162. }
  163. // IList methods
  164. int IList.Add (object value) {
  165. return Add ((EventDescriptor) value);
  166. }
  167. bool IList.Contains (object value) {
  168. return Contains ((EventDescriptor) value);
  169. }
  170. int IList.IndexOf (object value) {
  171. return IndexOf ((EventDescriptor) value);
  172. }
  173. void IList.Insert (int index, object value) {
  174. Insert (index, (EventDescriptor) value);
  175. }
  176. void IList.Remove (object value) {
  177. Remove ((EventDescriptor) value);
  178. }
  179. bool IList.IsFixedSize {
  180. get { return false; }
  181. }
  182. bool IList.IsReadOnly {
  183. get { return false; }
  184. }
  185. object IList.this[int index] {
  186. get {
  187. return eventList[index];
  188. }
  189. set {
  190. eventList[index] = value;
  191. }
  192. }
  193. // ICollection methods
  194. void ICollection.CopyTo (Array array, int index) {
  195. eventList.CopyTo (array, index);
  196. }
  197. bool ICollection.IsSynchronized {
  198. get { return false; }
  199. }
  200. object ICollection.SyncRoot {
  201. get { return null; }
  202. }
  203. }
  204. }