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.Globalization;
35 using System.Runtime.InteropServices;
37 namespace System.ComponentModel
40 public class EventDescriptorCollection : IList, ICollection, IEnumerable
42 private ArrayList eventList = new ArrayList ();
43 private bool isReadOnly;
44 public static readonly EventDescriptorCollection Empty = new EventDescriptorCollection (null, true);
46 private EventDescriptorCollection ()
50 internal EventDescriptorCollection (ArrayList list)
55 public EventDescriptorCollection (EventDescriptor[] events)
56 : this (events, false)
60 public EventDescriptorCollection (EventDescriptor[] events, bool readOnly)
62 this.isReadOnly = readOnly;
66 for (int i = 0; i < events.Length; i++)
70 public int Add (EventDescriptor value)
73 throw new NotSupportedException ("The collection is read-only");
74 return eventList.Add (value);
85 throw new NotSupportedException ("The collection is read-only");
89 public bool Contains (EventDescriptor value) {
90 return eventList.Contains (value);
93 public virtual EventDescriptor Find (string name, bool ignoreCase)
95 foreach (EventDescriptor e in eventList) {
97 if (0 == String.Compare (name, e.Name, StringComparison.OrdinalIgnoreCase))
100 if (0 == String.Compare (name, e.Name, StringComparison.Ordinal))
107 IEnumerator IEnumerable.GetEnumerator () {
108 return GetEnumerator ();
111 public IEnumerator GetEnumerator () {
112 return eventList.GetEnumerator ();
115 public int IndexOf (EventDescriptor value) {
116 return eventList.IndexOf (value);
119 public void Insert (int index, EventDescriptor value)
122 throw new NotSupportedException ("The collection is read-only");
123 eventList.Insert (index, value);
126 public void Remove (EventDescriptor value)
129 throw new NotSupportedException ("The collection is read-only");
130 eventList.Remove (value);
133 void IList.RemoveAt (int index)
138 public void RemoveAt (int index)
141 throw new NotSupportedException ("The collection is read-only");
142 eventList.RemoveAt (index);
145 public virtual EventDescriptorCollection Sort () {
146 EventDescriptorCollection col = CloneCollection ();
147 col.InternalSort ((IComparer) null);
151 public virtual EventDescriptorCollection Sort (IComparer comparer) {
152 EventDescriptorCollection col = CloneCollection ();
153 col.InternalSort (comparer);
157 public virtual EventDescriptorCollection Sort (string[] order) {
158 EventDescriptorCollection col = CloneCollection ();
159 col.InternalSort (order);
163 public virtual EventDescriptorCollection Sort (string[] order, IComparer comparer) {
164 EventDescriptorCollection col = CloneCollection ();
166 ArrayList sorted = col.ExtractItems (order);
167 col.InternalSort (comparer);
168 sorted.AddRange (col.eventList);
169 col.eventList = sorted;
171 col.InternalSort (comparer);
176 protected void InternalSort (IComparer comparer) {
177 if (comparer == null)
178 comparer = MemberDescriptor.DefaultComparer;
179 eventList.Sort (comparer);
182 protected void InternalSort (string[] order) {
184 ArrayList sorted = ExtractItems (order);
185 InternalSort ((IComparer) null);
186 sorted.AddRange (eventList);
189 InternalSort ((IComparer) null);
193 ArrayList ExtractItems (string[] names)
195 ArrayList sorted = new ArrayList (eventList.Count);
196 object[] ext = new object [names.Length];
198 for (int n=0; n<eventList.Count; n++)
200 EventDescriptor ed = (EventDescriptor) eventList[n];
201 int i = Array.IndexOf (names, ed.Name);
204 eventList.RemoveAt (n);
208 foreach (object ob in ext)
209 if (ob != null) sorted.Add (ob);
214 private EventDescriptorCollection CloneCollection ()
216 EventDescriptorCollection col = new EventDescriptorCollection ();
217 col.eventList = (ArrayList) eventList.Clone ();
221 internal EventDescriptorCollection Filter (Attribute[] attributes)
223 EventDescriptorCollection col = new EventDescriptorCollection ();
224 foreach (EventDescriptor ed in eventList)
225 if (ed.Attributes.Contains (attributes))
226 col.eventList.Add (ed);
230 int ICollection.Count {
231 get { return Count; }
236 return eventList.Count;
240 public virtual EventDescriptor this[string name] {
241 get { return Find (name, false); }
244 public virtual EventDescriptor this[int index] {
246 return (EventDescriptor) eventList[index];
252 int IList.Add (object value) {
253 return Add ((EventDescriptor) value);
256 bool IList.Contains (object value) {
257 return Contains ((EventDescriptor) value);
260 int IList.IndexOf (object value) {
261 return IndexOf ((EventDescriptor) value);
264 void IList.Insert (int index, object value) {
265 Insert (index, (EventDescriptor) value);
268 void IList.Remove (object value) {
269 Remove ((EventDescriptor) value);
272 bool IList.IsFixedSize {
278 bool IList.IsReadOnly {
279 get { return isReadOnly; }
282 object IList.this[int index] {
284 return eventList[index];
288 throw new NotSupportedException ("The collection is read-only");
289 eventList[index] = value;
293 // ICollection methods
295 void ICollection.CopyTo (Array array, int index) {
296 eventList.CopyTo (array, index);
299 bool ICollection.IsSynchronized {
300 get { return false; }
303 object ICollection.SyncRoot {