ListItemCollection.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. return current;
  133. }
  134. }
  135. return null;
  136. }
  137. internal int FindByValueInternal(string value)
  138. {
  139. int i = -1;
  140. foreach(ListItem current in items)
  141. {
  142. i++;
  143. if(current.Value == value)
  144. {
  145. return i;
  146. }
  147. }
  148. return -1;
  149. }
  150. public IEnumerator GetEnumerator()
  151. {
  152. return items.GetEnumerator();
  153. }
  154. public int IndexOf(ListItem item)
  155. {
  156. return items.IndexOf(item);
  157. }
  158. public void Insert(int index, ListItem item)
  159. {
  160. items.Insert(index, item);
  161. if(marked)
  162. saveAll = true;
  163. }
  164. public void Insert(int index, string item)
  165. {
  166. Insert(index, new ListItem(item));
  167. }
  168. public void RemoveAt(int index)
  169. {
  170. if(index < 0 || index >= items.Count)
  171. return;
  172. items.RemoveAt(index);
  173. if(marked)
  174. saveAll = true;
  175. }
  176. public void Remove(ListItem item)
  177. {
  178. RemoveAt(IndexOf(item));
  179. }
  180. public void Remove(string item)
  181. {
  182. RemoveAt(IndexOf(ListItem.FromString(item)));
  183. }
  184. internal object SaveViewState()
  185. {
  186. if(saveAll)
  187. {
  188. string[] keys = new string[Count];
  189. string[] vals = new string[Count];
  190. for(int i=0; i < Count; i++)
  191. {
  192. keys[i] = this[i].Text;
  193. vals[i] = this[i].Value;
  194. }
  195. return new Triplet(Count, keys, vals);
  196. }
  197. ArrayList indices = new ArrayList();
  198. ArrayList states = new ArrayList();
  199. object o;
  200. for(int i=0; i < Count; i++)
  201. {
  202. o = this[i].SaveViewState();
  203. if(o!=null)
  204. {
  205. indices.Add(i);
  206. states.Add(o);
  207. }
  208. }
  209. if(indices.Count > 0)
  210. return new Pair(indices, states);
  211. return null;
  212. }
  213. internal void LoadViewState(object savedState)
  214. {
  215. if(savedState!=null)
  216. {
  217. if(savedState is Pair)
  218. {
  219. ArrayList indices = (ArrayList)(((Pair)savedState).First);
  220. ArrayList states = (ArrayList)(((Pair)savedState).Second);
  221. for(int i=0; i < indices.Count; i++)
  222. {
  223. if( (int)indices[i] < Count )
  224. this[i].LoadViewState(states[i]);
  225. else
  226. {
  227. ListItem temp = new ListItem();
  228. temp.LoadViewState(states[i]);
  229. Add(temp);
  230. }
  231. }
  232. }
  233. if(savedState is Triplet)
  234. {
  235. Triplet t = (Triplet)savedState;
  236. items = new ArrayList((int)t.First);
  237. saveAll = true;
  238. string[] text = (string[])t.Second;
  239. string[] vals = (string[])t.Third;
  240. for(int i=0; i < text.Length; i++)
  241. items.Add(new ListItem(text[i], vals[i]));
  242. }
  243. }
  244. }
  245. internal void TrackViewState()
  246. {
  247. marked = true;
  248. foreach(ListItem current in this)
  249. current.TrackViewState();
  250. }
  251. bool IList.IsFixedSize
  252. {
  253. get
  254. {
  255. return false;
  256. }
  257. }
  258. object IList.this[int index]
  259. {
  260. get
  261. {
  262. return this[index];
  263. }
  264. set
  265. {
  266. if(value is ListItem)
  267. {
  268. this[index] = (ListItem)value;
  269. }
  270. }
  271. }
  272. int IList.Add(object item)
  273. {
  274. int index = (item is ListItem ? items.Add((ListItem)item) : -1);
  275. if(index!=-1 && marked)
  276. ((ListItem)item).Dirty = true;
  277. return index;
  278. }
  279. bool IList.Contains(object item)
  280. {
  281. if(item is ListItem)
  282. return Contains((ListItem)item);
  283. return false;
  284. }
  285. int IList.IndexOf(object item)
  286. {
  287. if(item is ListItem)
  288. return IndexOf((ListItem)item);
  289. return -1;
  290. }
  291. void IList.Insert(int index, object item)
  292. {
  293. if(item is ListItem)
  294. Insert(index, (ListItem)item);
  295. }
  296. void IList.Remove(object item)
  297. {
  298. if(item is string)
  299. Remove((string)item);
  300. if(item is ListItem)
  301. Remove((ListItem)item);
  302. }
  303. bool IStateManager.IsTrackingViewState
  304. {
  305. get
  306. {
  307. return marked;
  308. }
  309. }
  310. void IStateManager.LoadViewState(object state)
  311. {
  312. LoadViewState(state);
  313. }
  314. object IStateManager.SaveViewState()
  315. {
  316. return SaveViewState();
  317. }
  318. void IStateManager.TrackViewState()
  319. {
  320. TrackViewState();
  321. }
  322. }
  323. }