2 // System.ComponentModel.EventDescriptorCollection.cs
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Runtime.InteropServices;
36 namespace System.ComponentModel
39 public class EventDescriptorCollection : IList, ICollection, IEnumerable
41 private ArrayList eventList = new ArrayList ();
43 public static readonly EventDescriptorCollection Empty = new EventDescriptorCollection ();
45 private EventDescriptorCollection ()
49 internal EventDescriptorCollection (ArrayList list)
54 public EventDescriptorCollection (EventDescriptor[] events)
59 for (int i = 0; i < events.Length; i++)
63 public int Add (EventDescriptor value) {
64 return eventList.Add (value);
67 public void Clear () {
71 public bool Contains (EventDescriptor value) {
72 return eventList.Contains (value);
75 public virtual EventDescriptor Find (string name, bool ignoreCase)
77 foreach (EventDescriptor e in eventList) {
78 if (0 == String.Compare (name, e.Name, ignoreCase))
84 public IEnumerator GetEnumerator () {
85 return eventList.GetEnumerator ();
88 public int IndexOf (EventDescriptor value) {
89 return eventList.IndexOf (value);
92 public void Insert (int index, EventDescriptor value) {
93 eventList.Insert (index, value);
96 public void Remove (EventDescriptor value) {
97 eventList.Remove (value);
100 public void RemoveAt (int index) {
101 eventList.RemoveAt (index);
104 public virtual EventDescriptorCollection Sort () {
105 EventDescriptorCollection col = CloneCollection ();
106 col.InternalSort ((IComparer) null);
110 public virtual EventDescriptorCollection Sort (IComparer comparer) {
111 EventDescriptorCollection col = CloneCollection ();
112 col.InternalSort (comparer);
116 public virtual EventDescriptorCollection Sort (string[] order) {
117 EventDescriptorCollection col = CloneCollection ();
118 col.InternalSort (order);
122 public virtual EventDescriptorCollection Sort (string[] order, IComparer comparer) {
123 EventDescriptorCollection col = CloneCollection ();
124 ArrayList sorted = col.ExtractItems (order);
125 col.InternalSort (comparer);
126 sorted.AddRange (col.eventList);
127 col.eventList = sorted;
131 protected void InternalSort (IComparer comparer) {
132 eventList.Sort (comparer);
135 protected void InternalSort (string[] order) {
136 ArrayList sorted = ExtractItems (order);
137 InternalSort ((IComparer) null);
138 sorted.AddRange (eventList);
142 ArrayList ExtractItems (string[] names)
144 ArrayList sorted = new ArrayList (eventList.Count);
145 object[] ext = new object [names.Length];
147 for (int n=0; n<eventList.Count; n++)
149 EventDescriptor ed = (EventDescriptor) eventList[n];
150 int i = Array.IndexOf (names, ed.Name);
153 eventList.RemoveAt (n);
157 foreach (object ob in ext)
158 if (ob != null) sorted.Add (ob);
163 private EventDescriptorCollection CloneCollection ()
165 EventDescriptorCollection col = new EventDescriptorCollection ();
166 col.eventList = (ArrayList) eventList.Clone ();
170 internal EventDescriptorCollection Filter (Attribute[] attributes)
172 EventDescriptorCollection col = new EventDescriptorCollection ();
173 foreach (EventDescriptor ed in eventList)
174 if (ed.Attributes.Contains (attributes))
175 col.eventList.Add (ed);
181 return eventList.Count;
185 public virtual EventDescriptor this[string name] {
186 get { return Find (name, false); }
189 public virtual EventDescriptor this[int index] {
191 return (EventDescriptor) eventList[index];
197 int IList.Add (object value) {
198 return Add ((EventDescriptor) value);
201 bool IList.Contains (object value) {
202 return Contains ((EventDescriptor) value);
205 int IList.IndexOf (object value) {
206 return IndexOf ((EventDescriptor) value);
209 void IList.Insert (int index, object value) {
210 Insert (index, (EventDescriptor) value);
213 void IList.Remove (object value) {
214 Remove ((EventDescriptor) value);
217 bool IList.IsFixedSize {
218 get { return false; }
221 bool IList.IsReadOnly {
222 get { return false; }
225 object IList.this[int index] {
227 return eventList[index];
230 eventList[index] = value;
234 // ICollection methods
236 void ICollection.CopyTo (Array array, int index) {
237 eventList.CopyTo (array, index);
240 bool ICollection.IsSynchronized {
241 get { return false; }
244 object ICollection.SyncRoot {