Mark tests as not working under TARGET_JVM
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / HierarchicalDataBoundControl.cs
1 //
2 // System.Web.UI.WebControls.HierarchicalDataBoundControl.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2004 Novell, Inc (http://www.novell.com)
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.ComponentModel;
34
35 namespace System.Web.UI.WebControls
36 {
37         [DesignerAttribute ("System.Web.UI.Design.WebControls.HierarchicalDataBoundControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
38         public abstract class HierarchicalDataBoundControl : BaseDataBoundControl
39         {
40                 [IDReferencePropertyAttribute (typeof(HierarchicalDataSourceControl))]
41                 public override string DataSourceID {
42                         get {
43                                 object o = ViewState ["DataSourceID"];
44                                 if (o != null)
45                                         return (string)o;
46                                 
47                                 return String.Empty;
48                         }
49                         set {
50                                 if (Initialized)
51                                         RequiresDataBinding = true;
52                                 
53                                 ViewState ["DataSourceID"] = value;
54                         }
55                 }
56                 
57                 protected virtual HierarchicalDataSourceView GetData (string viewPath)
58                 {
59                         if (DataSource != null && DataSourceID != "")
60                                 throw new HttpException ();
61                         
62                         IHierarchicalDataSource ds = GetDataSource ();
63                         if (ds != null)
64                                 return ds.GetHierarchicalView (viewPath);
65                         else
66                                 return null; 
67                 }
68                 
69                 protected virtual IHierarchicalDataSource GetDataSource ()
70                 {
71                         if (IsBoundUsingDataSourceID) {
72                                 
73                                 Control ctrl = FindDataSource ();
74
75                                 if (ctrl == null)
76                                         throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
77                                 if (!(ctrl is IHierarchicalDataSource))
78                                         throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IHierarchicalDataSource.", DataSourceID));
79                                 return (IHierarchicalDataSource) ctrl;
80                         }
81                         
82                         return DataSource as IHierarchicalDataSource;
83                 }
84
85                 bool IsDataBound {
86                         get {
87                                 return ViewState.GetBool ("DataBound", false);
88                         }
89                         set {
90                                 ViewState ["DataBound"] = value;
91                         }
92                 }
93
94                 protected void MarkAsDataBound ()
95                 {
96                         IsDataBound = true;
97                 }
98                 
99                 protected override void OnDataPropertyChanged ()
100                 {
101                         RequiresDataBinding = true;
102                 }
103                 
104                 protected virtual void OnDataSourceChanged (object sender, EventArgs e)
105                 {
106                         RequiresDataBinding = true;
107                 }
108
109                 protected internal override void OnLoad (EventArgs e)
110                 {
111                         if (!Initialized) {
112                                 Initialize ();
113                                 ConfirmInitState ();
114                         }
115                         
116                         base.OnLoad(e);
117                 }
118
119                 private void Initialize ()
120                 {
121                         if (!Page.IsPostBack || (IsViewStateEnabled && !IsDataBound))
122                                 RequiresDataBinding = true;
123
124                         IHierarchicalDataSource ds = GetDataSource ();
125                         if (ds != null && DataSourceID != "")
126                                 ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
127                 }
128
129                 protected override void OnPagePreLoad (object sender, EventArgs e)
130                 {
131                         base.OnPagePreLoad (sender, e);
132                         
133                         Initialize ();
134                 }
135                 
136                 protected internal virtual void PerformDataBinding ()
137                 {
138                 }
139                 
140                 protected override void PerformSelect ()
141                 {
142                         OnDataBinding (EventArgs.Empty);
143                         PerformDataBinding ();
144                         // The PerformDataBinding method has completed.
145                         RequiresDataBinding = false;
146                         MarkAsDataBound ();
147                         OnDataBound (EventArgs.Empty);
148                 }
149                 
150                 protected override void ValidateDataSource (object dataSource)
151                 {
152                         if (dataSource == null || dataSource is IHierarchicalDataSource || dataSource is IHierarchicalEnumerable)
153                                 return;
154                         throw new InvalidOperationException ("Invalid data source");
155                 }
156         }
157 }
158 #endif
159
160