[Cleanup] Removed TARGET_JVM
[mono-project.git] / mcs / class / System / System.ComponentModel / PropertyDescriptorCollection.cs
blobe84115ff8bd86158cab8de5e269161661726afbd
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 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)
50 return;
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)
66 if (readOnly) {
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);
87 public void Clear ()
89 if (readOnly) {
90 throw new NotSupportedException ();
92 properties.Clear ();
95 void IList.Clear ()
97 Clear ();
100 void IDictionary.Clear ()
102 Clear ();
105 public bool Contains (PropertyDescriptor value)
107 return properties.Contains (value);
111 bool IList.Contains (object value)
113 return Contains ((PropertyDescriptor) value);
116 bool IDictionary.Contains (object value)
118 return Contains ((PropertyDescriptor) value);
121 public void CopyTo (Array array, int index)
123 properties.CopyTo (array, index);
126 public virtual PropertyDescriptor Find (string name, bool ignoreCase)
128 if (name == null)
129 throw new ArgumentNullException ("name");
131 for (int i = 0; i < properties.Count; ++i) {
132 PropertyDescriptor p = (PropertyDescriptor)properties [i];
133 if (ignoreCase) {
134 if (0 == String.Compare (name, p.Name, StringComparison.OrdinalIgnoreCase))
135 return p;
137 else {
138 if (0 == String.Compare (name, p.Name, StringComparison.Ordinal))
139 return p;
142 return null;
145 public virtual IEnumerator GetEnumerator ()
147 return properties.GetEnumerator ();
150 IEnumerator IEnumerable.GetEnumerator ()
152 return GetEnumerator ();
155 [MonoTODO]
156 IDictionaryEnumerator IDictionary.GetEnumerator ()
158 throw new NotImplementedException ();
161 public int IndexOf (PropertyDescriptor value)
163 return properties.IndexOf (value);
166 int IList.IndexOf (object value)
168 return IndexOf ((PropertyDescriptor) value);
171 public void Insert (int index, PropertyDescriptor value)
173 if (readOnly) {
174 throw new NotSupportedException ();
176 properties.Insert (index, value);
179 void IList.Insert (int index, object value)
181 Insert (index, (PropertyDescriptor) value);
184 public void Remove (PropertyDescriptor value)
186 if (readOnly) {
187 throw new NotSupportedException ();
189 properties.Remove (value);
192 void IDictionary.Remove (object value)
194 Remove ((PropertyDescriptor) value);
197 void IList.Remove (object value)
199 Remove ((PropertyDescriptor) value);
201 public void RemoveAt (int index)
203 if (readOnly) {
204 throw new NotSupportedException ();
206 properties.RemoveAt (index);
209 void IList.RemoveAt (int index)
211 RemoveAt (index);
214 private PropertyDescriptorCollection CloneCollection ()
216 PropertyDescriptorCollection col = new PropertyDescriptorCollection ();
217 col.properties = (ArrayList) properties.Clone ();
218 return col;
221 public virtual PropertyDescriptorCollection Sort ()
223 PropertyDescriptorCollection col = CloneCollection ();
224 col.InternalSort ((IComparer) null);
225 return col;
228 public virtual PropertyDescriptorCollection Sort (IComparer comparer)
230 PropertyDescriptorCollection col = CloneCollection ();
231 col.InternalSort (comparer);
232 return col;
235 public virtual PropertyDescriptorCollection Sort (string[] order)
237 PropertyDescriptorCollection col = CloneCollection ();
238 col.InternalSort (order);
239 return col;
242 public virtual PropertyDescriptorCollection Sort (string[] order, IComparer comparer)
244 PropertyDescriptorCollection col = CloneCollection ();
245 if (order != null) {
246 ArrayList sorted = col.ExtractItems (order);
247 col.InternalSort (comparer);
248 sorted.AddRange (col.properties);
249 col.properties = sorted;
250 } else {
251 col.InternalSort (comparer);
253 return col;
256 protected void InternalSort (IComparer ic)
258 if (ic == null)
259 ic = MemberDescriptor.DefaultComparer;
260 properties.Sort (ic);
263 protected void InternalSort (string [] order)
265 if (order != null) {
266 ArrayList sorted = ExtractItems (order);
267 InternalSort ((IComparer) null);
268 sorted.AddRange (properties);
269 properties = sorted;
270 } else {
271 InternalSort ((IComparer) null);
275 ArrayList ExtractItems (string[] names)
277 ArrayList sorted = new ArrayList (properties.Count);
278 object[] ext = new object [names.Length];
280 for (int n=0; n<properties.Count; n++)
282 PropertyDescriptor ed = (PropertyDescriptor) properties[n];
283 int i = Array.IndexOf (names, ed.Name);
284 if (i != -1) {
285 ext[i] = ed;
286 properties.RemoveAt (n);
287 n--;
290 foreach (object ob in ext)
291 if (ob != null) sorted.Add (ob);
293 return sorted;
296 internal PropertyDescriptorCollection Filter (Attribute[] attributes)
298 ArrayList list = new ArrayList ();
299 foreach (PropertyDescriptor pd in properties) {
300 if (pd.Attributes.Contains (attributes)) {
301 list.Add (pd);
304 PropertyDescriptor[] descriptors = new PropertyDescriptor[list.Count];
305 list.CopyTo (descriptors);
306 return new PropertyDescriptorCollection (descriptors, true);
309 bool IDictionary.IsFixedSize
311 get {return ((IList)this).IsFixedSize;}
313 bool IList.IsFixedSize
315 get
317 return readOnly;
320 bool IDictionary.IsReadOnly
322 get {return ((IList)this).IsReadOnly;}
324 bool IList.IsReadOnly
326 get
328 return readOnly;
332 bool ICollection.IsSynchronized
334 get {
335 return false;
339 int ICollection.Count {
340 get { return Count; }
343 public int Count
345 get {
346 return properties.Count;
350 object ICollection.SyncRoot
352 get {
353 return null;
357 ICollection IDictionary.Keys
359 get {
360 string [] keys = new string [properties.Count];
361 int i = 0;
362 foreach (PropertyDescriptor p in properties)
363 keys [i++] = p.Name;
364 return keys;
368 ICollection IDictionary.Values
370 get {
371 return (ICollection) properties.Clone ();
375 object IDictionary.this [object key]
377 get {
378 if (!(key is string))
379 return null;
380 return this [(string) key];
382 set {
383 if (readOnly) {
384 throw new NotSupportedException ();
387 if (!(key is string) || (value as PropertyDescriptor) == null)
388 throw new ArgumentException ();
389 int idx = properties.IndexOf (value);
390 if (idx == -1)
391 Add ((PropertyDescriptor) value);
392 else
393 properties [idx] = value;
397 public virtual PropertyDescriptor this [string s]
399 get {
400 return Find (s, false);
404 object IList.this [int index]
406 get {
407 return properties [index];
409 set {
410 if (readOnly) {
411 throw new NotSupportedException ();
413 properties [index] = value;
417 public virtual PropertyDescriptor this [int index]
419 get {
420 return (PropertyDescriptor) properties [index];