2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System / System.ComponentModel / EventDescriptorCollection.cs
blobbd15e720453725874fee61908f85b99253a5179a
1 //
2 // System.ComponentModel.EventDescriptorCollection.cs
3 //
4 // Authors:
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Ximian, Inc.
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:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
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
39 [ComVisible (true)]
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)
52 eventList = list;
55 public EventDescriptorCollection (EventDescriptor[] events)
56 : this (events, false)
60 #if NET_2_0
61 public
62 #endif
63 EventDescriptorCollection (EventDescriptor[] events, bool readOnly)
65 this.isReadOnly = readOnly;
66 if (events == null)
67 return;
69 for (int i = 0; i < events.Length; i++)
70 this.Add (events[i]);
73 public int Add (EventDescriptor value)
75 if (isReadOnly)
76 throw new NotSupportedException ("The collection is read-only");
77 return eventList.Add (value);
80 void IList.Clear ()
82 Clear ();
85 public void Clear ()
87 if (isReadOnly)
88 throw new NotSupportedException ("The collection is read-only");
89 eventList.Clear ();
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) {
99 #if NET_2_0
100 if (ignoreCase) {
101 if (0 == String.Compare (name, e.Name, StringComparison.OrdinalIgnoreCase))
102 return e;
103 } else {
104 if (0 == String.Compare (name, e.Name, StringComparison.Ordinal))
105 return e;
107 #else
108 if (0 == String.Compare (name, e.Name, ignoreCase, CultureInfo.InvariantCulture))
109 return e;
110 #endif
112 return null;
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)
129 if (isReadOnly)
130 throw new NotSupportedException ("The collection is read-only");
131 eventList.Insert (index, value);
134 public void Remove (EventDescriptor value)
136 if (isReadOnly)
137 throw new NotSupportedException ("The collection is read-only");
138 eventList.Remove (value);
141 void IList.RemoveAt (int index)
143 RemoveAt (index);
146 public void RemoveAt (int index)
148 if (isReadOnly)
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);
156 return col;
159 public virtual EventDescriptorCollection Sort (IComparer comparer) {
160 EventDescriptorCollection col = CloneCollection ();
161 col.InternalSort (comparer);
162 return col;
165 public virtual EventDescriptorCollection Sort (string[] order) {
166 EventDescriptorCollection col = CloneCollection ();
167 col.InternalSort (order);
168 return col;
171 public virtual EventDescriptorCollection Sort (string[] order, IComparer comparer) {
172 EventDescriptorCollection col = CloneCollection ();
173 if (order != null) {
174 ArrayList sorted = col.ExtractItems (order);
175 col.InternalSort (comparer);
176 sorted.AddRange (col.eventList);
177 col.eventList = sorted;
178 } else {
179 col.InternalSort (comparer);
181 return col;
184 protected void InternalSort (IComparer comparer) {
185 if (comparer == null)
186 comparer = MemberDescriptor.DefaultComparer;
187 eventList.Sort (comparer);
190 protected void InternalSort (string[] order) {
191 if (order != null) {
192 ArrayList sorted = ExtractItems (order);
193 InternalSort ((IComparer) null);
194 sorted.AddRange (eventList);
195 eventList = sorted;
196 } else {
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);
210 if (i != -1) {
211 ext[i] = ed;
212 eventList.RemoveAt (n);
213 n--;
216 foreach (object ob in ext)
217 if (ob != null) sorted.Add (ob);
219 return sorted;
222 private EventDescriptorCollection CloneCollection ()
224 EventDescriptorCollection col = new EventDescriptorCollection ();
225 col.eventList = (ArrayList) eventList.Clone ();
226 return col;
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);
235 return col;
238 int ICollection.Count {
239 get { return Count; }
242 public int Count {
243 get {
244 return eventList.Count;
248 public virtual EventDescriptor this[string name] {
249 get { return Find (name, false); }
252 public virtual EventDescriptor this[int index] {
253 get {
254 return (EventDescriptor) eventList[index];
258 // IList methods
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 {
281 get {
282 #if NET_2_0
283 return isReadOnly;
284 #else
285 return !isReadOnly;
286 #endif
290 bool IList.IsReadOnly {
291 get { return isReadOnly; }
294 object IList.this[int index] {
295 get {
296 return eventList[index];
298 set {
299 if (isReadOnly)
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 {
316 get { return null; }