ListItemCollection.cs 6.3 KB

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