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