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. 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. set
  75. {
  76. if(index >= 0 && index < Count)
  77. items[index] = value;
  78. }
  79. }
  80. public object SyncRoot
  81. {
  82. get
  83. {
  84. return this;
  85. }
  86. }
  87. public void Add(ListItem item)
  88. {
  89. items.Add(item);
  90. if(marked)
  91. item.Dirty = true;
  92. }
  93. public void Add(string item)
  94. {
  95. Add(new ListItem(item));
  96. }
  97. public void AddRange(ListItem[] items)
  98. {
  99. foreach(ListItem item in items)
  100. {
  101. if(item!=null)
  102. Add(item);
  103. }
  104. }
  105. public void Clear()
  106. {
  107. items.Clear();
  108. if(marked)
  109. saveAll = true;
  110. }
  111. public bool Contains(ListItem item)
  112. {
  113. return items.Contains(item);
  114. }
  115. public void CopyTo(Array array, int index)
  116. {
  117. items.CopyTo(array, index);
  118. }
  119. public ListItem FindByText(string text)
  120. {
  121. int i=-1;
  122. foreach(object current in items)
  123. {
  124. i++;
  125. if(((ListItem)current).Text == text)
  126. break;
  127. }
  128. return (i==-1 ? null : (ListItem)items[i]);
  129. }
  130. public ListItem FindByValue(string value)
  131. {
  132. foreach(ListItem current in items)
  133. {
  134. if(current.Value == value)
  135. {
  136. return current;
  137. }
  138. }
  139. return null;
  140. }
  141. internal int FindByValueInternal(string value)
  142. {
  143. int i = -1;
  144. foreach(ListItem current in items)
  145. {
  146. i++;
  147. if(current.Value == value)
  148. {
  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. }