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