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);
55 public PropertyDescriptorCollection (PropertyDescriptor[] properties, bool readOnly) : this (properties)
57 this.readOnly = readOnly;
60 private PropertyDescriptorCollection ()
64 public int Add (PropertyDescriptor value)
67 throw new NotSupportedException ();
69 properties.Add (value);
70 return properties.Count - 1;
73 int IList.Add (object value)
75 return Add ((PropertyDescriptor) value);
78 void IDictionary.Add (object key, object value)
80 if ((value as PropertyDescriptor) == null) {
81 throw new ArgumentException ("value");
84 Add ((PropertyDescriptor) value);
90 throw new NotSupportedException ();
95 #if !TARGET_JVM // DUAL_IFACE_CONFLICT
101 void IDictionary.Clear ()
107 public bool Contains (PropertyDescriptor value)
109 return properties.Contains (value);
112 #if TARGET_JVM // DUAL_IFACE_CONFLICT
113 public bool Contains (object value)
115 return Contains ((PropertyDescriptor) value);
119 bool IList.Contains (object value)
121 return Contains ((PropertyDescriptor) value);
124 bool IDictionary.Contains (object value)
126 return Contains ((PropertyDescriptor) value);
130 public void CopyTo (Array array, int index)
132 properties.CopyTo (array, index);
135 public virtual PropertyDescriptor Find (string name, bool ignoreCase)
138 throw new ArgumentNullException ("name");
140 for (int i = 0; i < properties.Count; ++i) {
141 PropertyDescriptor p = (PropertyDescriptor)properties [i];
143 if (0 == String.Compare (name, p.Name, StringComparison.OrdinalIgnoreCase))
147 if (0 == String.Compare (name, p.Name, StringComparison.Ordinal))
154 public virtual IEnumerator GetEnumerator ()
156 return properties.GetEnumerator ();
159 IEnumerator IEnumerable.GetEnumerator ()
161 return GetEnumerator ();
165 IDictionaryEnumerator IDictionary.GetEnumerator ()
167 throw new NotImplementedException ();
170 public int IndexOf (PropertyDescriptor value)
172 return properties.IndexOf (value);
175 int IList.IndexOf (object value)
177 return IndexOf ((PropertyDescriptor) value);
180 public void Insert (int index, PropertyDescriptor value)
183 throw new NotSupportedException ();
185 properties.Insert (index, value);
188 void IList.Insert (int index, object value)
190 Insert (index, (PropertyDescriptor) value);
193 public void Remove (PropertyDescriptor value)
196 throw new NotSupportedException ();
198 properties.Remove (value);
201 #if TARGET_JVM// DUAL_IFACE_CONFLICT
202 public void Remove (object value)
204 Remove ((PropertyDescriptor) value);
207 void IDictionary.Remove (object value)
209 Remove ((PropertyDescriptor) value);
212 void IList.Remove (object value)
214 Remove ((PropertyDescriptor) value);
217 public void RemoveAt (int index)
220 throw new NotSupportedException ();
222 properties.RemoveAt (index);
225 void IList.RemoveAt (int index)
230 private PropertyDescriptorCollection CloneCollection ()
232 PropertyDescriptorCollection col = new PropertyDescriptorCollection ();
233 col.properties = (ArrayList) properties.Clone ();
237 public virtual PropertyDescriptorCollection Sort ()
239 PropertyDescriptorCollection col = CloneCollection ();
240 col.InternalSort ((IComparer) null);
244 public virtual PropertyDescriptorCollection Sort (IComparer comparer)
246 PropertyDescriptorCollection col = CloneCollection ();
247 col.InternalSort (comparer);
251 public virtual PropertyDescriptorCollection Sort (string[] order)
253 PropertyDescriptorCollection col = CloneCollection ();
254 col.InternalSort (order);
258 public virtual PropertyDescriptorCollection Sort (string[] order, IComparer comparer)
260 PropertyDescriptorCollection col = CloneCollection ();
262 ArrayList sorted = col.ExtractItems (order);
263 col.InternalSort (comparer);
264 sorted.AddRange (col.properties);
265 col.properties = sorted;
267 col.InternalSort (comparer);
272 protected void InternalSort (IComparer ic)
275 ic = MemberDescriptor.DefaultComparer;
276 properties.Sort (ic);
279 protected void InternalSort (string [] order)
282 ArrayList sorted = ExtractItems (order);
283 InternalSort ((IComparer) null);
284 sorted.AddRange (properties);
287 InternalSort ((IComparer) null);
291 ArrayList ExtractItems (string[] names)
293 ArrayList sorted = new ArrayList (properties.Count);
294 object[] ext = new object [names.Length];
296 for (int n=0; n<properties.Count; n++)
298 PropertyDescriptor ed = (PropertyDescriptor) properties[n];
299 int i = Array.IndexOf (names, ed.Name);
302 properties.RemoveAt (n);
306 foreach (object ob in ext)
307 if (ob != null) sorted.Add (ob);
312 internal PropertyDescriptorCollection Filter (Attribute[] attributes)
314 ArrayList list = new ArrayList ();
315 foreach (PropertyDescriptor pd in properties) {
316 if (pd.Attributes.Contains (attributes)) {
320 PropertyDescriptor[] descriptors = new PropertyDescriptor[list.Count];
321 list.CopyTo (descriptors);
322 return new PropertyDescriptorCollection (descriptors, true);
325 #if TARGET_JVM //DUAL_IFACE_CONFLICT
326 public bool IsFixedSize
328 bool IDictionary.IsFixedSize
330 get {return ((IList)this).IsFixedSize;}
332 bool IList.IsFixedSize
340 #if TARGET_JVM //DUAL_IFACE_CONFLICT
341 public bool IsReadOnly
343 bool IDictionary.IsReadOnly
345 get {return ((IList)this).IsReadOnly;}
347 bool IList.IsReadOnly
356 bool ICollection.IsSynchronized
363 int ICollection.Count {
364 get { return Count; }
370 return properties.Count;
374 object ICollection.SyncRoot
381 ICollection IDictionary.Keys
384 string [] keys = new string [properties.Count];
386 foreach (PropertyDescriptor p in properties)
392 ICollection IDictionary.Values
395 return (ICollection) properties.Clone ();
399 object IDictionary.this [object key]
402 if (!(key is string))
404 return this [(string) key];
408 throw new NotSupportedException ();
411 if (!(key is string) || (value as PropertyDescriptor) == null)
412 throw new ArgumentException ();
413 int idx = properties.IndexOf (value);
415 Add ((PropertyDescriptor) value);
417 properties [idx] = value;
421 public virtual PropertyDescriptor this [string s]
424 return Find (s, false);
428 object IList.this [int index]
431 return properties [index];
435 throw new NotSupportedException ();
437 properties [index] = value;
441 public virtual PropertyDescriptor this [int index]
444 return (PropertyDescriptor) properties [index];