2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System / System.ComponentModel / PropertyDescriptorCollection.cs
1 //
2 // System.ComponentModel.PropertyDescriptorCollection.cs
3 //
4 // Authors:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 //
9 // (C) Rodrigo Moya, 2002
10 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
11 // (C) 2003 Andreas Nahr
12 //
13
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34 using System.Collections;
35 namespace System.ComponentModel
36 {
37         /// <summary>
38         /// Represents a collection of PropertyDescriptor objects.
39         /// </summary>
40         //[DefaultMember ("Item")]
41         public class PropertyDescriptorCollection : IList, ICollection, IEnumerable, IDictionary
42         {
43                 public static readonly PropertyDescriptorCollection Empty = new PropertyDescriptorCollection ((ArrayList)null);
44                 ArrayList properties;
45
46                 internal PropertyDescriptorCollection (ArrayList list)
47                 {
48                         properties = list;
49                 }
50                 
51                 public PropertyDescriptorCollection (PropertyDescriptor[] properties)
52                 {
53                         this.properties = new ArrayList ();
54                         if (properties == null)
55                                 return;
56
57                         this.properties.AddRange (properties);
58                 }
59                 
60                 private PropertyDescriptorCollection ()
61                 {
62                 }
63
64                 public int Add (PropertyDescriptor value)
65                 {
66                         properties.Add (value);
67                         return properties.Count - 1;
68                 }
69
70                 int IList.Add (object value)
71                 {
72                         return Add ((PropertyDescriptor) value);
73                 }
74
75                 void IDictionary.Add (object key, object value)
76                 {
77                         Add ((PropertyDescriptor) value);
78                 }
79
80                 public void Clear ()
81                 {
82                         properties.Clear ();
83                 }
84
85                 void IList.Clear ()
86                 {
87                         Clear ();
88                 }
89
90                 void IDictionary.Clear ()
91                 {
92                         Clear ();
93                 }
94
95                 public bool Contains (PropertyDescriptor value)
96                 {
97                         return properties.Contains (value);
98                 }
99
100                 bool IList.Contains (object value)
101                 {
102                         return Contains ((PropertyDescriptor) value);
103                 }
104
105                 bool IDictionary.Contains (object value)
106                 {
107                         return Contains ((PropertyDescriptor) value);
108                 }
109
110                 public void CopyTo (Array array, int index)
111                 {
112                         properties.CopyTo (array, index);
113                 }
114
115                 public virtual PropertyDescriptor Find (string name, bool ignoreCase)
116                 {
117                         foreach (PropertyDescriptor p in properties) {
118                                 if (0 == String.Compare (name, p.Name, ignoreCase))
119                                         return p;
120                         }
121                         return null;
122                 }
123
124                 public virtual IEnumerator GetEnumerator ()
125                 {
126                         return properties.GetEnumerator ();
127                 }
128
129                 [MonoTODO]
130                 IDictionaryEnumerator IDictionary.GetEnumerator ()
131                 {
132                         throw new NotImplementedException ();
133                 }
134
135                 public int IndexOf (PropertyDescriptor value)
136                 {
137                         return properties.IndexOf (value);
138                 }
139
140                 int IList.IndexOf (object value)
141                 {
142                         return IndexOf ((PropertyDescriptor) value);
143                 }
144
145                 public void Insert (int index, PropertyDescriptor value)
146                 {
147                         properties.Insert (index, value);
148                 }
149
150                 void IList.Insert (int index, object value)
151                 {
152                         Insert (index, (PropertyDescriptor) value);
153                 }
154
155                 public void Remove (PropertyDescriptor value)
156                 {
157                         properties.Remove (value);
158                 }
159
160                 void IDictionary.Remove (object value)
161                 {
162                         Remove ((PropertyDescriptor) value);
163                 }
164
165                 void IList.Remove (object value)
166                 {
167                         Remove ((PropertyDescriptor) value);
168                 }
169
170                 public void RemoveAt (int index)
171                 {
172                         properties.RemoveAt (index);
173                 }
174
175                 void IList.RemoveAt (int index)
176                 {
177                         RemoveAt (index);
178                 }
179
180                 private PropertyDescriptorCollection CloneCollection ()
181                 {
182                         PropertyDescriptorCollection col = new PropertyDescriptorCollection ();
183                         col.properties = (ArrayList) properties.Clone ();
184                         return col;
185                 }
186                 
187                 public virtual PropertyDescriptorCollection Sort ()
188                 {
189                         PropertyDescriptorCollection col = CloneCollection ();
190                         col.InternalSort ((IComparer) null);
191                         return col;
192                 }
193
194                 public virtual PropertyDescriptorCollection Sort (IComparer comparer)
195                 {
196                         PropertyDescriptorCollection col = CloneCollection ();
197                         col.InternalSort (comparer);
198                         return col;
199                 }
200
201                 public virtual PropertyDescriptorCollection Sort (string[] order) 
202                 {
203                         PropertyDescriptorCollection col = CloneCollection ();
204                         col.InternalSort (order);
205                         return col;
206                 }
207
208                 public virtual PropertyDescriptorCollection Sort (string[] order, IComparer comparer) 
209                 {
210                         PropertyDescriptorCollection col = CloneCollection ();
211                         ArrayList sorted = col.ExtractItems (order);
212                         col.InternalSort (comparer);
213                         sorted.AddRange (col.properties);
214                         col.properties = sorted;
215                         return col;
216                 }
217
218                 protected void InternalSort (IComparer ic)
219                 {
220                         properties.Sort (ic);
221                 }
222
223                 protected void InternalSort (string [] order)
224                 {
225                         ArrayList sorted = ExtractItems (order);
226                         InternalSort ((IComparer) null);
227                         sorted.AddRange (properties);
228                         properties = sorted;
229                 }
230                 
231                 ArrayList ExtractItems (string[] names)
232                 {
233                         ArrayList sorted = new ArrayList (properties.Count);
234                         object[] ext = new object [names.Length];
235                         
236                         for (int n=0; n<properties.Count; n++)
237                         {
238                                 PropertyDescriptor ed = (PropertyDescriptor) properties[n];
239                                 int i = Array.IndexOf (names, ed.Name);
240                                 if (i != -1) {
241                                         ext[i] = ed;
242                                         properties.RemoveAt (n);
243                                         n--;
244                                 }
245                         }
246                         foreach (object ob in ext)
247                                 if (ob != null) sorted.Add (ob);
248                                 
249                         return sorted;
250                 }
251                 
252                 internal PropertyDescriptorCollection Filter (Attribute[] attributes)
253                 {
254                         ArrayList list = new ArrayList ();
255                         foreach (PropertyDescriptor pd in properties)
256                                 if (pd.Attributes.Contains (attributes))
257                                         list.Add (pd);
258                                         
259                         return new PropertyDescriptorCollection (list);
260                 }
261                 
262                 bool IDictionary.IsFixedSize
263                 {
264                         get {
265                                 return true;
266                         }
267                 }
268
269                 bool IList.IsFixedSize
270                 {
271                         get {
272                                 return true;
273                         }
274                 }
275
276                 bool IList.IsReadOnly
277                 {
278                         get {
279                                 return false;
280                         }
281                 }
282
283                 bool IDictionary.IsReadOnly
284                 {
285                         get 
286                         {
287                                 return false;
288                         }
289                 }
290
291                 bool ICollection.IsSynchronized
292                 {
293                         get {
294                                 return false;
295                         }
296                 }
297
298                 public int Count
299                 {
300                         get {
301                                 return properties.Count;
302                         }
303                 }
304
305                 object ICollection.SyncRoot
306                 {
307                         get {
308                                 return null;
309                         }
310                 }
311
312                 ICollection IDictionary.Keys
313                 {
314                         get {
315                                 string [] keys = new string [properties.Count];
316                                 int i = 0;
317                                 foreach (PropertyDescriptor p in properties)
318                                         keys [i++] = p.Name;
319                                 return keys;
320                         }
321                 }
322
323                 ICollection IDictionary.Values
324                 {
325                         get {
326                                 return (ICollection) properties.Clone ();
327                         }
328                 }
329
330                 object IDictionary.this [object key]
331                 {
332                         get {
333                                 if (!(key is string))
334                                         return null;
335                                 return this [(string) key];
336                         }
337                         set {
338                                 if (!(key is string) || (value as PropertyDescriptor) == null)
339                                         throw new ArgumentException ();
340                                 int idx = properties.IndexOf (value);
341                                 if (idx == -1)
342                                         Add ((PropertyDescriptor) value);
343                                 else
344                                         properties [idx] = value;
345                         }
346                 }
347
348                 public virtual PropertyDescriptor this [string s]
349                 {
350                         get {
351                                 return Find (s, false);
352                         }
353                 }
354
355                 object IList.this [int index]
356                 {
357                         get {
358                                 return properties [index];
359                         }
360                         set {
361                                 properties [index] = value;
362                         }
363                 }
364
365                 public virtual PropertyDescriptor this [int index]
366                 {
367                         get {
368                                 return (PropertyDescriptor) properties [index];
369                         }
370                 }
371         }
372 }
373