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 ()
46 current_page_index = 0;
47 allow_custom_paging = false;
51 public bool AllowCustomPaging {
52 get { return allow_custom_paging; }
53 set { allow_custom_paging = value; }
56 public bool AllowPaging {
57 get { return allow_paging; }
58 set { allow_paging = value; }
63 if (DataSource == null)
66 if (IsPagingEnabled == true) {
67 if (IsCustomPagingEnabled == true || IsLastPage == false)
70 if (IsCustomPagingEnabled == false && IsLastPage == true)
71 return FirstIndexInPage - DataSourceCount;
74 return DataSourceCount;
78 public int CurrentPageIndex {
79 get { return current_page_index; }
80 set { current_page_index = value; }
83 public IEnumerable DataSource {
84 get { return source; }
85 set { source = value; }
88 public int DataSourceCount {
93 if (IsCustomPagingEnabled)
96 if (source is ICollection)
97 return ((ICollection) source).Count;
99 throw new HttpException ("The data source must implement ICollection");
103 public int FirstIndexInPage {
105 if (!IsPagingEnabled || IsCustomPagingEnabled || DataSource == null)
108 return current_page_index * page_size;
112 public bool IsCustomPagingEnabled {
113 get { return IsPagingEnabled && AllowCustomPaging; }
116 public bool IsFirstPage {
121 return CurrentPageIndex == 0;
125 public bool IsLastPage {
133 return CurrentPageIndex == PageCount - 1;
137 public bool IsPagingEnabled {
138 get { return AllowPaging && (PageSize != 0); }
141 public bool IsReadOnly {
142 get { return false; } // as documented
145 public bool IsSynchronized {
146 get { return false; } // as documented
149 public int PageCount {
151 if (DataSource == null)
154 if (!IsPagingEnabled || DataSourceCount == 0 || PageSize == 0)
157 return (DataSourceCount + PageSize - 1) / PageSize;
161 public int PageSize {
162 get { return page_size; }
163 set { page_size = value; }
166 public object SyncRoot {
170 public int VirtualCount {
171 get { return virtual_count; }
172 set { virtual_count = value; }
176 public bool AllowServerPaging {
178 throw new NotImplementedException ();
181 throw new NotImplementedException ();
186 public DataSourceSelectArguments DataSourceSelectArguments {
188 throw new NotImplementedException ();
191 throw new NotImplementedException ();
195 public DataSourceView DataSourceView {
197 throw new NotImplementedException ();
200 throw new NotImplementedException ();
205 public void SetItemCountFromPageIndex (int highestPageIndex)
207 throw new NotImplementedException ();
211 public void CopyTo (Array array, int index)
213 foreach (object o in source)
214 array.SetValue (o, index++);
217 public IEnumerator GetEnumerator ()
219 ICollection col = source as ICollection;
221 return GetEnumeratorEnum (col.GetEnumerator (),
222 FirstIndexInPage, FirstIndexInPage + PageSize);
224 IList list = source as IList;
226 return GetListEnum (list, FirstIndexInPage,
227 FirstIndexInPage + PageSize);
229 return source.GetEnumerator ();
232 public PropertyDescriptorCollection GetItemProperties (
233 PropertyDescriptor [] list_accessors)
235 ITypedList typed = DataSource 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 for (int i = start; i < end; i++)
251 yield return list [i];
254 private IEnumerator GetEnumeratorEnum (IEnumerator e, int start, int end)
256 for (int i = 0; i < start; i++)
258 for (int i = start; (!allow_paging || i < end) && e.MoveNext (); i++)
259 yield return e.Current;