ListItemCollection.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Namespace: System.Web.UI.WebControls
  23. * Class: ListItemCollection
  24. *
  25. * Author: Gaurav Vaish
  26. * Maintainer: [email protected]
  27. * Contact: <[email protected]>, <[email protected]>
  28. * Implementation: yes
  29. * Status: 100%
  30. *
  31. * (C) Gaurav Vaish (2001)
  32. */
  33. using System;
  34. using System.Collections;
  35. using System.Web;
  36. using System.Web.UI;
  37. using System.ComponentModel;
  38. using System.Reflection;
  39. namespace System.Web.UI.WebControls
  40. {
  41. [Editor ("System.Web.UI.Design.WebControls.ListItemsCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  42. public sealed class ListItemCollection : IList, ICollection, IEnumerable, IStateManager
  43. {
  44. private ArrayList items;
  45. private bool saveAll;
  46. private bool marked;
  47. public ListItemCollection()
  48. {
  49. items = new ArrayList();
  50. saveAll = false;
  51. marked = false;
  52. }
  53. public int Capacity
  54. {
  55. get
  56. {
  57. return items.Capacity;
  58. }
  59. set
  60. {
  61. items.Capacity = value;
  62. }
  63. }
  64. public int Count
  65. {
  66. get
  67. {
  68. return items.Count;
  69. }
  70. }
  71. public bool IsReadOnly
  72. {
  73. get
  74. {
  75. return items.IsReadOnly;
  76. }
  77. }
  78. public bool IsSynchronized
  79. {
  80. get
  81. {
  82. return items.IsSynchronized;
  83. }
  84. }
  85. public ListItem this[int index]
  86. {
  87. get
  88. {
  89. if(index < 0 || index >= Count)
  90. return null;
  91. return (ListItem)(items[index]);
  92. }
  93. }
  94. public object SyncRoot
  95. {
  96. get
  97. {
  98. return this;
  99. }
  100. }
  101. public void Add(ListItem item)
  102. {
  103. items.Add(item);
  104. if(marked)
  105. item.Dirty = true;
  106. }
  107. public void Add(string item)
  108. {
  109. Add(new ListItem(item));
  110. }
  111. public void AddRange(ListItem[] items)
  112. {
  113. foreach(ListItem item in items)
  114. {
  115. if(item!=null)
  116. Add(item);
  117. }
  118. }
  119. public void Clear()
  120. {
  121. items.Clear();
  122. if(marked)
  123. saveAll = true;
  124. }
  125. public bool Contains(ListItem item)
  126. {
  127. return items.Contains(item);
  128. }
  129. public void CopyTo(Array array, int index)
  130. {
  131. items.CopyTo(array, index);
  132. }
  133. public ListItem FindByText(string text)
  134. {
  135. int i=-1;
  136. foreach(object current in items)
  137. {
  138. i++;
  139. if(((ListItem)current).Text == text)
  140. break;
  141. }
  142. return (i==-1 ? null : (ListItem)items[i]);
  143. }
  144. public ListItem FindByValue(string value)
  145. {
  146. foreach(ListItem current in items)
  147. {
  148. if(current.Value == value)
  149. {
  150. return current;
  151. }
  152. }
  153. return null;
  154. }
  155. internal int FindByValueInternal(string value)
  156. {
  157. int i = -1;
  158. foreach(ListItem current in items)
  159. {
  160. i++;
  161. if(current.Value == value)
  162. {
  163. return i;
  164. }
  165. }
  166. return -1;
  167. }
  168. public IEnumerator GetEnumerator()
  169. {
  170. return items.GetEnumerator();
  171. }
  172. public int IndexOf(ListItem item)
  173. {
  174. return items.IndexOf(item);
  175. }
  176. public void Insert(int index, ListItem item)
  177. {
  178. items.Insert(index, item);
  179. if(marked)
  180. saveAll = true;
  181. }
  182. public void Insert(int index, string item)
  183. {
  184. Insert(index, new ListItem(item));
  185. }
  186. public void RemoveAt(int index)
  187. {
  188. if(index < 0 || index >= items.Count)
  189. return;
  190. items.RemoveAt(index);
  191. if(marked)
  192. saveAll = true;
  193. }
  194. public void Remove(ListItem item)
  195. {
  196. RemoveAt(IndexOf(item));
  197. }
  198. public void Remove(string item)
  199. {
  200. RemoveAt(IndexOf(ListItem.FromString(item)));
  201. }
  202. internal object SaveViewState ()
  203. {
  204. int count = Count;
  205. if (saveAll) {
  206. string [] keys = new string [count];
  207. string [] vals = new string [count];
  208. for(int i = 0; i < count; i++) {
  209. keys[i] = this [i].Text;
  210. vals[i] = this [i].Value;
  211. }
  212. return new Triplet (count, keys, vals);
  213. }
  214. ArrayList indices = new ArrayList ();
  215. ArrayList states = new ArrayList ();
  216. object o;
  217. for(int i = 0; i < count; i++) {
  218. o = this [i].SaveViewState ();
  219. if (o == null)
  220. continue;
  221. indices.Add (i);
  222. states.Add (o);
  223. }
  224. if (indices.Count > 0)
  225. return new Pair (indices, states);
  226. return null;
  227. }
  228. internal void LoadViewState (object savedState)
  229. {
  230. if (savedState == null)
  231. return;
  232. int i, end;
  233. if (savedState is Pair) {
  234. Pair pair = (Pair) savedState;
  235. ArrayList indices = (ArrayList) pair.First;
  236. ArrayList states = (ArrayList) pair.Second;
  237. end = indices.Count;
  238. for (i = 0; i < end; i++) {
  239. if ((int) indices [i] < Count ) {
  240. this [i].LoadViewState (states [i]);
  241. } else {
  242. ListItem temp = new ListItem ();
  243. temp.LoadViewState (states [i]);
  244. Add (temp);
  245. }
  246. }
  247. } else if (savedState is Triplet) {
  248. Triplet t = (Triplet) savedState;
  249. items = new ArrayList ((int) t.First);
  250. saveAll = true;
  251. object [] text = (object []) t.Second;
  252. object [] vals = (object []) t.Third;
  253. end = text.Length;
  254. for(i = 0; i < end; i++)
  255. items.Add (new ListItem (text[i].ToString (), vals[i].ToString ()));
  256. }
  257. }
  258. internal void TrackViewState()
  259. {
  260. marked = true;
  261. foreach(ListItem current in items)
  262. current.TrackViewState();
  263. }
  264. bool IList.IsFixedSize
  265. {
  266. get
  267. {
  268. return false;
  269. }
  270. }
  271. object IList.this[int index]
  272. {
  273. get
  274. {
  275. return this[index];
  276. }
  277. set
  278. {
  279. if(index >= 0 && index < Count)
  280. if(value is ListItem)
  281. items[index] = (ListItem) value;
  282. }
  283. }
  284. int IList.Add(object item)
  285. {
  286. int index = (item is ListItem ? items.Add((ListItem)item) : -1);
  287. if(index!=-1 && marked)
  288. ((ListItem)item).Dirty = true;
  289. return index;
  290. }
  291. bool IList.Contains(object item)
  292. {
  293. if(item is ListItem)
  294. return Contains((ListItem)item);
  295. return false;
  296. }
  297. int IList.IndexOf(object item)
  298. {
  299. if(item is ListItem)
  300. return IndexOf((ListItem)item);
  301. return -1;
  302. }
  303. void IList.Insert(int index, object item)
  304. {
  305. if(item is ListItem)
  306. Insert(index, (ListItem)item);
  307. }
  308. void IList.Remove(object item)
  309. {
  310. if(item is string)
  311. Remove((string)item);
  312. if(item is ListItem)
  313. Remove((ListItem)item);
  314. }
  315. bool IStateManager.IsTrackingViewState
  316. {
  317. get
  318. {
  319. return marked;
  320. }
  321. }
  322. void IStateManager.LoadViewState(object state)
  323. {
  324. LoadViewState(state);
  325. }
  326. object IStateManager.SaveViewState()
  327. {
  328. return SaveViewState();
  329. }
  330. void IStateManager.TrackViewState()
  331. {
  332. TrackViewState();
  333. }
  334. }
  335. }