EventDescriptorCollection.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. for (int i = 0; i < events.Length; i++)
  50. this.Add (events[i]);
  51. }
  52. public int Add (EventDescriptor value) {
  53. return eventList.Add (value);
  54. }
  55. public void Clear () {
  56. eventList.Clear ();
  57. }
  58. public bool Contains (EventDescriptor value) {
  59. return eventList.Contains (value);
  60. }
  61. public virtual EventDescriptor Find (string name, bool ignoreCase)
  62. {
  63. foreach (EventDescriptor e in eventList) {
  64. if (0 == String.Compare (name, e.Name, ignoreCase))
  65. return e;
  66. }
  67. return null;
  68. }
  69. public IEnumerator GetEnumerator () {
  70. return eventList.GetEnumerator ();
  71. }
  72. public int IndexOf (EventDescriptor value) {
  73. return eventList.IndexOf (value);
  74. }
  75. public void Insert (int index, EventDescriptor value) {
  76. eventList.Insert (index, value);
  77. }
  78. public void Remove (EventDescriptor value) {
  79. eventList.Remove (value);
  80. }
  81. public void RemoveAt (int index) {
  82. eventList.RemoveAt (index);
  83. }
  84. public virtual EventDescriptorCollection Sort () {
  85. EventDescriptorCollection col = CloneCollection ();
  86. col.InternalSort ((IComparer) null);
  87. return col;
  88. }
  89. public virtual EventDescriptorCollection Sort (IComparer comparer) {
  90. EventDescriptorCollection col = CloneCollection ();
  91. col.InternalSort (comparer);
  92. return col;
  93. }
  94. public virtual EventDescriptorCollection Sort (string[] order) {
  95. EventDescriptorCollection col = CloneCollection ();
  96. col.InternalSort (order);
  97. return col;
  98. }
  99. public virtual EventDescriptorCollection Sort (string[] order, IComparer comparer) {
  100. EventDescriptorCollection col = CloneCollection ();
  101. ArrayList sorted = col.ExtractItems (order);
  102. col.InternalSort (comparer);
  103. sorted.AddRange (col.eventList);
  104. col.eventList = sorted;
  105. return col;
  106. }
  107. protected void InternalSort (IComparer comparer) {
  108. eventList.Sort (comparer);
  109. }
  110. protected void InternalSort (string[] order) {
  111. ArrayList sorted = ExtractItems (order);
  112. InternalSort ((IComparer) null);
  113. sorted.AddRange (eventList);
  114. eventList = sorted;
  115. }
  116. ArrayList ExtractItems (string[] names)
  117. {
  118. ArrayList sorted = new ArrayList (eventList.Count);
  119. object[] ext = new object [names.Length];
  120. for (int n=0; n<eventList.Count; n++)
  121. {
  122. EventDescriptor ed = (EventDescriptor) eventList[n];
  123. int i = Array.IndexOf (names, ed.Name);
  124. if (i != -1) {
  125. ext[i] = ed;
  126. eventList.RemoveAt (n);
  127. n--;
  128. }
  129. }
  130. foreach (object ob in ext)
  131. if (ob != null) sorted.Add (ob);
  132. return sorted;
  133. }
  134. private EventDescriptorCollection CloneCollection ()
  135. {
  136. EventDescriptorCollection col = new EventDescriptorCollection ();
  137. col.eventList = (ArrayList) eventList.Clone ();
  138. return col;
  139. }
  140. internal EventDescriptorCollection Filter (Attribute[] attributes)
  141. {
  142. EventDescriptorCollection col = new EventDescriptorCollection ();
  143. foreach (EventDescriptor ed in eventList)
  144. if (ed.Attributes.Contains (attributes))
  145. col.eventList.Add (ed);
  146. return col;
  147. }
  148. public int Count {
  149. get {
  150. return eventList.Count;
  151. }
  152. }
  153. public virtual EventDescriptor this[string name] {
  154. get { return Find (name, false); }
  155. }
  156. public virtual EventDescriptor this[int index] {
  157. get {
  158. return (EventDescriptor) eventList[index];
  159. }
  160. }
  161. // IList methods
  162. int IList.Add (object value) {
  163. return Add ((EventDescriptor) value);
  164. }
  165. bool IList.Contains (object value) {
  166. return Contains ((EventDescriptor) value);
  167. }
  168. int IList.IndexOf (object value) {
  169. return IndexOf ((EventDescriptor) value);
  170. }
  171. void IList.Insert (int index, object value) {
  172. Insert (index, (EventDescriptor) value);
  173. }
  174. void IList.Remove (object value) {
  175. Remove ((EventDescriptor) value);
  176. }
  177. bool IList.IsFixedSize {
  178. get { return false; }
  179. }
  180. bool IList.IsReadOnly {
  181. get { return false; }
  182. }
  183. object IList.this[int index] {
  184. get {
  185. return eventList[index];
  186. }
  187. set {
  188. eventList[index] = value;
  189. }
  190. }
  191. // ICollection methods
  192. void ICollection.CopyTo (Array array, int index) {
  193. eventList.CopyTo (array, index);
  194. }
  195. bool ICollection.IsSynchronized {
  196. get { return false; }
  197. }
  198. object ICollection.SyncRoot {
  199. get { return null; }
  200. }
  201. }
  202. }