ListItemCollection.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: ListItemCollection
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.Collections;
  15. using System.Web;
  16. using System.Web.UI;
  17. namespace System.Web.UI.WebControls
  18. {
  19. public class ListItemCollection : IList, ICollection, IEnumerable, IStateManager
  20. {
  21. private ArrayList items;
  22. private bool saveAll;
  23. private bool marked;
  24. public ListItemCollection()
  25. {
  26. items = new ArrayList();
  27. saveAll = false;
  28. marked = false;
  29. }
  30. public int Capacity
  31. {
  32. get
  33. {
  34. return items.Capacity;
  35. }
  36. set
  37. {
  38. items.Capacity = value;
  39. }
  40. }
  41. public int Count
  42. {
  43. get
  44. {
  45. return items.Count;
  46. }
  47. }
  48. public bool IsReadOnly
  49. {
  50. get
  51. {
  52. return items.IsReadOnly;
  53. }
  54. }
  55. public bool IsSynchronized
  56. {
  57. get
  58. {
  59. return items.IsSynchronized;
  60. }
  61. }
  62. public ListItem this[int index]
  63. {
  64. get
  65. {
  66. if(index < 0 || index >= Count)
  67. return null;
  68. return (ListItem)(items[index]);
  69. }
  70. set
  71. {
  72. if(index >= 0 && index < Count)
  73. items[index] = value;
  74. }
  75. }
  76. public object SyncRoot
  77. {
  78. get
  79. {
  80. return this;
  81. }
  82. }
  83. public void Add(ListItem item)
  84. {
  85. items.Add(item);
  86. if(marked)
  87. item.Dirty = true;
  88. }
  89. public void Add(string item)
  90. {
  91. Add(new ListItem(item));
  92. }
  93. public void AddRange(ListItem[] items)
  94. {
  95. foreach(ListItem item in items)
  96. {
  97. if(item!=null)
  98. Add(item);
  99. }
  100. }
  101. public void Clear()
  102. {
  103. items.Clear();
  104. if(marked)
  105. saveAll = true;
  106. }
  107. public bool Contains(ListItem item)
  108. {
  109. return items.Contains(item);
  110. }
  111. public void CopyTo(Array array, int index)
  112. {
  113. items.CopyTo(array, index);
  114. }
  115. public ListItem FindByText(string text)
  116. {
  117. int i=-1;
  118. foreach(object current in items)
  119. {
  120. i++;
  121. if(((ListItem)current).Text == text)
  122. break;
  123. }
  124. return (i==-1 ? null : (ListItem)items[i]);
  125. }
  126. public ListItem FindByValue(string value)
  127. {
  128. foreach(ListItem current in items)
  129. {
  130. if(current.Value == value)
  131. {
  132. if(current is Disposable)
  133. current.Dispose();
  134. return current;
  135. }
  136. }
  137. return null;
  138. }
  139. internal int FindByValueInternal(string value)
  140. {
  141. int i = -1;
  142. foreach(ListItem current in items)
  143. {
  144. i++;
  145. if(current.Value == value)
  146. {
  147. if(current is Disposable)
  148. current.Dispose();
  149. return i;
  150. }
  151. }
  152. return -1;
  153. }
  154. public IEnumerator GetEnumerator()
  155. {
  156. return items.GetEnumerator();
  157. }
  158. public int IndexOf(ListItem item)
  159. {
  160. return items.IndexOf(item);
  161. }
  162. public void Insert(int index, ListItem item)
  163. {
  164. items.Insert(index, item);
  165. if(marked)
  166. saveAll = true;
  167. }
  168. public void Insert(int index, string item)
  169. {
  170. Insert(index, new ListItem(item));
  171. }
  172. public void RemoveAt(int index)
  173. {
  174. if(index < 0 || index >= items.Count)
  175. return;
  176. items.RemoveAt(index);
  177. if(marked)
  178. saveAll = true;
  179. }
  180. public void Remove(ListItem item)
  181. {
  182. RemoveAt(IndexOf(item));
  183. }
  184. public void Remove(string item)
  185. {
  186. RemoveAt(IndexOf(ListItem.FromString(item)));
  187. }
  188. internal object SaveViewState()
  189. {
  190. if(saveAll)
  191. {
  192. string[] keys = new string[Count];
  193. string[] vals = new string[Count];
  194. for(int i=0; i < Count; i++)
  195. {
  196. keys[i] = this[i].Text;
  197. vals[i] = this[i].Value;
  198. }
  199. return new Triplet(Count, keys, vals);
  200. }
  201. ArrayList indices = new ArrayList();
  202. ArrayList states = new ArrayList();
  203. object o;
  204. for(int i=0; i < Count; i++)
  205. {
  206. o = this[i].SaveViewState();
  207. if(o!=null)
  208. {
  209. indices.Add(i);
  210. states.Add(o);
  211. }
  212. }
  213. if(indices.Count > 0)
  214. return new Pair(indices, states);
  215. return null;
  216. }
  217. internal void LoadViewState(object savedState)
  218. {
  219. if(savedState!=null)
  220. {
  221. if(savedState is Pair)
  222. {
  223. ArrayList indices = (ArrayList)(((Pair)savedState).First);
  224. ArrayList states = (ArrayList)(((Pair)savedState).Second);
  225. for(int i=0; i < indices.Count; i++)
  226. {
  227. if( (int)indices[i] < Count )
  228. this[i].LoadViewState(states[i]);
  229. else
  230. {
  231. ListItem temp = new ListItem();
  232. temp.LoadViewState(states[i]);
  233. Add(temp);
  234. }
  235. }
  236. }
  237. if(savedState is Triplet)
  238. {
  239. Triplet t = (Triplet)savedState;
  240. items = new ArrayList((int)t.First);
  241. saveAll = true;
  242. string[] text = (string[])t.Second;
  243. string[] vals = (string[])t.Third;
  244. for(int i=0; i < text.Length; i++)
  245. items.Add(new ListItem(text[i], vals[i]));
  246. }
  247. }
  248. }
  249. internal void TrackViewState()
  250. {
  251. marked = true;
  252. foreach(ListItem current in this)
  253. current.TrackViewState();
  254. }
  255. bool IList.IsFixedSize
  256. {
  257. get
  258. {
  259. return false;
  260. }
  261. }
  262. object IList.this[int index]
  263. {
  264. get
  265. {
  266. return this[index];
  267. }
  268. set
  269. {
  270. if(value is ListItem)
  271. {
  272. this[index] = (ListItem)value;
  273. }
  274. }
  275. }
  276. int IList.Add(object item)
  277. {
  278. int index = (item is ListItem ? items.Add((ListItem)item) : -1);
  279. if(index!=-1 && marked)
  280. ((ListItem)item).Dirty = true;
  281. return index;
  282. }
  283. bool IList.Contains(object item)
  284. {
  285. if(item is ListItem)
  286. return Contains((ListItem)item);
  287. return false;
  288. }
  289. int IList.IndexOf(object item)
  290. {
  291. if(item is ListItem)
  292. return IndexOf((ListItem)item);
  293. return -1;
  294. }
  295. void IList.Insert(int index, object item)
  296. {
  297. if(item is ListItem)
  298. Insert(index, (ListItem)item);
  299. }
  300. void IList.Remove(object item)
  301. {
  302. if(item is string)
  303. Remove((string)item);
  304. if(item is ListItem)
  305. Remove((ListItem)item);
  306. }
  307. bool IStateManager.IsTrackingViewState
  308. {
  309. get
  310. {
  311. return marked;
  312. }
  313. }
  314. void IStateManager.LoadViewState(object state)
  315. {
  316. LoadViewState(state);
  317. }
  318. object IStateManager.SaveViewState()
  319. {
  320. return SaveViewState();
  321. }
  322. void IStateManager.TrackViewState()
  323. {
  324. TrackViewState();
  325. }
  326. }
  327. }