PropertyDescriptorCollection.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. #if TARGET_JVM
  128. if (!ignoreCase)
  129. {
  130. foreach (PropertyDescriptor p in properties)
  131. {
  132. if (String.Equals (name, p.Name))
  133. return p;
  134. }
  135. return null;
  136. }
  137. #endif
  138. foreach (PropertyDescriptor p in properties) {
  139. if (0 == String.Compare (name, p.Name, ignoreCase))
  140. return p;
  141. }
  142. return null;
  143. }
  144. public virtual IEnumerator GetEnumerator ()
  145. {
  146. return properties.GetEnumerator ();
  147. }
  148. [MonoTODO]
  149. IDictionaryEnumerator IDictionary.GetEnumerator ()
  150. {
  151. throw new NotImplementedException ();
  152. }
  153. #if TARGET_JVM
  154. IEnumerator IEnumerable.GetEnumerator ()
  155. {
  156. return GetEnumerator ();
  157. }
  158. #endif
  159. public int IndexOf (PropertyDescriptor value)
  160. {
  161. return properties.IndexOf (value);
  162. }
  163. int IList.IndexOf (object value)
  164. {
  165. return IndexOf ((PropertyDescriptor) value);
  166. }
  167. public void Insert (int index, PropertyDescriptor value)
  168. {
  169. if (readOnly) {
  170. throw new NotSupportedException ();
  171. }
  172. properties.Insert (index, value);
  173. }
  174. void IList.Insert (int index, object value)
  175. {
  176. Insert (index, (PropertyDescriptor) value);
  177. }
  178. public void Remove (PropertyDescriptor value)
  179. {
  180. if (readOnly) {
  181. throw new NotSupportedException ();
  182. }
  183. properties.Remove (value);
  184. }
  185. #if TARGET_JVM// DUAL_IFACE_CONFLICT
  186. public void Remove (object value)
  187. {
  188. Remove ((PropertyDescriptor) value);
  189. }
  190. #else
  191. void IDictionary.Remove (object value)
  192. {
  193. Remove ((PropertyDescriptor) value);
  194. }
  195. void IList.Remove (object value)
  196. {
  197. Remove ((PropertyDescriptor) value);
  198. }
  199. #endif
  200. public void RemoveAt (int index)
  201. {
  202. if (readOnly) {
  203. throw new NotSupportedException ();
  204. }
  205. properties.RemoveAt (index);
  206. }
  207. void IList.RemoveAt (int index)
  208. {
  209. RemoveAt (index);
  210. }
  211. private PropertyDescriptorCollection CloneCollection ()
  212. {
  213. PropertyDescriptorCollection col = new PropertyDescriptorCollection ();
  214. col.properties = (ArrayList) properties.Clone ();
  215. return col;
  216. }
  217. public virtual PropertyDescriptorCollection Sort ()
  218. {
  219. PropertyDescriptorCollection col = CloneCollection ();
  220. col.InternalSort ((IComparer) null);
  221. return col;
  222. }
  223. public virtual PropertyDescriptorCollection Sort (IComparer comparer)
  224. {
  225. PropertyDescriptorCollection col = CloneCollection ();
  226. col.InternalSort (comparer);
  227. return col;
  228. }
  229. public virtual PropertyDescriptorCollection Sort (string[] order)
  230. {
  231. PropertyDescriptorCollection col = CloneCollection ();
  232. col.InternalSort (order);
  233. return col;
  234. }
  235. public virtual PropertyDescriptorCollection Sort (string[] order, IComparer comparer)
  236. {
  237. PropertyDescriptorCollection col = CloneCollection ();
  238. ArrayList sorted = col.ExtractItems (order);
  239. col.InternalSort (comparer);
  240. sorted.AddRange (col.properties);
  241. col.properties = sorted;
  242. return col;
  243. }
  244. protected void InternalSort (IComparer ic)
  245. {
  246. properties.Sort (ic);
  247. }
  248. protected void InternalSort (string [] order)
  249. {
  250. ArrayList sorted = ExtractItems (order);
  251. InternalSort ((IComparer) null);
  252. sorted.AddRange (properties);
  253. properties = sorted;
  254. }
  255. ArrayList ExtractItems (string[] names)
  256. {
  257. ArrayList sorted = new ArrayList (properties.Count);
  258. object[] ext = new object [names.Length];
  259. for (int n=0; n<properties.Count; n++)
  260. {
  261. PropertyDescriptor ed = (PropertyDescriptor) properties[n];
  262. int i = Array.IndexOf (names, ed.Name);
  263. if (i != -1) {
  264. ext[i] = ed;
  265. properties.RemoveAt (n);
  266. n--;
  267. }
  268. }
  269. foreach (object ob in ext)
  270. if (ob != null) sorted.Add (ob);
  271. return sorted;
  272. }
  273. internal PropertyDescriptorCollection Filter (Attribute[] attributes)
  274. {
  275. ArrayList list = new ArrayList ();
  276. foreach (PropertyDescriptor pd in properties) {
  277. if (pd.Attributes.Contains (attributes)) {
  278. list.Add (pd);
  279. }
  280. }
  281. PropertyDescriptor[] descriptors = new PropertyDescriptor[list.Count];
  282. list.CopyTo (descriptors);
  283. return new PropertyDescriptorCollection (descriptors, true);
  284. }
  285. #if TARGET_JVM //DUAL_IFACE_CONFLICT
  286. public bool IsFixedSize
  287. #else
  288. bool IDictionary.IsFixedSize
  289. {
  290. get {return ((IList)this).IsFixedSize;}
  291. }
  292. bool IList.IsFixedSize
  293. #endif
  294. {
  295. get
  296. {
  297. #if NET_2_0
  298. return readOnly;
  299. #else
  300. return !readOnly;
  301. #endif
  302. }
  303. }
  304. #if TARGET_JVM //DUAL_IFACE_CONFLICT
  305. public bool IsReadOnly
  306. #else
  307. bool IDictionary.IsReadOnly
  308. {
  309. get {return ((IList)this).IsReadOnly;}
  310. }
  311. bool IList.IsReadOnly
  312. #endif
  313. {
  314. get
  315. {
  316. return readOnly;
  317. }
  318. }
  319. bool ICollection.IsSynchronized
  320. {
  321. get {
  322. return false;
  323. }
  324. }
  325. int ICollection.Count {
  326. get { return Count; }
  327. }
  328. public int Count
  329. {
  330. get {
  331. return properties.Count;
  332. }
  333. }
  334. object ICollection.SyncRoot
  335. {
  336. get {
  337. return null;
  338. }
  339. }
  340. ICollection IDictionary.Keys
  341. {
  342. get {
  343. string [] keys = new string [properties.Count];
  344. int i = 0;
  345. foreach (PropertyDescriptor p in properties)
  346. keys [i++] = p.Name;
  347. return keys;
  348. }
  349. }
  350. ICollection IDictionary.Values
  351. {
  352. get {
  353. return (ICollection) properties.Clone ();
  354. }
  355. }
  356. object IDictionary.this [object key]
  357. {
  358. get {
  359. if (!(key is string))
  360. return null;
  361. return this [(string) key];
  362. }
  363. set {
  364. if (readOnly) {
  365. throw new NotSupportedException ();
  366. }
  367. if (!(key is string) || (value as PropertyDescriptor) == null)
  368. throw new ArgumentException ();
  369. int idx = properties.IndexOf (value);
  370. if (idx == -1)
  371. Add ((PropertyDescriptor) value);
  372. else
  373. properties [idx] = value;
  374. }
  375. }
  376. public virtual PropertyDescriptor this [string s]
  377. {
  378. get {
  379. return Find (s, false);
  380. }
  381. }
  382. object IList.this [int index]
  383. {
  384. get {
  385. return properties [index];
  386. }
  387. set {
  388. if (readOnly) {
  389. throw new NotSupportedException ();
  390. }
  391. properties [index] = value;
  392. }
  393. }
  394. public virtual PropertyDescriptor this [int index]
  395. {
  396. get {
  397. return (PropertyDescriptor) properties [index];
  398. }
  399. }
  400. }
  401. }