**** Merged from MCS ****
[mono-project.git] / mcs / class / System / System.ComponentModel / PropertyDescriptorCollection.cs
blob8e363144afe4ce2de96e70d8d18d4a5204573681
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
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.
34 using System.Collections;
35 namespace System.ComponentModel
37 /// <summary>
38 /// Represents a collection of PropertyDescriptor objects.
39 /// </summary>
40 //[DefaultMember ("Item")]
41 public class PropertyDescriptorCollection : IList, ICollection, IEnumerable, IDictionary
43 public static readonly PropertyDescriptorCollection Empty = new PropertyDescriptorCollection ((ArrayList)null);
44 ArrayList properties;
46 internal PropertyDescriptorCollection (ArrayList list)
48 properties = list;
51 public PropertyDescriptorCollection (PropertyDescriptor[] properties)
53 this.properties = new ArrayList ();
54 if (properties == null)
55 return;
57 this.properties.AddRange (properties);
60 private PropertyDescriptorCollection ()
64 public int Add (PropertyDescriptor value)
66 properties.Add (value);
67 return properties.Count - 1;
70 int IList.Add (object value)
72 return Add ((PropertyDescriptor) value);
75 void IDictionary.Add (object key, object value)
77 Add ((PropertyDescriptor) value);
80 public void Clear ()
82 properties.Clear ();
85 void IList.Clear ()
87 Clear ();
90 void IDictionary.Clear ()
92 Clear ();
95 public bool Contains (PropertyDescriptor value)
97 return properties.Contains (value);
100 bool IList.Contains (object value)
102 return Contains ((PropertyDescriptor) value);
105 bool IDictionary.Contains (object value)
107 return Contains ((PropertyDescriptor) value);
110 public void CopyTo (Array array, int index)
112 properties.CopyTo (array, index);
115 public virtual PropertyDescriptor Find (string name, bool ignoreCase)
117 foreach (PropertyDescriptor p in properties) {
118 if (0 == String.Compare (name, p.Name, ignoreCase))
119 return p;
121 return null;
124 public virtual IEnumerator GetEnumerator ()
126 return properties.GetEnumerator ();
129 [MonoTODO]
130 IDictionaryEnumerator IDictionary.GetEnumerator ()
132 throw new NotImplementedException ();
135 public int IndexOf (PropertyDescriptor value)
137 return properties.IndexOf (value);
140 int IList.IndexOf (object value)
142 return IndexOf ((PropertyDescriptor) value);
145 public void Insert (int index, PropertyDescriptor value)
147 properties.Insert (index, value);
150 void IList.Insert (int index, object value)
152 Insert (index, (PropertyDescriptor) value);
155 public void Remove (PropertyDescriptor value)
157 properties.Remove (value);
160 void IDictionary.Remove (object value)
162 Remove ((PropertyDescriptor) value);
165 void IList.Remove (object value)
167 Remove ((PropertyDescriptor) value);
170 public void RemoveAt (int index)
172 properties.RemoveAt (index);
175 void IList.RemoveAt (int index)
177 RemoveAt (index);
180 private PropertyDescriptorCollection CloneCollection ()
182 PropertyDescriptorCollection col = new PropertyDescriptorCollection ();
183 col.properties = (ArrayList) properties.Clone ();
184 return col;
187 public virtual PropertyDescriptorCollection Sort ()
189 PropertyDescriptorCollection col = CloneCollection ();
190 col.InternalSort ((IComparer) null);
191 return col;
194 public virtual PropertyDescriptorCollection Sort (IComparer comparer)
196 PropertyDescriptorCollection col = CloneCollection ();
197 col.InternalSort (comparer);
198 return col;
201 public virtual PropertyDescriptorCollection Sort (string[] order)
203 PropertyDescriptorCollection col = CloneCollection ();
204 col.InternalSort (order);
205 return col;
208 public virtual PropertyDescriptorCollection Sort (string[] order, IComparer comparer)
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;
218 protected void InternalSort (IComparer ic)
220 properties.Sort (ic);
223 protected void InternalSort (string [] order)
225 ArrayList sorted = ExtractItems (order);
226 InternalSort ((IComparer) null);
227 sorted.AddRange (properties);
228 properties = sorted;
231 ArrayList ExtractItems (string[] names)
233 ArrayList sorted = new ArrayList (properties.Count);
234 object[] ext = new object [names.Length];
236 for (int n=0; n<properties.Count; n++)
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--;
246 foreach (object ob in ext)
247 if (ob != null) sorted.Add (ob);
249 return sorted;
252 internal PropertyDescriptorCollection Filter (Attribute[] attributes)
254 ArrayList list = new ArrayList ();
255 foreach (PropertyDescriptor pd in properties)
256 if (pd.Attributes.Contains (attributes))
257 list.Add (pd);
259 return new PropertyDescriptorCollection (list);
262 bool IDictionary.IsFixedSize
264 get {
265 return true;
269 bool IList.IsFixedSize
271 get {
272 return true;
276 bool IList.IsReadOnly
278 get {
279 return false;
283 bool IDictionary.IsReadOnly
285 get
287 return false;
291 bool ICollection.IsSynchronized
293 get {
294 return false;
298 public int Count
300 get {
301 return properties.Count;
305 object ICollection.SyncRoot
307 get {
308 return null;
312 ICollection IDictionary.Keys
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;
323 ICollection IDictionary.Values
325 get {
326 return (ICollection) properties.Clone ();
330 object IDictionary.this [object key]
332 get {
333 if (!(key is string))
334 return null;
335 return this [(string) key];
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;
348 public virtual PropertyDescriptor this [string s]
350 get {
351 return Find (s, false);
355 object IList.this [int index]
357 get {
358 return properties [index];
360 set {
361 properties [index] = value;
365 public virtual PropertyDescriptor this [int index]
367 get {
368 return (PropertyDescriptor) properties [index];