Merge pull request #131 from txdv/master
[mono.git] / mcs / class / System.Web / System.Web.UI / ListSourceHelper.cs
1 //
2 // System.Web.UI.ListSourceHelper
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
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
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Text;
34 using System.ComponentModel;
35 using System.Collections.Generic;
36
37 namespace System.Web.UI {
38         public static class ListSourceHelper {
39                 
40                 public static bool ContainsListCollection (IDataSource dataSource)
41                 {
42                         return dataSource.GetViewNames ().Count > 0;
43                 }
44                 
45                 public static IList GetList (IDataSource dataSource)
46                 {
47                         if (dataSource.GetViewNames ().Count == 0)
48                                 return null;
49
50                         ListSourceList list = new ListSourceList ();
51                         list.Add (dataSource);
52                         return list;
53                 }
54
55                 sealed class ListSourceList : List<IDataSource>, ITypedList
56                 {
57                         #region ITypedList Members
58
59                         PropertyDescriptorCollection ITypedList.GetItemProperties (PropertyDescriptor [] listAccessors) {
60                                 ICollection viewNames = this [0].GetViewNames ();
61                                 PropertyDescriptor [] a = new PropertyDescriptor [viewNames.Count];
62                                 int i = 0;
63                                 foreach (string viewName in viewNames) {
64                                         a[i++] = new ListSourcePropertyDescriptor (viewName, null);
65                                 }
66                                 return new PropertyDescriptorCollection (a);
67                         }
68
69                         string ITypedList.GetListName (PropertyDescriptor [] listAccessors) {
70                                 return String.Empty;
71                         }
72
73                         #endregion
74                 }
75
76                 sealed class ListSourcePropertyDescriptor : PropertyDescriptor
77                 {
78                         public ListSourcePropertyDescriptor (MemberDescriptor descr)
79                                 : base (descr) {
80                         }
81
82                         public ListSourcePropertyDescriptor (string name, Attribute [] attrs)
83                                 : base (name, attrs) {
84                         }
85
86                         public ListSourcePropertyDescriptor (MemberDescriptor descr, Attribute [] attrs)
87                                 : base (descr, attrs) {
88                         }
89
90                         public override bool CanResetValue (object component) {
91                                 throw new NotImplementedException ();
92                         }
93
94                         public override Type ComponentType {
95                                 get { throw new NotImplementedException (); }
96                         }
97
98                         public override object GetValue (object component) {
99                                 IDataSource dataSource = component as IDataSource;
100                                 if (dataSource == null)
101                                         return null;
102
103                                 DataSourceView view = dataSource.GetView (Name);
104                                 return view.ExecuteSelect (DataSourceSelectArguments.Empty);
105                         }
106
107                         public override bool IsReadOnly {
108                                 get { return true; }
109                         }
110
111                         public override Type PropertyType {
112                                 get { return typeof(IEnumerable); }
113                         }
114
115                         public override void ResetValue (object component) {
116                                 throw new NotImplementedException ();
117                         }
118
119                         public override void SetValue (object component, object value) {
120                                 throw new NotImplementedException ();
121                         }
122
123                         public override bool ShouldSerializeValue (object component) {
124                                 throw new NotImplementedException ();
125                         }
126                 }
127         }
128         
129
130 }