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);
71 public void Clear () {
75 public bool Contains (EventDescriptor value) {
76 return eventList.Contains (value);
79 public virtual EventDescriptor Find (string name, bool ignoreCase)
81 foreach (EventDescriptor e in eventList) {
82 if (0 == String.Compare (name, e.Name, ignoreCase))
88 IEnumerator IEnumerable.GetEnumerator () {
89 return GetEnumerator ();
92 public IEnumerator GetEnumerator () {
93 return eventList.GetEnumerator ();
96 public int IndexOf (EventDescriptor value) {
97 return eventList.IndexOf (value);
100 public void Insert (int index, EventDescriptor value) {
101 eventList.Insert (index, value);
104 public void Remove (EventDescriptor value) {
105 eventList.Remove (value);
108 void IList.RemoveAt (int index) {
112 public void RemoveAt (int index) {
113 eventList.RemoveAt (index);
116 public virtual EventDescriptorCollection Sort () {
117 EventDescriptorCollection col = CloneCollection ();
118 col.InternalSort ((IComparer) null);
122 public virtual EventDescriptorCollection Sort (IComparer comparer) {
123 EventDescriptorCollection col = CloneCollection ();
124 col.InternalSort (comparer);
128 public virtual EventDescriptorCollection Sort (string[] order) {
129 EventDescriptorCollection col = CloneCollection ();
130 col.InternalSort (order);
134 public virtual EventDescriptorCollection Sort (string[] order, IComparer comparer) {
135 EventDescriptorCollection col = CloneCollection ();
136 ArrayList sorted = col.ExtractItems (order);
137 col.InternalSort (comparer);
138 sorted.AddRange (col.eventList);
139 col.eventList = sorted;
143 protected void InternalSort (IComparer comparer) {
144 eventList.Sort (comparer);
147 protected void InternalSort (string[] order) {
148 ArrayList sorted = ExtractItems (order);
149 InternalSort ((IComparer) null);
150 sorted.AddRange (eventList);
154 ArrayList ExtractItems (string[] names)
156 ArrayList sorted = new ArrayList (eventList.Count);
157 object[] ext = new object [names.Length];
159 for (int n=0; n<eventList.Count; n++)
161 EventDescriptor ed = (EventDescriptor) eventList[n];
162 int i = Array.IndexOf (names, ed.Name);
165 eventList.RemoveAt (n);
169 foreach (object ob in ext)
170 if (ob != null) sorted.Add (ob);
175 private EventDescriptorCollection CloneCollection ()
177 EventDescriptorCollection col = new EventDescriptorCollection ();
178 col.eventList = (ArrayList) eventList.Clone ();
182 internal EventDescriptorCollection Filter (Attribute[] attributes)
184 EventDescriptorCollection col = new EventDescriptorCollection ();
185 foreach (EventDescriptor ed in eventList)
186 if (ed.Attributes.Contains (attributes))
187 col.eventList.Add (ed);
191 int ICollection.Count {
192 get { return Count; }
197 return eventList.Count;
201 public virtual EventDescriptor this[string name] {
202 get { return Find (name, false); }
205 public virtual EventDescriptor this[int index] {
207 return (EventDescriptor) eventList[index];
213 int IList.Add (object value) {
214 return Add ((EventDescriptor) value);
217 bool IList.Contains (object value) {
218 return Contains ((EventDescriptor) value);
221 int IList.IndexOf (object value) {
222 return IndexOf ((EventDescriptor) value);
225 void IList.Insert (int index, object value) {
226 Insert (index, (EventDescriptor) value);
229 void IList.Remove (object value) {
230 Remove ((EventDescriptor) value);
233 bool IList.IsFixedSize {
234 get { return false; }
237 bool IList.IsReadOnly {
238 get { return false; }
241 object IList.this[int index] {
243 return eventList[index];
246 eventList[index] = value;
250 // ICollection methods
252 void ICollection.CopyTo (Array array, int index) {
253 eventList.CopyTo (array, index);
256 bool ICollection.IsSynchronized {
257 get { return false; }
260 object ICollection.SyncRoot {