1 // System.Web.UI.WebControls.PagedDataSource.cs
3 // Author: Duncan Mak (duncan@novell.com)
4 // Jackson Harper (jackson@ximian.com)
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections;
30 using System.ComponentModel;
32 namespace System.Web.UI.WebControls {
33 public sealed class PagedDataSource : ICollection, IEnumerable, ITypedList
35 int page_size, current_page_index, virtual_count;
36 bool allow_paging, allow_custom_paging;
39 public PagedDataSource ()
43 current_page_index = 0;
44 allow_custom_paging = false;
48 public bool AllowCustomPaging {
49 get { return allow_custom_paging; }
50 set { allow_custom_paging = value; }
53 public bool AllowPaging {
54 get { return allow_paging; }
55 set { allow_paging = value; }
60 if (DataSource == null)
63 if (IsPagingEnabled == true) {
64 if (IsCustomPagingEnabled == true || IsLastPage == false)
67 if (IsCustomPagingEnabled == false && IsLastPage == true)
68 return FirstIndexInPage - DataSourceCount;
71 return DataSourceCount;
75 public int CurrentPageIndex {
76 get { return current_page_index; }
77 set { current_page_index = value; }
80 public IEnumerable DataSource {
81 get { return source; }
82 set { source = value; }
85 public int DataSourceCount {
90 if (IsCustomPagingEnabled)
93 if (source is ICollection)
94 return ((ICollection) source).Count;
96 throw new HttpException ("The data source must implement ICollection");
100 public int FirstIndexInPage {
102 if (!IsPagingEnabled || IsCustomPagingEnabled || DataSource == null)
105 return current_page_index * page_size;
109 public bool IsCustomPagingEnabled {
110 get { return IsPagingEnabled && AllowCustomPaging; }
113 public bool IsFirstPage {
118 return CurrentPageIndex == 0;
122 public bool IsLastPage {
130 return CurrentPageIndex == PageCount - 1;
134 public bool IsPagingEnabled {
135 get { return AllowPaging && (PageSize != 0); }
138 public bool IsReadOnly {
139 get { return false; } // as documented
142 public bool IsSynchronized {
143 get { return false; } // as documented
146 public int PageCount {
148 if (DataSource == null)
151 if (!IsPagingEnabled || DataSourceCount == 0 || PageSize == 0)
154 return (DataSourceCount + PageSize - 1) / PageSize;
158 public int PageSize {
159 get { return page_size; }
160 set { page_size = value; }
163 public object SyncRoot {
167 public int VirtualCount {
168 get { return virtual_count; }
169 set { virtual_count = value; }
173 public bool AllowServerPaging {
175 throw new NotImplementedException ();
178 throw new NotImplementedException ();
183 public DataSourceSelectArguments DataSourceSelectArguments {
185 throw new NotImplementedException ();
188 throw new NotImplementedException ();
192 public DataSourceView DataSourceView {
194 throw new NotImplementedException ();
197 throw new NotImplementedException ();
202 public void SetItemCountFromPageIndex (int highestPageIndex)
204 throw new NotImplementedException ();
208 public void CopyTo (Array array, int index)
210 foreach (object o in source)
211 array.SetValue (o, index++);
214 public IEnumerator GetEnumerator ()
216 ICollection col = source as ICollection;
218 return GetEnumeratorEnum (col.GetEnumerator (),
219 FirstIndexInPage, FirstIndexInPage + PageSize);
221 IList list = source as IList;
223 return GetListEnum (list, FirstIndexInPage,
224 FirstIndexInPage + PageSize);
226 return source.GetEnumerator ();
229 public PropertyDescriptorCollection GetItemProperties (
230 PropertyDescriptor [] list_accessors)
232 ITypedList typed = DataSource as ITypedList;
235 return typed.GetItemProperties (list_accessors);
238 public string GetListName (PropertyDescriptor [] list_accessors)
240 return String.Empty; // as documented
243 private IEnumerator GetListEnum (IList list, int start, int end)
247 for (int i = start; i < end; i++)
248 yield return list [i];
251 private IEnumerator GetEnumeratorEnum (IEnumerator e, int start, int end)
253 for (int i = 0; i < start; i++)
255 for (int i = start; (!allow_paging || i < end) && e.MoveNext (); i++)
256 yield return e.Current;