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.
28 using System.Collections;
29 using System.ComponentModel;
30 using System.Security.Permissions;
32 namespace System.Web.UI.WebControls {
34 // CAS - no inheritance demand required because the class is sealed
35 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
36 public sealed class PagedDataSource : ICollection, IEnumerable, ITypedList
38 int page_size, current_page_index, virtual_count;
39 bool allow_paging, allow_custom_paging;
42 public PagedDataSource ()
47 public bool AllowCustomPaging {
48 get { return allow_custom_paging; }
49 set { allow_custom_paging = value; }
52 public bool AllowPaging {
53 get { return allow_paging; }
54 set { allow_paging = value; }
62 if (IsPagingEnabled) {
63 if (IsCustomPagingEnabled || !IsLastPage)
65 return DataSourceCount - FirstIndexInPage;
68 return DataSourceCount;
72 public int CurrentPageIndex {
73 get { return current_page_index; }
74 set { current_page_index = value; }
77 public IEnumerable DataSource {
78 get { return source; }
79 set { source = value; }
82 public int DataSourceCount {
87 if (IsCustomPagingEnabled)
90 if (source is ICollection)
91 return ((ICollection) source).Count;
93 throw new HttpException ("The data source must implement ICollection");
97 public int FirstIndexInPage {
99 if (!IsPagingEnabled || IsCustomPagingEnabled || source == null)
102 return current_page_index * page_size;
106 public bool IsCustomPagingEnabled {
107 get { return IsPagingEnabled && allow_custom_paging; }
110 public bool IsFirstPage {
115 return current_page_index == 0;
119 public bool IsLastPage {
121 if (!allow_paging || page_size == 0)
124 return (current_page_index == (PageCount - 1));
128 public bool IsPagingEnabled {
129 get { return (allow_paging && page_size != 0); }
132 public bool IsReadOnly {
133 get { return false; } // as documented
136 public bool IsSynchronized {
137 get { return false; } // as documented
140 public int PageCount {
145 if (!IsPagingEnabled || DataSourceCount == 0 || page_size == 0)
148 return (DataSourceCount + page_size - 1) / page_size;
152 public int PageSize {
153 get { return page_size; }
154 set { page_size = value; }
157 public object SyncRoot {
161 public int VirtualCount {
162 get { return virtual_count; }
163 set { virtual_count = value; }
167 public bool AllowServerPaging {
169 throw new NotImplementedException ();
172 throw new NotImplementedException ();
177 public DataSourceSelectArguments DataSourceSelectArguments {
179 throw new NotImplementedException ();
182 throw new NotImplementedException ();
186 public DataSourceView DataSourceView {
188 throw new NotImplementedException ();
191 throw new NotImplementedException ();
196 public void SetItemCountFromPageIndex (int highestPageIndex)
198 throw new NotImplementedException ();
202 public void CopyTo (Array array, int index)
204 foreach (object o in source)
205 array.SetValue (o, index++);
208 public IEnumerator GetEnumerator ()
210 // IList goes first, as it implements ICollection
211 IList list = source as IList;
216 first = FirstIndexInPage;
217 count = ((ICollection) source).Count;
218 limit = ((first + page_size) > count) ? (count - first) : page_size;
219 return GetListEnum (list, first, first + limit);
222 ICollection col = source as ICollection;
224 first = FirstIndexInPage;
226 limit = ((first + page_size) > count) ? (count - first) : page_size;
227 return GetEnumeratorEnum (col.GetEnumerator (), first, first + page_size);
230 return source.GetEnumerator ();
233 public PropertyDescriptorCollection GetItemProperties (PropertyDescriptor [] list_accessors)
235 ITypedList typed = source as ITypedList;
238 return typed.GetItemProperties (list_accessors);
241 public string GetListName (PropertyDescriptor [] list_accessors)
243 return String.Empty; // as documented
246 private IEnumerator GetListEnum (IList list, int start, int end)
250 else if (start >= list.Count)
253 for (int i = start; i < end; i++)
254 yield return list [i];
257 private IEnumerator GetEnumeratorEnum (IEnumerator e, int start, int end)
259 for (int i = 0; i < start; i++)
261 for (int i = start; (!allow_paging || i < end) && e.MoveNext (); i++)
262 yield return e.Current;