fixed: when removed the names from the bag also removed FontStyles.Names flag from...
[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 virtual IList GetList ()
56                 {
57                         return ListSourceHelper.GetList (this);
58                 }
59                 
60                 [BrowsableAttribute (false)]
61                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
62                 public virtual 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)]
72                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
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 ("")]
95                 public virtual string SiteMapProvider {
96                         get { return ViewState.GetString ("SiteMapProvider", ""); }
97                         set {
98                                 ViewState ["SiteMapProvider"] = value;
99                                 OnDataSourceChanged (EventArgs.Empty);
100                         }
101                 }
102                 
103                 [DefaultValueAttribute ("")]
104                 [EditorAttribute ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
105                 [UrlPropertyAttribute]
106                 public virtual string StartingNodeUrl {
107                         get { return ViewState.GetString ("StartingNodeUrl", ""); }
108                         set {
109                                 ViewState ["StartingNodeUrl"] = value;
110                                 OnDataSourceChanged (EventArgs.Empty);
111                         }
112                 }
113
114                 [DefaultValue (0)]
115                 public virtual int StartingNodeOffset {
116                         get { return ViewState.GetInt ("StartingNodeOffset", 0); }
117                         set { ViewState["StartingNodeOffset"] = value; }
118                 }
119                 
120                 [DefaultValueAttribute (false)]
121                 public virtual bool StartFromCurrentNode {
122                         get { return ViewState.GetBool ("StartFromCurrentNode", false); }
123                         set {
124                                 ViewState ["StartFromCurrentNode"] = value;
125                                 OnDataSourceChanged (EventArgs.Empty);
126                         }
127                 }
128                 
129                 [DefaultValueAttribute (true)]
130                 public virtual bool ShowStartingNode {
131                         get { return ViewState.GetBool ("ShowStartingNode", true); }
132                         set {
133                                 ViewState ["ShowStartingNode"] = value;
134                                 OnDataSourceChanged (EventArgs.Empty);
135                         }
136                 }
137
138                 public virtual DataSourceView GetView (string viewName)
139                 {
140                         SiteMapNode node = GetStartNode (viewName);
141                         if (node == null)
142                                 return new SiteMapDataSourceView (this, viewName, SiteMapNodeCollection.EmptyList);
143                         else if (ShowStartingNode)
144                                 return new SiteMapDataSourceView (this, viewName, node);
145                         else
146                                 return new SiteMapDataSourceView (this, viewName, node.ChildNodes);
147                 }
148
149                 protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
150                 {
151                         SiteMapNode node = GetStartNode (viewPath);
152                         if (node == null)
153                                 return new SiteMapHierarchicalDataSourceView (SiteMapNodeCollection.EmptyList);
154                         else if (ShowStartingNode || node == null)
155                                 return new SiteMapHierarchicalDataSourceView (node);
156                         else
157                                 return new SiteMapHierarchicalDataSourceView (node.ChildNodes);
158                 }
159                 
160                 [MonoTODO ("handle StartNodeOffsets > 0")]
161                 SiteMapNode GetStartNode (string viewPath)
162                 {
163                         SiteMapNode starting_node;
164
165                         if (viewPath != null && viewPath.Length != 0) {
166                                 string url = MapUrl (StartingNodeUrl);
167                                 return Provider.FindSiteMapNode (url);
168                         }
169                         else if (StartFromCurrentNode) {
170                                 if (StartingNodeUrl.Length != 0)
171                                         throw new InvalidOperationException ("StartingNodeUrl can't be set if StartFromCurrentNode is set to true.");
172                                 starting_node = Provider.CurrentNode;
173                         }
174                         else if (StartingNodeUrl.Length != 0) {
175                                 string url = MapUrl (StartingNodeUrl);
176                                 SiteMapNode node = Provider.FindSiteMapNode (url);
177                                 if (node == null) throw new ArgumentException ("Can't find a site map node for the url: " + StartingNodeUrl);
178
179                                 starting_node = node;
180                         }
181                         else
182                                 starting_node = Provider.RootNode;
183
184                         int i;
185                         if (StartingNodeOffset < 0) {
186                                 for (i = StartingNodeOffset; i < 0; i ++) {
187                                         if (starting_node.ParentNode == null)
188                                                 break;
189                                         starting_node = starting_node.ParentNode;
190                                 }
191                         }
192                         else if (StartingNodeOffset > 0) {
193                         }
194
195                         return starting_node;
196                 }
197                 
198                 string MapUrl (string url)
199                 {
200                         if (UrlUtils.IsRelativeUrl (url))
201                                 return UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
202                         else
203                                 return UrlUtils.ResolveVirtualPathFromAppAbsolute (url);
204                 }
205         }
206 }
207
208 #endif
209