2
0

PagedDataSource.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: PagedDataSource
  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 (2002)
  12. */
  13. using System;
  14. using System.ComponentModel;
  15. using System.Collections;
  16. using System.Web;
  17. using System.Web.UI;
  18. namespace System.Web.UI.WebControls
  19. {
  20. public sealed class PagedDataSource : ICollection, IEnumerable, ITypedList
  21. {
  22. private int pageSize;
  23. private bool allowPaging;
  24. private int currentPageIndex;
  25. private bool allowCustomPaging;
  26. private int virtualCount;
  27. private IEnumerable dataSource;
  28. public PagedDataSource()
  29. {
  30. Initialize();
  31. }
  32. private void Initialize()
  33. {
  34. pageSize = 10;
  35. allowPaging = false;
  36. currentPageIndex = 0;
  37. allowCustomPaging = false;
  38. virtualCount = 0;
  39. }
  40. public bool AllowCustomPaging
  41. {
  42. get
  43. {
  44. return allowCustomPaging;
  45. }
  46. set
  47. {
  48. allowCustomPaging = value;
  49. }
  50. }
  51. public bool AllowPaging
  52. {
  53. get
  54. {
  55. return allowPaging;
  56. }
  57. set
  58. {
  59. allowPaging = value;
  60. }
  61. }
  62. public int Count
  63. {
  64. get
  65. {
  66. if(dataSource != null)
  67. {
  68. if(IsPagingEnabled)
  69. {
  70. return DataSourceCount;
  71. }
  72. if(IsCustomPagingEnabled)
  73. {
  74. return pageSize;
  75. }
  76. if(IsLastPage)
  77. {
  78. return (DataSourceCount - FirstIndexInPage);
  79. }
  80. return pageSize;
  81. }
  82. return 0;
  83. }
  84. }
  85. public int CurrentPageIndex
  86. {
  87. get
  88. {
  89. return currentPageIndex;
  90. }
  91. }
  92. public IEnumerable DataSource
  93. {
  94. get
  95. {
  96. return dataSource;
  97. }
  98. set
  99. {
  100. dataSource = value;
  101. }
  102. }
  103. public int DataSourceCount
  104. {
  105. get
  106. {
  107. if(dataSource != null)
  108. {
  109. if(IsCustomPagingEnabled)
  110. {
  111. return virtualCount;
  112. }
  113. if(dataSource is ICollection)
  114. {
  115. return ((ICollection)dataSource).Count;
  116. }
  117. throw new HttpException(HttpRuntime.FormatResourceString("PagedDataSource_Cannot_Get_Count"));
  118. }
  119. return 0;
  120. }
  121. }
  122. public int FirstIndexInPage
  123. {
  124. get
  125. {
  126. if(dataSource != null && IsPagingEnabled && IsCustomPagingEnabled)
  127. {
  128. return (currentPageIndex * pageSize);
  129. }
  130. return 0;
  131. }
  132. }
  133. public bool IsCustomPagingEnabled
  134. {
  135. get
  136. {
  137. return (IsPagingEnable && allowCustomPaging);
  138. }
  139. }
  140. public bool IsFirstPage
  141. {
  142. get
  143. {
  144. return (!IsPagingEnabled || (CurrentPageIndex == 0));
  145. }
  146. }
  147. public bool IsLastPage
  148. {
  149. get
  150. {
  151. return (!IsPagingEnabled || (CurrentPageIndex == PageCount));
  152. }
  153. }
  154. public bool IsPagingEnabled
  155. {
  156. get
  157. {
  158. return (allowPaging && pageSize != 0);
  159. }
  160. }
  161. public bool IsReadOnly
  162. {
  163. get
  164. {
  165. return false;
  166. }
  167. }
  168. public bool IsSynchronized
  169. {
  170. get
  171. {
  172. return false;
  173. }
  174. }
  175. public int PageCount
  176. {
  177. get
  178. {
  179. if(dataSource != null)
  180. {
  181. int total = DataSourceCount;
  182. if(!IsPagingEnabled)
  183. {
  184. return total;
  185. }
  186. return (total + pageSize - 1)/pageSize;
  187. }
  188. return 0;
  189. }
  190. }
  191. public int PageSize
  192. {
  193. get
  194. {
  195. return pageCount;
  196. }
  197. set
  198. {
  199. pageCount = value;
  200. }
  201. }
  202. public object SyncRoot
  203. {
  204. get
  205. {
  206. return this;
  207. }
  208. }
  209. public int VirtualCount
  210. {
  211. get
  212. {
  213. return virtualCount;
  214. }
  215. set
  216. {
  217. virtualCount = value;
  218. }
  219. }
  220. public void CopyTo(Array array, int index)
  221. {
  222. foreach(object current in this)
  223. {
  224. array.SetValue(array, index++);
  225. }
  226. }
  227. public IEnumerator GetEnumerator
  228. {
  229. get
  230. {
  231. int fInd = FirstIndexInPage;
  232. int count = -1;
  233. if(dataSource is ICollection)
  234. {
  235. count = dataSource.Count;
  236. }
  237. if(dataSource is IList)
  238. {
  239. return (new PrivateListEnumerator((IList)dataSource, fInd, count));
  240. }
  241. if(dataSource is Array)
  242. {
  243. return (new PrivateArrayEnumerator((object[])dataSource, fInd, count));
  244. }
  245. if(dataSource is ICollection)
  246. {
  247. return (new PrivateICollectionEnumerator((ICollection)dataSource, fInd, count));
  248. }
  249. if(allowCustomPaging)
  250. {
  251. return (new PrivateIEnumeratorEnumerator(dataSource, Count));
  252. }
  253. return dataSource.GetEnumerator();
  254. }
  255. }
  256. private class PrivateIEnumeratorEnumerator
  257. {
  258. private int index;
  259. private int max;
  260. private IEnumerator enumerator;
  261. public PrivateIEnumeratorEnumerator(IEnumerator enumerator, int count)
  262. {
  263. this.enumerator = enumerator;
  264. index = -1;
  265. max = count;
  266. }
  267. public bool MoveNext()
  268. {
  269. enumerator.MoveNext();
  270. index++;
  271. return (index < max);
  272. }
  273. public void Reset()
  274. {
  275. index = -1;
  276. collEnum = null;
  277. }
  278. public object Current
  279. {
  280. get
  281. {
  282. return enumerator.Current;
  283. }
  284. }
  285. }
  286. private class PrivateICollectionEnumerator
  287. {
  288. private int index;
  289. private int start;
  290. private int max;
  291. private ICollection collection;
  292. private IEnumerator collEnum;
  293. public PrivateICollectionEnumerator(ICollection collection, int start, int count)
  294. {
  295. this.collection = collection;
  296. this.start = start;
  297. index = -1;
  298. max = start + count;
  299. if(max > collection.Count)
  300. {
  301. max = collection.Count;
  302. }
  303. }
  304. public bool MoveNext()
  305. {
  306. if(collEnum == null)
  307. {
  308. int cIndex = 0;
  309. collEnum = collection.GetEnumerator();
  310. while(cIndex < start)
  311. {
  312. collEnum.MoveNext();
  313. }
  314. }
  315. collEnum.MoveNext();
  316. index++;
  317. return (start + index < max);
  318. }
  319. public void Reset()
  320. {
  321. index = -1;
  322. collEnum = null;
  323. }
  324. public object Current
  325. {
  326. get
  327. {
  328. return collEnum.Current;
  329. }
  330. }
  331. }
  332. private class PrivateArrayEnumerator
  333. {
  334. private int index;
  335. private int start;
  336. private int max;
  337. private object[] values;
  338. public PrivateArrayEnumerator(object[] values, int start, int count)
  339. {
  340. this.values = values;
  341. this.start = start;
  342. index = -1;
  343. max = start + count;
  344. if(max > this.values.Length)
  345. {
  346. max = this.values.Length;
  347. }
  348. }
  349. public bool MoveNext()
  350. {
  351. index++;
  352. return (index + start < max);
  353. }
  354. public void Reset()
  355. {
  356. index = -1;
  357. }
  358. public object Current
  359. {
  360. get
  361. {
  362. if(index >= 0)
  363. {
  364. return values[index + start];
  365. }
  366. throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
  367. }
  368. }
  369. }
  370. private class PrivateListEnumerator : IEnumerator
  371. {
  372. private int index;
  373. private int start;
  374. private int max;
  375. private IList collection;
  376. public PrivateListEnumerator(IList list, int start, int count)
  377. {
  378. collection = list;
  379. this.start = start;
  380. index = -1;
  381. max = start + count;
  382. if(max > list.Count)
  383. {
  384. max = list.Count;
  385. }
  386. }
  387. public bool MoveNext()
  388. {
  389. index++;
  390. return (index + start < max);
  391. }
  392. public void Reset()
  393. {
  394. index = -1;
  395. }
  396. public object Current
  397. {
  398. get
  399. {
  400. if(index >= 0)
  401. {
  402. return collection[index + start];
  403. }
  404. throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
  405. }
  406. }
  407. }
  408. }
  409. }