PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System/System.ComponentModel/PropertyDescriptorCollection.cs

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