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