PagedDataSource.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 (IsPagingEnabled && 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 pageSize;
  196. }
  197. set
  198. {
  199. pageSize = 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. int fInd = FirstIndexInPage;
  230. int count = -1;
  231. if(dataSource is ICollection)
  232. {
  233. count = Count;
  234. }
  235. if(dataSource is IList)
  236. {
  237. return (new PrivateListEnumerator((IList)dataSource, fInd, count));
  238. }
  239. if(dataSource is Array)
  240. {
  241. return (new PrivateArrayEnumerator((object[])dataSource, fInd, count));
  242. }
  243. if(dataSource is ICollection)
  244. {
  245. return (new PrivateICollectionEnumerator((ICollection)dataSource, fInd, count));
  246. }
  247. if(allowCustomPaging)
  248. {
  249. return (new PrivateIEnumeratorEnumerator(dataSource.GetEnumerator(), Count));
  250. }
  251. return dataSource.GetEnumerator();
  252. }
  253. class PrivateIEnumeratorEnumerator : IEnumerator
  254. {
  255. private int index;
  256. private int max;
  257. private IEnumerator enumerator;
  258. public PrivateIEnumeratorEnumerator(IEnumerator enumerator, int count)
  259. {
  260. this.enumerator = enumerator;
  261. index = -1;
  262. max = count;
  263. }
  264. public bool MoveNext()
  265. {
  266. enumerator.MoveNext();
  267. index++;
  268. return (index < max);
  269. }
  270. public void Reset()
  271. {
  272. index = -1;
  273. enumerator.Reset();
  274. }
  275. public object Current
  276. {
  277. get
  278. {
  279. return enumerator.Current;
  280. }
  281. }
  282. }
  283. class PrivateICollectionEnumerator : IEnumerator
  284. {
  285. private int index;
  286. private int start;
  287. private int max;
  288. private ICollection collection;
  289. private IEnumerator collEnum;
  290. public PrivateICollectionEnumerator(ICollection collection, int start, int count)
  291. {
  292. this.collection = collection;
  293. this.start = start;
  294. index = -1;
  295. max = start + count;
  296. if(max > collection.Count)
  297. {
  298. max = collection.Count;
  299. }
  300. }
  301. public bool MoveNext()
  302. {
  303. if(collEnum == null)
  304. {
  305. int cIndex = 0;
  306. collEnum = collection.GetEnumerator();
  307. while(cIndex < start)
  308. {
  309. collEnum.MoveNext();
  310. }
  311. }
  312. collEnum.MoveNext();
  313. index++;
  314. return (start + index < max);
  315. }
  316. public void Reset()
  317. {
  318. index = -1;
  319. collEnum = null;
  320. }
  321. public object Current
  322. {
  323. get
  324. {
  325. return collEnum.Current;
  326. }
  327. }
  328. }
  329. class PrivateArrayEnumerator : IEnumerator
  330. {
  331. private int index;
  332. private int start;
  333. private int max;
  334. private object[] values;
  335. public PrivateArrayEnumerator(object[] values, int start, int count)
  336. {
  337. this.values = values;
  338. this.start = start;
  339. index = -1;
  340. max = start + count;
  341. if(max > this.values.Length)
  342. {
  343. max = this.values.Length;
  344. }
  345. }
  346. public bool MoveNext()
  347. {
  348. index++;
  349. return (index + start < max);
  350. }
  351. public void Reset()
  352. {
  353. index = -1;
  354. }
  355. public object Current
  356. {
  357. get
  358. {
  359. if(index >= 0)
  360. {
  361. return values[index + start];
  362. }
  363. throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
  364. }
  365. }
  366. }
  367. class PrivateListEnumerator : IEnumerator
  368. {
  369. private int index;
  370. private int start;
  371. private int max;
  372. private IList collection;
  373. public PrivateListEnumerator(IList list, int start, int count)
  374. {
  375. collection = list;
  376. this.start = start;
  377. index = -1;
  378. max = start + count;
  379. if(max > list.Count)
  380. {
  381. max = list.Count;
  382. }
  383. }
  384. public bool MoveNext()
  385. {
  386. index++;
  387. return (index + start < max);
  388. }
  389. public void Reset()
  390. {
  391. index = -1;
  392. }
  393. public object Current
  394. {
  395. get
  396. {
  397. if(index >= 0)
  398. {
  399. return collection[index + start];
  400. }
  401. throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
  402. }
  403. }
  404. }
  405. public string GetListName(PropertyDescriptor[] listAccessors)
  406. {
  407. return String.Empty;
  408. }
  409. public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
  410. {
  411. if(dataSource != null)
  412. {
  413. if(dataSource is ITypedList)
  414. {
  415. return ((ITypedList)dataSource).GetItemProperties(listAccessors);
  416. }
  417. }
  418. return null;
  419. }
  420. }
  421. }