Fixed space indents to tabs to adhere to Mono coding guidelines.
[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 #if NET_2_0
61                 public
62 #endif
63                 EventDescriptorCollection (EventDescriptor[] events, bool readOnly) 
64                 {
65                         this.isReadOnly = readOnly;
66                         if (events == null)
67                                 return;
68
69                         for (int i = 0; i < events.Length; i++)
70                                 this.Add (events[i]);
71                 }
72
73                 public int Add (EventDescriptor value)
74                 {
75                         if (isReadOnly)
76                                 throw new NotSupportedException ("The collection is read-only");
77                         return eventList.Add (value);
78                 }
79
80                 void IList.Clear ()
81                 {
82                         Clear ();
83                 }
84
85                 public void Clear ()
86                 {
87                         if (isReadOnly)
88                                 throw new NotSupportedException ("The collection is read-only");
89                         eventList.Clear ();
90                 }
91
92                 public bool Contains (EventDescriptor value) {
93                         return eventList.Contains (value);
94                 }
95
96                 public virtual EventDescriptor Find (string name, bool ignoreCase) 
97                 {
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;
106                                 }
107 #else
108                                 if (0 == String.Compare (name, e.Name, ignoreCase, CultureInfo.InvariantCulture))
109                                         return e;
110 #endif
111                         }
112                         return null;
113                 }
114
115                 IEnumerator IEnumerable.GetEnumerator () {
116                         return GetEnumerator ();
117                 }
118
119                 public IEnumerator GetEnumerator () {
120                         return eventList.GetEnumerator ();
121                 }
122
123                 public int IndexOf (EventDescriptor value) {
124                         return eventList.IndexOf (value);
125                 }
126
127                 public void Insert (int index, EventDescriptor value)
128                 {
129                         if (isReadOnly)
130                                 throw new NotSupportedException ("The collection is read-only");
131                         eventList.Insert (index, value);
132                 }
133
134                 public void Remove (EventDescriptor value)
135                 {
136                         if (isReadOnly)
137                                 throw new NotSupportedException ("The collection is read-only");
138                         eventList.Remove (value);
139                 }
140
141                 void IList.RemoveAt (int index)
142                 {
143                         RemoveAt (index);
144                 }
145
146                 public void RemoveAt (int index)
147                 {
148                         if (isReadOnly)
149                                 throw new NotSupportedException ("The collection is read-only");
150                         eventList.RemoveAt (index);
151                 }
152
153                 public virtual EventDescriptorCollection Sort () {
154                         EventDescriptorCollection col = CloneCollection ();
155                         col.InternalSort ((IComparer) null);
156                         return col;
157                 }
158
159                 public virtual EventDescriptorCollection Sort (IComparer comparer) {
160                         EventDescriptorCollection col = CloneCollection ();
161                         col.InternalSort (comparer);
162                         return col;
163                 }
164
165                 public virtual EventDescriptorCollection Sort (string[] order) {
166                         EventDescriptorCollection col = CloneCollection ();
167                         col.InternalSort (order);
168                         return col;
169                 }
170
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);
180                         }
181                         return col;
182                 }
183
184                 protected void InternalSort (IComparer comparer) {
185                         if (comparer == null)
186                                 comparer = MemberDescriptor.DefaultComparer;
187                         eventList.Sort (comparer);
188                 }
189
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);
198                         }
199                 }
200                 
201                 ArrayList ExtractItems (string[] names)
202                 {
203                         ArrayList sorted = new ArrayList (eventList.Count);
204                         object[] ext = new object [names.Length];
205                         
206                         for (int n=0; n<eventList.Count; n++)
207                         {
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--;
214                                 }
215                         }
216                         foreach (object ob in ext)
217                                 if (ob != null) sorted.Add (ob);
218                                 
219                         return sorted;
220                 }
221                 
222                 private EventDescriptorCollection CloneCollection ()
223                 {
224                         EventDescriptorCollection col = new EventDescriptorCollection ();
225                         col.eventList = (ArrayList) eventList.Clone ();
226                         return col;
227                 }
228                 
229                 internal EventDescriptorCollection Filter (Attribute[] attributes)
230                 {
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;
236                 }
237                 
238                 int ICollection.Count {
239                         get { return Count; }
240                 }
241
242                 public int Count {
243                         get {
244                                 return eventList.Count;
245                         }
246                 }
247
248                  public virtual EventDescriptor this[string name] {
249                          get { return Find (name, false); }
250                  }
251
252                 public virtual EventDescriptor this[int index] {
253                         get {
254                                 return (EventDescriptor) eventList[index];
255                         }
256                 }
257
258                 // IList methods
259
260                 int IList.Add (object value) {
261                         return Add ((EventDescriptor) value);
262                 }
263
264                 bool IList.Contains (object value) {
265                         return Contains ((EventDescriptor) value);
266                 }
267
268                 int IList.IndexOf (object value) {
269                         return IndexOf ((EventDescriptor) value);
270                 }
271
272                 void IList.Insert (int index, object value) {
273                         Insert (index, (EventDescriptor) value);
274                 }
275
276                 void IList.Remove (object value) {
277                         Remove ((EventDescriptor) value);
278                 }
279
280                 bool IList.IsFixedSize {
281                         get {
282 #if NET_2_0
283                                 return isReadOnly;
284 #else
285                                 return !isReadOnly;
286 #endif
287                         }
288                 }
289
290                 bool IList.IsReadOnly {
291                         get { return isReadOnly; }
292                 }
293
294                 object IList.this[int index] {
295                         get {
296                                 return eventList[index];
297                         }
298                         set {
299                                 if (isReadOnly)
300                                         throw new NotSupportedException ("The collection is read-only");
301                                 eventList[index] = value;
302                         }
303                 }
304
305                 // ICollection methods
306
307                 void ICollection.CopyTo (Array array, int index) {
308                         eventList.CopyTo (array, index);
309                 }
310
311                 bool ICollection.IsSynchronized {
312                         get { return false; }
313                 }
314
315                 object ICollection.SyncRoot {
316                         get { return null; }
317                 }
318         }
319 }