ListViewPagedDataSource.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // System.Web.UI.WebControls.ListView
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2007-2008 Novell, Inc
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_3_5
  30. using System;
  31. using System.Collections;
  32. using System.ComponentModel;
  33. namespace System.Web.UI.WebControls
  34. {
  35. public class ListViewPagedDataSource : ICollection, IEnumerable, ITypedList
  36. {
  37. sealed class ListEnumerator : IEnumerator
  38. {
  39. int index;
  40. int startIndex;
  41. int end;
  42. IList list;
  43. public object Current {
  44. get { return list [startIndex + index]; }
  45. }
  46. public ListEnumerator (IList list, int startIndex, int end)
  47. {
  48. this.list = list;
  49. this.index = -1;
  50. this.startIndex = startIndex;
  51. this.end = startIndex + end;
  52. }
  53. public bool MoveNext ()
  54. {
  55. index++;
  56. return (startIndex + index) < end;
  57. }
  58. // See:
  59. // http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewpageddatasource.getenumerator.aspx
  60. // (Note 1)
  61. public void Reset ()
  62. {
  63. index = -1;
  64. }
  65. }
  66. sealed class CollectionEnumerator : IEnumerator
  67. {
  68. int index;
  69. int startIndex;
  70. int end;
  71. ICollection collection;
  72. IEnumerator enumerator;
  73. public object Current {
  74. get {
  75. if (enumerator != null)
  76. return enumerator.Current;
  77. return null;
  78. }
  79. }
  80. public CollectionEnumerator (ICollection collection, int startIndex, int end)
  81. {
  82. this.collection = collection;
  83. this.index = -1;
  84. this.startIndex = startIndex;
  85. this.end = end;
  86. }
  87. public bool MoveNext ()
  88. {
  89. if (enumerator == null) {
  90. enumerator = collection.GetEnumerator ();
  91. for (int i = 0; i < startIndex; i++)
  92. enumerator.MoveNext ();
  93. }
  94. index++;
  95. enumerator.MoveNext ();
  96. return (startIndex + index) < end;
  97. }
  98. // See:
  99. // http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewpageddatasource.getenumerator.aspx
  100. // (Note 1)
  101. public void Reset ()
  102. {
  103. index = -1;
  104. enumerator = null;
  105. }
  106. }
  107. public ListViewPagedDataSource ()
  108. {
  109. StartRowIndex = 0;
  110. MaximumRows = 0;
  111. AllowServerPaging = false;
  112. TotalRowCount = 0;
  113. }
  114. public void CopyTo (Array array, int index)
  115. {
  116. }
  117. public IEnumerator GetEnumerator ()
  118. {
  119. IEnumerable ds = DataSource;
  120. if (ds == null)
  121. return null;
  122. IList list = ds as IList;
  123. if (list != null)
  124. return new ListEnumerator (list, AllowServerPaging ? 0 : StartRowIndex, Count);
  125. ICollection collection = ds as ICollection;
  126. if (collection != null)
  127. return new CollectionEnumerator (collection, AllowServerPaging ? 0 : StartRowIndex, Count);
  128. return ds.GetEnumerator ();
  129. }
  130. public PropertyDescriptorCollection GetItemProperties (PropertyDescriptor [] listAccessors)
  131. {
  132. IEnumerable ds = DataSource;
  133. if (ds == null || !(ds is ITypedList))
  134. return null;
  135. return ((ITypedList) ds).GetItemProperties (listAccessors);
  136. }
  137. public string GetListName (PropertyDescriptor [] listAccessors)
  138. {
  139. return String.Empty;
  140. }
  141. public bool AllowServerPaging {
  142. get;
  143. set;
  144. }
  145. public int Count {
  146. get {
  147. IEnumerable ds = DataSource;
  148. if (ds == null)
  149. return 0;
  150. bool onLastPage = OnLastPage;
  151. int maxRows = MaximumRows;
  152. if (!onLastPage && maxRows >= 0)
  153. return maxRows;
  154. // LAMESPEC: MSDN says that DataSourceCount should be subtracted
  155. // from StartRowIndex
  156. return DataSourceCount - StartRowIndex;
  157. }
  158. }
  159. public IEnumerable DataSource {
  160. get;
  161. set;
  162. }
  163. public int DataSourceCount {
  164. get {
  165. IEnumerable ds = DataSource;
  166. if (ds == null)
  167. return 0;
  168. if (!(ds is ICollection))
  169. throw new InvalidOperationException ("The data source object does not implement the System.Collections..::.ICollection interface.");
  170. if (IsServerPagingEnabled)
  171. return TotalRowCount;
  172. return ((ICollection) ds).Count;
  173. }
  174. }
  175. public bool IsReadOnly {
  176. get { return false; }
  177. }
  178. public bool IsServerPagingEnabled {
  179. get { return AllowServerPaging; }
  180. }
  181. public bool IsSynchronized {
  182. get { return false; }
  183. }
  184. public int MaximumRows {
  185. get;
  186. set;
  187. }
  188. public int StartRowIndex {
  189. get;
  190. set;
  191. }
  192. public object SyncRoot {
  193. get { return this; }
  194. }
  195. public int TotalRowCount {
  196. get;
  197. set;
  198. }
  199. bool OnLastPage {
  200. get { return ((StartRowIndex + MaximumRows) >= DataSourceCount); }
  201. }
  202. }
  203. }
  204. #endif