2 // System.ComponentModel.PropertyDescriptorCollection.cs
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
9 // (C) Rodrigo Moya, 2002
10 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
11 // (C) 2003 Andreas Nahr
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections;
35 namespace System.ComponentModel
38 /// Represents a collection of PropertyDescriptor objects.
40 public class PropertyDescriptorCollection : IList, ICollection, IEnumerable, IDictionary
42 public static readonly PropertyDescriptorCollection Empty = new PropertyDescriptorCollection (null, true);
43 private ArrayList properties;
44 private bool readOnly;
46 public PropertyDescriptorCollection (PropertyDescriptor[] properties)
48 this.properties = new ArrayList ();
49 if (properties == null)
52 this.properties.AddRange (properties);
60 PropertyDescriptorCollection (PropertyDescriptor[] properties, bool readOnly) : this (properties)
62 this.readOnly = readOnly;
65 private PropertyDescriptorCollection ()
69 public int Add (PropertyDescriptor value)
72 throw new NotSupportedException ();
74 properties.Add (value);
75 return properties.Count - 1;
78 int IList.Add (object value)
80 return Add ((PropertyDescriptor) value);
83 void IDictionary.Add (object key, object value)
85 if ((value as PropertyDescriptor) == null) {
86 throw new ArgumentException ("value");
89 Add ((PropertyDescriptor) value);
95 throw new NotSupportedException ();
100 #if !TARGET_JVM // DUAL_IFACE_CONFLICT
106 void IDictionary.Clear ()
112 public bool Contains (PropertyDescriptor value)
114 return properties.Contains (value);
117 #if TARGET_JVM // DUAL_IFACE_CONFLICT
118 public bool Contains (object value)
120 return Contains ((PropertyDescriptor) value);
124 bool IList.Contains (object value)
126 return Contains ((PropertyDescriptor) value);
129 bool IDictionary.Contains (object value)
131 return Contains ((PropertyDescriptor) value);
135 public void CopyTo (Array array, int index)
137 properties.CopyTo (array, index);
140 public virtual PropertyDescriptor Find (string name, bool ignoreCase)
143 throw new ArgumentNullException ("name");
149 foreach (PropertyDescriptor p in properties)
151 if (String.Equals (name, p.Name))
157 foreach (PropertyDescriptor p in properties) {
158 if (0 == String.Compare (name, p.Name, ignoreCase))
164 public virtual IEnumerator GetEnumerator ()
166 return properties.GetEnumerator ();
170 IDictionaryEnumerator IDictionary.GetEnumerator ()
172 throw new NotImplementedException ();
176 IEnumerator IEnumerable.GetEnumerator ()
178 return GetEnumerator ();
181 public int IndexOf (PropertyDescriptor value)
183 return properties.IndexOf (value);
186 int IList.IndexOf (object value)
188 return IndexOf ((PropertyDescriptor) value);
191 public void Insert (int index, PropertyDescriptor value)
194 throw new NotSupportedException ();
196 properties.Insert (index, value);
199 void IList.Insert (int index, object value)
201 Insert (index, (PropertyDescriptor) value);
204 public void Remove (PropertyDescriptor value)
207 throw new NotSupportedException ();
209 properties.Remove (value);
212 #if TARGET_JVM// DUAL_IFACE_CONFLICT
213 public void Remove (object value)
215 Remove ((PropertyDescriptor) value);
218 void IDictionary.Remove (object value)
220 Remove ((PropertyDescriptor) value);
223 void IList.Remove (object value)
225 Remove ((PropertyDescriptor) value);
228 public void RemoveAt (int index)
231 throw new NotSupportedException ();
233 properties.RemoveAt (index);
236 void IList.RemoveAt (int index)
241 private PropertyDescriptorCollection CloneCollection ()
243 PropertyDescriptorCollection col = new PropertyDescriptorCollection ();
244 col.properties = (ArrayList) properties.Clone ();
248 public virtual PropertyDescriptorCollection Sort ()
250 PropertyDescriptorCollection col = CloneCollection ();
251 col.InternalSort ((IComparer) null);
255 public virtual PropertyDescriptorCollection Sort (IComparer comparer)
257 PropertyDescriptorCollection col = CloneCollection ();
258 col.InternalSort (comparer);
262 public virtual PropertyDescriptorCollection Sort (string[] order)
264 PropertyDescriptorCollection col = CloneCollection ();
265 col.InternalSort (order);
269 public virtual PropertyDescriptorCollection Sort (string[] order, IComparer comparer)
271 PropertyDescriptorCollection col = CloneCollection ();
272 ArrayList sorted = col.ExtractItems (order);
273 col.InternalSort (comparer);
274 sorted.AddRange (col.properties);
275 col.properties = sorted;
279 protected void InternalSort (IComparer ic)
281 properties.Sort (ic);
284 protected void InternalSort (string [] order)
286 ArrayList sorted = ExtractItems (order);
287 InternalSort ((IComparer) null);
288 sorted.AddRange (properties);
292 ArrayList ExtractItems (string[] names)
294 ArrayList sorted = new ArrayList (properties.Count);
295 object[] ext = new object [names.Length];
297 for (int n=0; n<properties.Count; n++)
299 PropertyDescriptor ed = (PropertyDescriptor) properties[n];
300 int i = Array.IndexOf (names, ed.Name);
303 properties.RemoveAt (n);
307 foreach (object ob in ext)
308 if (ob != null) sorted.Add (ob);
313 internal PropertyDescriptorCollection Filter (Attribute[] attributes)
315 ArrayList list = new ArrayList ();
316 foreach (PropertyDescriptor pd in properties) {
317 if (pd.Attributes.Contains (attributes)) {
321 PropertyDescriptor[] descriptors = new PropertyDescriptor[list.Count];
322 list.CopyTo (descriptors);
323 return new PropertyDescriptorCollection (descriptors, true);
326 #if TARGET_JVM //DUAL_IFACE_CONFLICT
327 public bool IsFixedSize
329 bool IDictionary.IsFixedSize
331 get {return ((IList)this).IsFixedSize;}
333 bool IList.IsFixedSize
345 #if TARGET_JVM //DUAL_IFACE_CONFLICT
346 public bool IsReadOnly
348 bool IDictionary.IsReadOnly
350 get {return ((IList)this).IsReadOnly;}
352 bool IList.IsReadOnly
361 bool ICollection.IsSynchronized
368 int ICollection.Count {
369 get { return Count; }
375 return properties.Count;
379 object ICollection.SyncRoot
386 ICollection IDictionary.Keys
389 string [] keys = new string [properties.Count];
391 foreach (PropertyDescriptor p in properties)
397 ICollection IDictionary.Values
400 return (ICollection) properties.Clone ();
404 object IDictionary.this [object key]
407 if (!(key is string))
409 return this [(string) key];
413 throw new NotSupportedException ();
416 if (!(key is string) || (value as PropertyDescriptor) == null)
417 throw new ArgumentException ();
418 int idx = properties.IndexOf (value);
420 Add ((PropertyDescriptor) value);
422 properties [idx] = value;
426 public virtual PropertyDescriptor this [string s]
429 return Find (s, false);
433 object IList.this [int index]
436 return properties [index];
440 throw new NotSupportedException ();
442 properties [index] = value;
446 public virtual PropertyDescriptor this [int index]
449 return (PropertyDescriptor) properties [index];