* SiteMapNode.cs (GetExplicitResourceString): implement.
[mono-project.git] / mcs / class / System.Web / System.Web.UI.WebControls / PagedDataSource.cs
blob15eb78b0865fb0e554815ca15814cfc7eba896c7
1 // System.Web.UI.WebControls.PagedDataSource.cs
2 //
3 // Author: Duncan Mak (duncan@novell.com)
4 // Jackson Harper (jackson@ximian.com)
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.
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;
40 IEnumerable source;
42 #if NET_2_0
43 bool allow_server_paging;
44 #endif
46 public PagedDataSource ()
48 page_size = 10;
51 public bool AllowCustomPaging {
52 get { return allow_custom_paging; }
53 set {
54 allow_custom_paging = value;
55 #if NET_2_0
56 // AllowCustomPaging and AllowServerPaging are mutually exclusive
57 if (allow_custom_paging)
58 allow_server_paging = false;
59 #endif
63 public bool AllowPaging {
64 get { return allow_paging; }
65 set { allow_paging = value; }
68 public int Count {
69 get {
70 if (source == null)
71 return 0;
73 if (IsPagingEnabled) {
74 if (IsCustomPagingEnabled || !IsLastPage)
75 return page_size;
76 return DataSourceCount - FirstIndexInPage;
79 return DataSourceCount;
83 public int CurrentPageIndex {
84 get { return current_page_index; }
85 set { current_page_index = value; }
88 public IEnumerable DataSource {
89 get { return source; }
90 set { source = value; }
93 public int DataSourceCount {
94 get {
95 if (source == null)
96 return 0;
98 if (IsCustomPagingEnabled
99 #if NET_2_0
100 || IsServerPagingEnabled
101 #endif
103 return virtual_count;
105 if (source is ICollection)
106 return ((ICollection) source).Count;
108 throw new HttpException ("The data source must implement ICollection");
112 public int FirstIndexInPage {
113 get {
114 if (!IsPagingEnabled || IsCustomPagingEnabled ||
115 #if NET_2_0
116 IsServerPagingEnabled ||
117 #endif
118 source == null)
119 return 0;
121 return current_page_index * page_size;
125 public bool IsCustomPagingEnabled {
126 get { return IsPagingEnabled && allow_custom_paging; }
129 #if NET_2_0
130 public bool IsServerPagingEnabled {
131 get { return IsPagingEnabled && allow_server_paging; }
133 #endif
135 public bool IsFirstPage {
136 get {
137 if (!allow_paging)
138 return true;
140 return current_page_index == 0;
144 public bool IsLastPage {
145 get {
146 if (!allow_paging || page_size == 0)
147 return true;
149 return (current_page_index == (PageCount - 1));
153 public bool IsPagingEnabled {
154 get { return (allow_paging && page_size != 0); }
157 public bool IsReadOnly {
158 get { return false; } // as documented
161 public bool IsSynchronized {
162 get { return false; } // as documented
165 public int PageCount {
166 get {
167 if (source == null)
168 return 0;
170 if (!IsPagingEnabled || DataSourceCount == 0 || page_size == 0)
171 return 1;
173 return (DataSourceCount + page_size - 1) / page_size;
177 public int PageSize {
178 get { return page_size; }
179 set { page_size = value; }
182 public object SyncRoot {
183 get { return this; }
186 public int VirtualCount {
187 get { return virtual_count; }
188 set { virtual_count = value; }
190 #if NET_2_0
191 public bool AllowServerPaging {
192 get {
193 return allow_server_paging;
195 set {
196 allow_server_paging = value;
197 // AllowCustomPaging and AllowServerPaging are mutually exclusive
198 if (allow_server_paging)
199 allow_custom_paging = false;
202 #endif
204 public void CopyTo (Array array, int index)
206 foreach (object o in source)
207 array.SetValue (o, index++);
210 public IEnumerator GetEnumerator ()
212 // IList goes first, as it implements ICollection
213 IList list = source as IList;
214 int first = 0;
215 int count;
216 int limit;
217 if (list != null) {
218 first = FirstIndexInPage;
219 count = ((ICollection) source).Count;
220 limit = ((first + page_size) > count) ? (count - first) : page_size;
221 return GetListEnum (list, first, first + limit);
224 ICollection col = source as ICollection;
225 if (col != null) {
226 first = FirstIndexInPage;
227 count = col.Count;
228 limit = ((first + page_size) > count) ? (count - first) : page_size;
229 return GetEnumeratorEnum (col.GetEnumerator (), first, first + page_size);
232 return source.GetEnumerator ();
235 public PropertyDescriptorCollection GetItemProperties (PropertyDescriptor [] list_accessors)
237 ITypedList typed = source as ITypedList;
238 if (typed == null)
239 return null;
240 return typed.GetItemProperties (list_accessors);
243 public string GetListName (PropertyDescriptor [] list_accessors)
245 return String.Empty; // as documented
248 private IEnumerator GetListEnum (IList list, int start, int end)
250 if (!AllowPaging)
251 end = list.Count;
252 else if (start >= list.Count)
253 yield break;
255 for (int i = start; i < end; i++)
256 yield return list [i];
259 private IEnumerator GetEnumeratorEnum (IEnumerator e, int start, int end)
261 for (int i = 0; i < start; i++)
262 e.MoveNext ();
263 for (int i = start; (!allow_paging || i < end) && e.MoveNext (); i++)
264 yield return e.Current;