merge -r 53370:58178
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / SiteMapDataSource.cs
1 //
2 // System.Web.UI.WebControls.SiteMapDataSource.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
33 using System;
34 using System.Collections;
35 using System.Web.UI;
36 using System.Web.Util;
37 using System.ComponentModel;
38
39 namespace System.Web.UI.WebControls
40 {
41         [PersistChildrenAttribute (false)]
42         [DesignerAttribute ("System.Web.UI.Design.WebControls.SiteMapDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43         [ParseChildrenAttribute (true)]
44         public class SiteMapDataSource : HierarchicalDataSourceControl, IDataSource, IListSource
45         {
46                 static string[] emptyNames = new string[] { string.Empty };
47                 
48                 SiteMapProvider provider;
49                 
50                 public virtual ICollection GetViewNames ()
51                 {
52                         return emptyNames;
53                 }
54                 
55                 public IList GetList ()
56                 {
57                         return ListSourceHelper.GetList (this);
58                 }
59                 
60             [BrowsableAttribute (false)]\r
61             [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]\r
62                 public bool ContainsListCollection {
63                         get { return ListSourceHelper.ContainsListCollection (this); }
64                 }
65                 
66                 event EventHandler IDataSource.DataSourceChanged {
67                         add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
68                         remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
69                 }
70                 
71             [BrowsableAttribute (false)]\r
72             [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]\r
73                 public SiteMapProvider Provider {
74                         get {
75                                 if (provider == null) {
76                                         if (this.SiteMapProvider.Length == 0) {
77                                                 provider = SiteMap.Provider;
78                                                 if (provider == null)
79                                                         throw new HttpException ("There is no default provider configured for the site.");
80                                         } else {
81                                                 provider = SiteMap.Providers [this.SiteMapProvider];
82                                                 if (provider == null)
83                                                         throw new HttpException ("SiteMap provider '" + this.SiteMapProvider + "' not found.");
84                                         }
85                                 }
86                                 return provider;
87                         }
88                         set {
89                                 provider = value;
90                                 OnDataSourceChanged (EventArgs.Empty);
91                         }
92                 }
93                 
94             [DefaultValueAttribute ("")]\r
95                 public string SiteMapProvider {
96                         get {
97                                 object o = ViewState ["SiteMapProvider"];
98                                 if (o != null) return (string) o;
99                                 else return string.Empty;
100                         }
101                         set {
102                                 ViewState ["SiteMapProvider"] = value;
103                                 OnDataSourceChanged (EventArgs.Empty);
104                         }
105                 }
106                 
107             [DefaultValueAttribute ("")]\r
108             [EditorAttribute ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]\r
109             [UrlPropertyAttribute]\r
110                 public string StartingNodeUrl {
111                         get {
112                                 object o = ViewState ["StartingNodeUrl"];
113                                 if (o != null) return (string) o;
114                                 else return string.Empty;
115                         }
116                         set {
117                                 ViewState ["StartingNodeUrl"] = value;
118                                 OnDataSourceChanged (EventArgs.Empty);
119                         }
120                 }
121                 
122             [DefaultValueAttribute (false)]\r
123                 public bool StartFromCurrentNode {
124                         get {
125                                 object o = ViewState ["StartFromCurrentNode"];
126                                 if (o != null) return (bool) o;
127                                 else return false;
128                         }
129                         set {
130                                 ViewState ["StartFromCurrentNode"] = value;
131                                 OnDataSourceChanged (EventArgs.Empty);
132                         }
133                 }
134                 
135             [DefaultValueAttribute (true)]\r
136                 public bool ShowStartingNode {
137                         get {
138                                 object o = ViewState ["ShowStartingNode"];
139                                 if (o != null) return (bool) o;
140                                 else return true;
141                         }
142                         set {
143                                 ViewState ["ShowStartingNode"] = value;
144                                 OnDataSourceChanged (EventArgs.Empty);
145                         }
146                 }
147
148                 public DataSourceView GetView (string viewName)
149                 {
150                         SiteMapNode node = GetStartNode (viewName);
151                         if (node == null)
152                                 return new SiteMapDataSourceView (this, viewName, SiteMapNodeCollection.EmptyList);
153                         else if (ShowStartingNode)
154                                 return new SiteMapDataSourceView (this, viewName, node);
155                         else
156                                 return new SiteMapDataSourceView (this, viewName, node.ChildNodes);
157                 }
158
159                 protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
160                 {
161                         SiteMapNode node = GetStartNode (viewPath);
162                         if (node == null)
163                                 return new SiteMapHierarchicalDataSourceView (SiteMapNodeCollection.EmptyList);
164                         else if (ShowStartingNode || node == null)
165                                 return new SiteMapHierarchicalDataSourceView (node);
166                         else
167                                 return new SiteMapHierarchicalDataSourceView (node.ChildNodes);
168                 }
169                 
170                 SiteMapNode GetStartNode (string viewPath)
171                 {
172                         if (viewPath != null && viewPath.Length != 0) {
173                                 string url = MapUrl (StartingNodeUrl);
174                                 return Provider.FindSiteMapNode (url);
175                         }
176                         else if (StartFromCurrentNode) {
177                                 if (StartingNodeUrl.Length != 0)
178                                         throw new InvalidOperationException ("StartingNodeUrl can't be set if StartFromCurrentNode is set to true.");
179                                 return Provider.CurrentNode;
180                         }
181                         else if (StartingNodeUrl.Length != 0) {
182                                 string url = MapUrl (StartingNodeUrl);
183                                 SiteMapNode node = Provider.FindSiteMapNode (url);
184                                 if (node == null) throw new ArgumentException ("Can't find a site map node for the url: " + StartingNodeUrl);
185                                 return node;
186                         }
187                         else
188                                 return Provider.RootNode;
189                 }
190                 
191                 string MapUrl (string url)
192                 {
193                         if (UrlUtils.IsRelativeUrl (url))
194                                 return UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
195                         else
196                                 return UrlUtils.ResolveVirtualPathFromAppAbsolute (url);
197                 }
198         }
199 }
200
201 #endif
202