PagedDataSource.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // System.Web.UI.WebControls.PagedDataSource.cs
  2. //
  3. // Author: Duncan Mak ([email protected])
  4. // Jackson Harper ([email protected])
  5. //
  6. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System.Collections;
  28. using System.ComponentModel;
  29. using System.Security.Permissions;
  30. namespace System.Web.UI.WebControls {
  31. // CAS - no inheritance demand required because the class is sealed
  32. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  33. public sealed class PagedDataSource : ICollection, IEnumerable, ITypedList
  34. {
  35. int page_size, current_page_index, virtual_count;
  36. bool allow_paging, allow_custom_paging;
  37. IEnumerable source;
  38. public PagedDataSource ()
  39. {
  40. page_size = 10;
  41. }
  42. public bool AllowCustomPaging {
  43. get { return allow_custom_paging; }
  44. set { allow_custom_paging = value; }
  45. }
  46. public bool AllowPaging {
  47. get { return allow_paging; }
  48. set { allow_paging = value; }
  49. }
  50. public int Count {
  51. get {
  52. if (source == null)
  53. return 0;
  54. if (IsPagingEnabled) {
  55. if (IsCustomPagingEnabled || !IsLastPage)
  56. return page_size;
  57. return DataSourceCount - FirstIndexInPage;
  58. }
  59. return DataSourceCount;
  60. }
  61. }
  62. public int CurrentPageIndex {
  63. get { return current_page_index; }
  64. set { current_page_index = value; }
  65. }
  66. public IEnumerable DataSource {
  67. get { return source; }
  68. set { source = value; }
  69. }
  70. public int DataSourceCount {
  71. get {
  72. if (source == null)
  73. return 0;
  74. if (IsCustomPagingEnabled)
  75. return virtual_count;
  76. if (source is ICollection)
  77. return ((ICollection) source).Count;
  78. throw new HttpException ("The data source must implement ICollection");
  79. }
  80. }
  81. public int FirstIndexInPage {
  82. get {
  83. if (!IsPagingEnabled || IsCustomPagingEnabled || source == null)
  84. return 0;
  85. return current_page_index * page_size;
  86. }
  87. }
  88. public bool IsCustomPagingEnabled {
  89. get { return IsPagingEnabled && allow_custom_paging; }
  90. }
  91. public bool IsFirstPage {
  92. get {
  93. if (!allow_paging)
  94. return true;
  95. return current_page_index == 0;
  96. }
  97. }
  98. public bool IsLastPage {
  99. get {
  100. if (!allow_paging || page_size == 0)
  101. return true;
  102. return (current_page_index == (PageCount - 1));
  103. }
  104. }
  105. public bool IsPagingEnabled {
  106. get { return (allow_paging && page_size != 0); }
  107. }
  108. public bool IsReadOnly {
  109. get { return false; } // as documented
  110. }
  111. public bool IsSynchronized {
  112. get { return false; } // as documented
  113. }
  114. public int PageCount {
  115. get {
  116. if (source == null)
  117. return 0;
  118. if (!IsPagingEnabled || DataSourceCount == 0 || page_size == 0)
  119. return 1;
  120. return (DataSourceCount + page_size - 1) / page_size;
  121. }
  122. }
  123. public int PageSize {
  124. get { return page_size; }
  125. set { page_size = value; }
  126. }
  127. public object SyncRoot {
  128. get { return this; }
  129. }
  130. public int VirtualCount {
  131. get { return virtual_count; }
  132. set { virtual_count = value; }
  133. }
  134. #if NET_2_0
  135. [MonoTODO]
  136. public bool AllowServerPaging {
  137. get {
  138. throw new NotImplementedException ();
  139. }
  140. set {
  141. throw new NotImplementedException ();
  142. }
  143. }
  144. [MonoTODO]
  145. public DataSourceSelectArguments DataSourceSelectArguments {
  146. get {
  147. throw new NotImplementedException ();
  148. }
  149. set {
  150. throw new NotImplementedException ();
  151. }
  152. }
  153. [MonoTODO]
  154. public DataSourceView DataSourceView {
  155. get {
  156. throw new NotImplementedException ();
  157. }
  158. set {
  159. throw new NotImplementedException ();
  160. }
  161. }
  162. [MonoTODO]
  163. public void SetItemCountFromPageIndex (int highestPageIndex)
  164. {
  165. throw new NotImplementedException ();
  166. }
  167. #endif
  168. public void CopyTo (Array array, int index)
  169. {
  170. foreach (object o in source)
  171. array.SetValue (o, index++);
  172. }
  173. public IEnumerator GetEnumerator ()
  174. {
  175. // IList goes first, as it implements ICollection
  176. IList list = source as IList;
  177. int first = 0;
  178. if (list != null) {
  179. first = FirstIndexInPage;
  180. return GetListEnum (list, first, first + page_size);
  181. }
  182. ICollection col = source as ICollection;
  183. if (col != null) {
  184. first = FirstIndexInPage;
  185. return GetEnumeratorEnum (col.GetEnumerator (), first, first + page_size);
  186. }
  187. return source.GetEnumerator ();
  188. }
  189. public PropertyDescriptorCollection GetItemProperties (PropertyDescriptor [] list_accessors)
  190. {
  191. ITypedList typed = source as ITypedList;
  192. if (typed == null)
  193. return null;
  194. return typed.GetItemProperties (list_accessors);
  195. }
  196. public string GetListName (PropertyDescriptor [] list_accessors)
  197. {
  198. return String.Empty; // as documented
  199. }
  200. private IEnumerator GetListEnum (IList list, int start, int end)
  201. {
  202. if (!allow_paging)
  203. end = list.Count;
  204. else if (start >= list.Count)
  205. yield break;
  206. for (int i = start; i < end; i++)
  207. yield return list [i];
  208. }
  209. private IEnumerator GetEnumeratorEnum (IEnumerator e, int start, int end)
  210. {
  211. for (int i = 0; i < start; i++)
  212. e.MoveNext ();
  213. for (int i = start; (!allow_paging || i < end) && e.MoveNext (); i++)
  214. yield return e.Current;
  215. }
  216. }
  217. }