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