PropertyDescriptorCollection.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. //
  2. // System.ComponentModel.PropertyDescriptorCollection.cs
  3. //
  4. // Authors:
  5. // Rodrigo Moya ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Andreas Nahr ([email protected])
  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. #if NET_2_0
  52. public
  53. #else
  54. internal
  55. #endif
  56. PropertyDescriptorCollection (PropertyDescriptor[] properties, bool readOnly) : this (properties)
  57. {
  58. this.readOnly = readOnly;
  59. }
  60. private PropertyDescriptorCollection ()
  61. {
  62. }
  63. public int Add (PropertyDescriptor value)
  64. {
  65. if (readOnly) {
  66. throw new NotSupportedException ();
  67. }
  68. properties.Add (value);
  69. return properties.Count - 1;
  70. }
  71. int IList.Add (object value)
  72. {
  73. return Add ((PropertyDescriptor) value);
  74. }
  75. void IDictionary.Add (object key, object value)
  76. {
  77. if ((value as PropertyDescriptor) == null) {
  78. throw new ArgumentException ("value");
  79. }
  80. Add ((PropertyDescriptor) value);
  81. }
  82. public void Clear ()
  83. {
  84. if (readOnly) {
  85. throw new NotSupportedException ();
  86. }
  87. properties.Clear ();
  88. }
  89. #if !TARGET_JVM // DUAL_IFACE_CONFLICT
  90. void IList.Clear ()
  91. {
  92. Clear ();
  93. }
  94. void IDictionary.Clear ()
  95. {
  96. Clear ();
  97. }
  98. #endif
  99. public bool Contains (PropertyDescriptor value)
  100. {
  101. return properties.Contains (value);
  102. }
  103. #if TARGET_JVM // DUAL_IFACE_CONFLICT
  104. public bool Contains (object value)
  105. {
  106. return Contains ((PropertyDescriptor) value);
  107. }
  108. #else
  109. bool IList.Contains (object value)
  110. {
  111. return Contains ((PropertyDescriptor) value);
  112. }
  113. bool IDictionary.Contains (object value)
  114. {
  115. return Contains ((PropertyDescriptor) value);
  116. }
  117. #endif
  118. public void CopyTo (Array array, int index)
  119. {
  120. properties.CopyTo (array, index);
  121. }
  122. public virtual PropertyDescriptor Find (string name, bool ignoreCase)
  123. {
  124. if (name == null) {
  125. throw new ArgumentNullException ("name");
  126. }
  127. foreach (PropertyDescriptor p in properties) {
  128. if (0 == String.Compare (name, p.Name, ignoreCase, System.Globalization.CultureInfo.InvariantCulture))
  129. return p;
  130. }
  131. return null;
  132. }
  133. public virtual IEnumerator GetEnumerator ()
  134. {
  135. return properties.GetEnumerator ();
  136. }
  137. [MonoTODO]
  138. IDictionaryEnumerator IDictionary.GetEnumerator ()
  139. {
  140. throw new NotImplementedException ();
  141. }
  142. #if TARGET_JVM
  143. IEnumerator IEnumerable.GetEnumerator ()
  144. {
  145. return GetEnumerator ();
  146. }
  147. #endif
  148. public int IndexOf (PropertyDescriptor value)
  149. {
  150. return properties.IndexOf (value);
  151. }
  152. int IList.IndexOf (object value)
  153. {
  154. return IndexOf ((PropertyDescriptor) value);
  155. }
  156. public void Insert (int index, PropertyDescriptor value)
  157. {
  158. if (readOnly) {
  159. throw new NotSupportedException ();
  160. }
  161. properties.Insert (index, value);
  162. }
  163. void IList.Insert (int index, object value)
  164. {
  165. Insert (index, (PropertyDescriptor) value);
  166. }
  167. public void Remove (PropertyDescriptor value)
  168. {
  169. if (readOnly) {
  170. throw new NotSupportedException ();
  171. }
  172. properties.Remove (value);
  173. }
  174. #if TARGET_JVM// DUAL_IFACE_CONFLICT
  175. public void Remove (object value)
  176. {
  177. Remove ((PropertyDescriptor) value);
  178. }
  179. #else
  180. void IDictionary.Remove (object value)
  181. {
  182. Remove ((PropertyDescriptor) value);
  183. }
  184. void IList.Remove (object value)
  185. {
  186. Remove ((PropertyDescriptor) value);
  187. }
  188. #endif
  189. public void RemoveAt (int index)
  190. {
  191. if (readOnly) {
  192. throw new NotSupportedException ();
  193. }
  194. properties.RemoveAt (index);
  195. }
  196. void IList.RemoveAt (int index)
  197. {
  198. RemoveAt (index);
  199. }
  200. private PropertyDescriptorCollection CloneCollection ()
  201. {
  202. PropertyDescriptorCollection col = new PropertyDescriptorCollection ();
  203. col.properties = (ArrayList) properties.Clone ();
  204. return col;
  205. }
  206. public virtual PropertyDescriptorCollection Sort ()
  207. {
  208. PropertyDescriptorCollection col = CloneCollection ();
  209. col.InternalSort ((IComparer) null);
  210. return col;
  211. }
  212. public virtual PropertyDescriptorCollection Sort (IComparer comparer)
  213. {
  214. PropertyDescriptorCollection col = CloneCollection ();
  215. col.InternalSort (comparer);
  216. return col;
  217. }
  218. public virtual PropertyDescriptorCollection Sort (string[] order)
  219. {
  220. PropertyDescriptorCollection col = CloneCollection ();
  221. col.InternalSort (order);
  222. return col;
  223. }
  224. public virtual PropertyDescriptorCollection Sort (string[] order, IComparer comparer)
  225. {
  226. PropertyDescriptorCollection col = CloneCollection ();
  227. ArrayList sorted = col.ExtractItems (order);
  228. col.InternalSort (comparer);
  229. sorted.AddRange (col.properties);
  230. col.properties = sorted;
  231. return col;
  232. }
  233. protected void InternalSort (IComparer ic)
  234. {
  235. properties.Sort (ic);
  236. }
  237. protected void InternalSort (string [] order)
  238. {
  239. ArrayList sorted = ExtractItems (order);
  240. InternalSort ((IComparer) null);
  241. sorted.AddRange (properties);
  242. properties = sorted;
  243. }
  244. ArrayList ExtractItems (string[] names)
  245. {
  246. ArrayList sorted = new ArrayList (properties.Count);
  247. object[] ext = new object [names.Length];
  248. for (int n=0; n<properties.Count; n++)
  249. {
  250. PropertyDescriptor ed = (PropertyDescriptor) properties[n];
  251. int i = Array.IndexOf (names, ed.Name);
  252. if (i != -1) {
  253. ext[i] = ed;
  254. properties.RemoveAt (n);
  255. n--;
  256. }
  257. }
  258. foreach (object ob in ext)
  259. if (ob != null) sorted.Add (ob);
  260. return sorted;
  261. }
  262. internal PropertyDescriptorCollection Filter (Attribute[] attributes)
  263. {
  264. ArrayList list = new ArrayList ();
  265. foreach (PropertyDescriptor pd in properties) {
  266. if (pd.Attributes.Contains (attributes)) {
  267. list.Add (pd);
  268. }
  269. }
  270. PropertyDescriptor[] descriptors = new PropertyDescriptor[list.Count];
  271. list.CopyTo (descriptors);
  272. return new PropertyDescriptorCollection (descriptors, true);
  273. }
  274. #if TARGET_JVM //DUAL_IFACE_CONFLICT
  275. public bool IsFixedSize
  276. #else
  277. bool IDictionary.IsFixedSize
  278. {
  279. get {return ((IList)this).IsFixedSize;}
  280. }
  281. bool IList.IsFixedSize
  282. #endif
  283. {
  284. get
  285. {
  286. #if NET_2_0
  287. return readOnly;
  288. #else
  289. return !readOnly;
  290. #endif
  291. }
  292. }
  293. #if TARGET_JVM //DUAL_IFACE_CONFLICT
  294. public bool IsReadOnly
  295. #else
  296. bool IDictionary.IsReadOnly
  297. {
  298. get {return ((IList)this).IsReadOnly;}
  299. }
  300. bool IList.IsReadOnly
  301. #endif
  302. {
  303. get
  304. {
  305. return readOnly;
  306. }
  307. }
  308. bool ICollection.IsSynchronized
  309. {
  310. get {
  311. return false;
  312. }
  313. }
  314. int ICollection.Count {
  315. get { return Count; }
  316. }
  317. public int Count
  318. {
  319. get {
  320. return properties.Count;
  321. }
  322. }
  323. object ICollection.SyncRoot
  324. {
  325. get {
  326. return null;
  327. }
  328. }
  329. ICollection IDictionary.Keys
  330. {
  331. get {
  332. string [] keys = new string [properties.Count];
  333. int i = 0;
  334. foreach (PropertyDescriptor p in properties)
  335. keys [i++] = p.Name;
  336. return keys;
  337. }
  338. }
  339. ICollection IDictionary.Values
  340. {
  341. get {
  342. return (ICollection) properties.Clone ();
  343. }
  344. }
  345. object IDictionary.this [object key]
  346. {
  347. get {
  348. if (!(key is string))
  349. return null;
  350. return this [(string) key];
  351. }
  352. set {
  353. if (readOnly) {
  354. throw new NotSupportedException ();
  355. }
  356. if (!(key is string) || (value as PropertyDescriptor) == null)
  357. throw new ArgumentException ();
  358. int idx = properties.IndexOf (value);
  359. if (idx == -1)
  360. Add ((PropertyDescriptor) value);
  361. else
  362. properties [idx] = value;
  363. }
  364. }
  365. public virtual PropertyDescriptor this [string s]
  366. {
  367. get {
  368. return Find (s, false);
  369. }
  370. }
  371. object IList.this [int index]
  372. {
  373. get {
  374. return properties [index];
  375. }
  376. set {
  377. if (readOnly) {
  378. throw new NotSupportedException ();
  379. }
  380. properties [index] = value;
  381. }
  382. }
  383. public virtual PropertyDescriptor this [int index]
  384. {
  385. get {
  386. return (PropertyDescriptor) properties [index];
  387. }
  388. }
  389. }
  390. }