2008-03-13 Marek Habersack <mhabersack@novell.com>
[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 using System.Collections.Generic;
39
40 namespace System.Web.UI.WebControls
41 {
42         [PersistChildrenAttribute (false)]
43         [DesignerAttribute ("System.Web.UI.Design.WebControls.SiteMapDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
44         [ParseChildrenAttribute (true)]
45         public class SiteMapDataSource : HierarchicalDataSourceControl, IDataSource, IListSource
46         {
47                 static string[] emptyNames = new string[] { "DefaultView" };
48                 
49                 SiteMapProvider provider;
50                 
51                 public virtual ICollection GetViewNames ()
52                 {
53                         return emptyNames;
54                 }
55
56                 IList IListSource.GetList () {
57                         return GetList ();
58                 }
59
60                 bool IListSource.ContainsListCollection {
61                         get { return ContainsListCollection; }
62                 }
63
64                 public virtual IList GetList ()
65                 {
66                         return ListSourceHelper.GetList (this);
67                 }
68                 
69                 [BrowsableAttribute (false)]
70                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
71                 public virtual bool ContainsListCollection {
72                         get { return ListSourceHelper.ContainsListCollection (this); }
73                 }
74
75                 DataSourceView IDataSource.GetView (string viewName) {
76                         return GetView (viewName);
77                 }
78
79                 ICollection IDataSource.GetViewNames () {
80                         return GetViewNames ();
81                 }
82                 
83                 event EventHandler IDataSource.DataSourceChanged {
84                         add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
85                         remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
86                 }
87                 
88                 [BrowsableAttribute (false)]
89                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
90                 public SiteMapProvider Provider {
91                         get {
92                                 if (provider == null) {
93                                         if (this.SiteMapProvider.Length == 0) {
94                                                 provider = SiteMap.Provider;
95                                                 if (provider == null)
96                                                         throw new HttpException ("There is no default provider configured for the site.");
97                                         } else {
98                                                 provider = SiteMap.Providers [this.SiteMapProvider];
99                                                 if (provider == null)
100                                                         throw new HttpException ("SiteMap provider '" + this.SiteMapProvider + "' not found.");
101                                         }
102                                 }
103                                 return provider;
104                         }
105                         set {
106                                 provider = value;
107                                 OnDataSourceChanged (EventArgs.Empty);
108                         }
109                 }
110                 
111                 [DefaultValueAttribute ("")]
112                 public virtual string SiteMapProvider {
113                         get { return ViewState.GetString ("SiteMapProvider", ""); }
114                         set {
115                                 ViewState ["SiteMapProvider"] = value;
116                                 OnDataSourceChanged (EventArgs.Empty);
117                         }
118                 }
119                 
120                 [DefaultValueAttribute ("")]
121                 [EditorAttribute ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
122                 [UrlPropertyAttribute]
123                 public virtual string StartingNodeUrl {
124                         get { return ViewState.GetString ("StartingNodeUrl", ""); }
125                         set {
126                                 ViewState ["StartingNodeUrl"] = value;
127                                 OnDataSourceChanged (EventArgs.Empty);
128                         }
129                 }
130
131                 [DefaultValue (0)]
132                 public virtual int StartingNodeOffset {
133                         get { return ViewState.GetInt ("StartingNodeOffset", 0); }
134                         set {
135                                 ViewState ["StartingNodeOffset"] = value;
136                                 OnDataSourceChanged (EventArgs.Empty);
137                         }
138                 }
139                 
140                 [DefaultValueAttribute (false)]
141                 public virtual bool StartFromCurrentNode {
142                         get { return ViewState.GetBool ("StartFromCurrentNode", false); }
143                         set {
144                                 ViewState ["StartFromCurrentNode"] = value;
145                                 OnDataSourceChanged (EventArgs.Empty);
146                         }
147                 }
148                 
149                 [DefaultValueAttribute (true)]
150                 public virtual bool ShowStartingNode {
151                         get { return ViewState.GetBool ("ShowStartingNode", true); }
152                         set {
153                                 ViewState ["ShowStartingNode"] = value;
154                                 OnDataSourceChanged (EventArgs.Empty);
155                         }
156                 }
157
158                 public virtual DataSourceView GetView (string viewName)
159                 {
160                         SiteMapNode node = GetStartNode (viewName);
161                         if (node == null)
162                                 return new SiteMapDataSourceView (this, viewName, SiteMapNodeCollection.EmptyList);
163                         else if (ShowStartingNode)
164                                 return new SiteMapDataSourceView (this, viewName, node);
165                         else
166                                 return new SiteMapDataSourceView (this, viewName, node.ChildNodes);
167                 }
168
169                 protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
170                 {
171                         SiteMapNode node = GetStartNode (viewPath);
172                         if (node == null)
173                                 return new SiteMapHierarchicalDataSourceView (SiteMapNodeCollection.EmptyList);
174                         else if (ShowStartingNode || node == null)
175                                 return new SiteMapHierarchicalDataSourceView (node);
176                         else
177                                 return new SiteMapHierarchicalDataSourceView (node.ChildNodes);
178                 }
179                 
180                 [MonoTODO ("handle StartNodeOffsets > 0")]
181                 SiteMapNode GetStartNode (string viewPath)
182                 {
183                         SiteMapNode starting_node;
184
185                         if (viewPath != null && viewPath.Length != 0) {
186                                 string url = MapUrl (StartingNodeUrl);
187                                 return Provider.FindSiteMapNode (url);
188                         } else if (StartFromCurrentNode) {
189                                 if (StartingNodeUrl.Length != 0)
190                                         throw new InvalidOperationException ("StartingNodeUrl can't be set if StartFromCurrentNode is set to true.");
191                                 starting_node = SiteMap.CurrentNode;
192                         } else if (StartingNodeUrl.Length != 0) {
193                                 string url = MapUrl (StartingNodeUrl);
194                                 SiteMapNode node = Provider.FindSiteMapNode (url);
195                                 if (node == null) throw new ArgumentException ("Can't find a site map node for the url: " + StartingNodeUrl);
196
197                                 starting_node = node;
198                         } else
199                                 starting_node = Provider.RootNode;
200
201                         if (starting_node == null)
202                                 return Provider.RootNode;
203                         
204                         int i;
205                         if (StartingNodeOffset < 0) {
206                                 for (i = StartingNodeOffset; i < 0; i ++) {
207                                         if (starting_node.ParentNode == null)
208                                                 break;
209                                         starting_node = starting_node.ParentNode;
210                                 }
211                         } else if (StartingNodeOffset > 0) {
212                                 List<SiteMapNode> pathCurrentToStartingNode = new List<SiteMapNode> ();
213                                 SiteMapNode tmpNode = Provider.CurrentNode;
214                                 while (tmpNode != null && tmpNode != starting_node) {
215                                         pathCurrentToStartingNode.Insert (0, tmpNode);
216                                         tmpNode = tmpNode.ParentNode;
217                                 }
218                                 if (tmpNode == starting_node &&
219                                         StartingNodeOffset <= pathCurrentToStartingNode.Count) {
220                                         // The requested node is in the same subtree as the starting_node
221                                         // try to advance on this path.
222                                         starting_node = pathCurrentToStartingNode [StartingNodeOffset - 1];
223                                 }
224                         }
225
226                         return starting_node;
227                 }
228                 
229                 string MapUrl (string url)
230                 {
231                         if (String.IsNullOrEmpty (url))
232                                 return String.Empty;
233                         if (UrlUtils.IsRelativeUrl (url))
234                                 return UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
235                         else
236                                 return UrlUtils.ResolveVirtualPathFromAppAbsolute (url);
237                 }
238         }
239 }
240
241 #endif
242
243