Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System / System.ComponentModel / EventDescriptorCollection.cs
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
10 //
11
12 //
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.
31 //
32
33 using System.Collections;
34 using System.Globalization;
35 using System.Runtime.InteropServices;
36
37 namespace System.ComponentModel
38 {
39         [ComVisible (true)]
40         public class EventDescriptorCollection : IList, ICollection, IEnumerable
41         {
42                 private ArrayList eventList = new ArrayList ();
43                 private bool isReadOnly;
44                 public static readonly EventDescriptorCollection Empty = new EventDescriptorCollection (null, true);
45                 
46                 private EventDescriptorCollection ()
47                 {
48                 }
49                 
50                 internal EventDescriptorCollection (ArrayList list)
51                 {
52                         eventList = list;
53                 }
54                 
55                 public EventDescriptorCollection (EventDescriptor[] events) 
56                         : this (events, false)
57                 {
58                 }
59
60                 public EventDescriptorCollection (EventDescriptor[] events, bool readOnly) 
61                 {
62                         this.isReadOnly = readOnly;
63                         if (events == null)
64                                 return;
65
66                         for (int i = 0; i < events.Length; i++)
67                                 this.Add (events[i]);
68                 }
69
70                 public int Add (EventDescriptor value)
71                 {
72                         if (isReadOnly)
73                                 throw new NotSupportedException ("The collection is read-only");
74                         return eventList.Add (value);
75                 }
76
77                 void IList.Clear ()
78                 {
79                         Clear ();
80                 }
81
82                 public void Clear ()
83                 {
84                         if (isReadOnly)
85                                 throw new NotSupportedException ("The collection is read-only");
86                         eventList.Clear ();
87                 }
88
89                 public bool Contains (EventDescriptor value) {
90                         return eventList.Contains (value);
91                 }
92
93                 public virtual EventDescriptor Find (string name, bool ignoreCase) 
94                 {
95                         foreach (EventDescriptor e in eventList) {
96                                 if (ignoreCase) {
97                                         if (0 == String.Compare (name, e.Name, StringComparison.OrdinalIgnoreCase))
98                                                 return e;
99                                 } else {
100                                         if (0 == String.Compare (name, e.Name, StringComparison.Ordinal))
101                                                 return e;
102                                 }
103                         }
104                         return null;
105                 }
106
107                 IEnumerator IEnumerable.GetEnumerator () {
108                         return GetEnumerator ();
109                 }
110
111                 public IEnumerator GetEnumerator () {
112                         return eventList.GetEnumerator ();
113                 }
114
115                 public int IndexOf (EventDescriptor value) {
116                         return eventList.IndexOf (value);
117                 }
118
119                 public void Insert (int index, EventDescriptor value)
120                 {
121                         if (isReadOnly)
122                                 throw new NotSupportedException ("The collection is read-only");
123                         eventList.Insert (index, value);
124                 }
125
126                 public void Remove (EventDescriptor value)
127                 {
128                         if (isReadOnly)
129                                 throw new NotSupportedException ("The collection is read-only");
130                         eventList.Remove (value);
131                 }
132
133                 void IList.RemoveAt (int index)
134                 {
135                         RemoveAt (index);
136                 }
137
138                 public void RemoveAt (int index)
139                 {
140                         if (isReadOnly)
141                                 throw new NotSupportedException ("The collection is read-only");
142                         eventList.RemoveAt (index);
143                 }
144
145                 public virtual EventDescriptorCollection Sort () {
146                         EventDescriptorCollection col = CloneCollection ();
147                         col.InternalSort ((IComparer) null);
148                         return col;
149                 }
150
151                 public virtual EventDescriptorCollection Sort (IComparer comparer) {
152                         EventDescriptorCollection col = CloneCollection ();
153                         col.InternalSort (comparer);
154                         return col;
155                 }
156
157                 public virtual EventDescriptorCollection Sort (string[] order) {
158                         EventDescriptorCollection col = CloneCollection ();
159                         col.InternalSort (order);
160                         return col;
161                 }
162
163                 public virtual EventDescriptorCollection Sort (string[] order, IComparer comparer) {
164                         EventDescriptorCollection col = CloneCollection ();
165                         if (order != null) {
166                                 ArrayList sorted = col.ExtractItems (order);
167                                 col.InternalSort (comparer);
168                                 sorted.AddRange (col.eventList);
169                                 col.eventList = sorted;
170                         } else {
171                                 col.InternalSort (comparer);
172                         }
173                         return col;
174                 }
175
176                 protected void InternalSort (IComparer comparer) {
177                         if (comparer == null)
178                                 comparer = MemberDescriptor.DefaultComparer;
179                         eventList.Sort (comparer);
180                 }
181
182                 protected void InternalSort (string[] order) {
183                         if (order != null) {
184                                 ArrayList sorted = ExtractItems (order);
185                                 InternalSort ((IComparer) null);
186                                 sorted.AddRange (eventList);
187                                 eventList = sorted;
188                         } else {
189                                 InternalSort ((IComparer) null);
190                         }
191                 }
192                 
193                 ArrayList ExtractItems (string[] names)
194                 {
195                         ArrayList sorted = new ArrayList (eventList.Count);
196                         object[] ext = new object [names.Length];
197                         
198                         for (int n=0; n<eventList.Count; n++)
199                         {
200                                 EventDescriptor ed = (EventDescriptor) eventList[n];
201                                 int i = Array.IndexOf (names, ed.Name);
202                                 if (i != -1) {
203                                         ext[i] = ed;
204                                         eventList.RemoveAt (n);
205                                         n--;
206                                 }
207                         }
208                         foreach (object ob in ext)
209                                 if (ob != null) sorted.Add (ob);
210                                 
211                         return sorted;
212                 }
213                 
214                 private EventDescriptorCollection CloneCollection ()
215                 {
216                         EventDescriptorCollection col = new EventDescriptorCollection ();
217                         col.eventList = (ArrayList) eventList.Clone ();
218                         return col;
219                 }
220                 
221                 internal EventDescriptorCollection Filter (Attribute[] attributes)
222                 {
223                         EventDescriptorCollection col = new EventDescriptorCollection ();
224                         foreach (EventDescriptor ed in eventList)
225                                 if (ed.Attributes.Contains (attributes))
226                                         col.eventList.Add (ed);
227                         return col;
228                 }
229                 
230                 int ICollection.Count {
231                         get { return Count; }
232                 }
233
234                 public int Count {
235                         get {
236                                 return eventList.Count;
237                         }
238                 }
239
240                  public virtual EventDescriptor this[string name] {
241                          get { return Find (name, false); }
242                  }
243
244                 public virtual EventDescriptor this[int index] {
245                         get {
246                                 return (EventDescriptor) eventList[index];
247                         }
248                 }
249
250                 // IList methods
251
252                 int IList.Add (object value) {
253                         return Add ((EventDescriptor) value);
254                 }
255
256                 bool IList.Contains (object value) {
257                         return Contains ((EventDescriptor) value);
258                 }
259
260                 int IList.IndexOf (object value) {
261                         return IndexOf ((EventDescriptor) value);
262                 }
263
264                 void IList.Insert (int index, object value) {
265                         Insert (index, (EventDescriptor) value);
266                 }
267
268                 void IList.Remove (object value) {
269                         Remove ((EventDescriptor) value);
270                 }
271
272                 bool IList.IsFixedSize {
273                         get {
274                                 return isReadOnly;
275                         }
276                 }
277
278                 bool IList.IsReadOnly {
279                         get { return isReadOnly; }
280                 }
281
282                 object IList.this[int index] {
283                         get {
284                                 return eventList[index];
285                         }
286                         set {
287                                 if (isReadOnly)
288                                         throw new NotSupportedException ("The collection is read-only");
289                                 eventList[index] = value;
290                         }
291                 }
292
293                 // ICollection methods
294
295                 void ICollection.CopyTo (Array array, int index) {
296                         eventList.CopyTo (array, index);
297                 }
298
299                 bool ICollection.IsSynchronized {
300                         get { return false; }
301                 }
302
303                 object ICollection.SyncRoot {
304                         get { return null; }
305                 }
306         }
307 }