ListItemCollection.cs 6.1 KB

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