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)
63 EventDescriptorCollection (EventDescriptor
[] events
, bool readOnly
)
65 this.isReadOnly
= readOnly
;
69 for (int i
= 0; i
< events
.Length
; i
++)
73 public int Add (EventDescriptor
value)
76 throw new NotSupportedException ("The collection is read-only");
77 return eventList
.Add (value);
88 throw new NotSupportedException ("The collection is read-only");
92 public bool Contains (EventDescriptor
value) {
93 return eventList
.Contains (value);
96 public virtual EventDescriptor
Find (string name
, bool ignoreCase
)
98 foreach (EventDescriptor e
in eventList
) {
101 if (0 == String
.Compare (name
, e
.Name
, StringComparison
.OrdinalIgnoreCase
))
104 if (0 == String
.Compare (name
, e
.Name
, StringComparison
.Ordinal
))
108 if (0 == String
.Compare (name
, e
.Name
, ignoreCase
, CultureInfo
.InvariantCulture
))
115 IEnumerator IEnumerable
.GetEnumerator () {
116 return GetEnumerator ();
119 public IEnumerator
GetEnumerator () {
120 return eventList
.GetEnumerator ();
123 public int IndexOf (EventDescriptor
value) {
124 return eventList
.IndexOf (value);
127 public void Insert (int index
, EventDescriptor
value)
130 throw new NotSupportedException ("The collection is read-only");
131 eventList
.Insert (index
, value);
134 public void Remove (EventDescriptor
value)
137 throw new NotSupportedException ("The collection is read-only");
138 eventList
.Remove (value);
141 void IList
.RemoveAt (int index
)
146 public void RemoveAt (int index
)
149 throw new NotSupportedException ("The collection is read-only");
150 eventList
.RemoveAt (index
);
153 public virtual EventDescriptorCollection
Sort () {
154 EventDescriptorCollection col
= CloneCollection ();
155 col
.InternalSort ((IComparer
) null);
159 public virtual EventDescriptorCollection
Sort (IComparer comparer
) {
160 EventDescriptorCollection col
= CloneCollection ();
161 col
.InternalSort (comparer
);
165 public virtual EventDescriptorCollection
Sort (string[] order
) {
166 EventDescriptorCollection col
= CloneCollection ();
167 col
.InternalSort (order
);
171 public virtual EventDescriptorCollection
Sort (string[] order
, IComparer comparer
) {
172 EventDescriptorCollection col
= CloneCollection ();
174 ArrayList sorted
= col
.ExtractItems (order
);
175 col
.InternalSort (comparer
);
176 sorted
.AddRange (col
.eventList
);
177 col
.eventList
= sorted
;
179 col
.InternalSort (comparer
);
184 protected void InternalSort (IComparer comparer
) {
185 if (comparer
== null)
186 comparer
= MemberDescriptor
.DefaultComparer
;
187 eventList
.Sort (comparer
);
190 protected void InternalSort (string[] order
) {
192 ArrayList sorted
= ExtractItems (order
);
193 InternalSort ((IComparer
) null);
194 sorted
.AddRange (eventList
);
197 InternalSort ((IComparer
) null);
201 ArrayList
ExtractItems (string[] names
)
203 ArrayList sorted
= new ArrayList (eventList
.Count
);
204 object[] ext
= new object [names
.Length
];
206 for (int n
=0; n
<eventList
.Count
; n
++)
208 EventDescriptor ed
= (EventDescriptor
) eventList
[n
];
209 int i
= Array
.IndexOf (names
, ed
.Name
);
212 eventList
.RemoveAt (n
);
216 foreach (object ob
in ext
)
217 if (ob
!= null) sorted
.Add (ob
);
222 private EventDescriptorCollection
CloneCollection ()
224 EventDescriptorCollection col
= new EventDescriptorCollection ();
225 col
.eventList
= (ArrayList
) eventList
.Clone ();
229 internal EventDescriptorCollection
Filter (Attribute
[] attributes
)
231 EventDescriptorCollection col
= new EventDescriptorCollection ();
232 foreach (EventDescriptor ed
in eventList
)
233 if (ed
.Attributes
.Contains (attributes
))
234 col
.eventList
.Add (ed
);
238 int ICollection
.Count
{
239 get { return Count; }
244 return eventList
.Count
;
248 public virtual EventDescriptor
this[string name
] {
249 get { return Find (name, false); }
252 public virtual EventDescriptor
this[int index
] {
254 return (EventDescriptor
) eventList
[index
];
260 int IList
.Add (object value) {
261 return Add ((EventDescriptor
) value);
264 bool IList
.Contains (object value) {
265 return Contains ((EventDescriptor
) value);
268 int IList
.IndexOf (object value) {
269 return IndexOf ((EventDescriptor
) value);
272 void IList
.Insert (int index
, object value) {
273 Insert (index
, (EventDescriptor
) value);
276 void IList
.Remove (object value) {
277 Remove ((EventDescriptor
) value);
280 bool IList
.IsFixedSize
{
290 bool IList
.IsReadOnly
{
291 get { return isReadOnly; }
294 object IList
.this[int index
] {
296 return eventList
[index
];
300 throw new NotSupportedException ("The collection is read-only");
301 eventList
[index
] = value;
305 // ICollection methods
307 void ICollection
.CopyTo (Array array
, int index
) {
308 eventList
.CopyTo (array
, index
);
311 bool ICollection
.IsSynchronized
{
312 get { return false; }
315 object ICollection
.SyncRoot
{