refactoring
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / SiteMapDataSource.cs
index 1c8e83879443f3b14ac32752f3d5a606bfce3202..1aa88f78ca5b98ba8fb08462fa6bbd44abd849d6 100644 (file)
 using System;
 using System.Collections;
 using System.Web.UI;
+using System.Web.Util;
 using System.ComponentModel;
 
 namespace System.Web.UI.WebControls
 {
+       [PersistChildrenAttribute (false)]
+       [DesignerAttribute ("System.Web.UI.Design.WebControls.SiteMapDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
+       [ParseChildrenAttribute (true)]
        public class SiteMapDataSource : HierarchicalDataSourceControl, IDataSource, IListSource
        {
-               [MonoTODO]
-               public DataSourceView GetView (string viewName)
+               static string[] emptyNames = new string[] { string.Empty };
+               
+               SiteMapProvider provider;
+               
+               public virtual ICollection GetViewNames ()
                {
-                       throw new NotImplementedException ();
+                       return emptyNames;
                }
                
-               [MonoTODO]
-               public ICollection GetViewNames ()
+               public virtual IList GetList ()
                {
-                       throw new NotImplementedException ();
+                       return ListSourceHelper.GetList (this);
+               }
+               
+               [BrowsableAttribute (false)]
+               [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
+               public virtual bool ContainsListCollection {
+                       get { return ListSourceHelper.ContainsListCollection (this); }
+               }
+               
+               event EventHandler IDataSource.DataSourceChanged {
+                       add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
+                       remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
+               }
+               
+               [BrowsableAttribute (false)]
+               [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
+               public SiteMapProvider Provider {
+                       get {
+                               if (provider == null) {
+                                       if (this.SiteMapProvider.Length == 0) {
+                                               provider = SiteMap.Provider;
+                                               if (provider == null)
+                                                       throw new HttpException ("There is no default provider configured for the site.");
+                                       } else {
+                                               provider = SiteMap.Providers [this.SiteMapProvider];
+                                               if (provider == null)
+                                                       throw new HttpException ("SiteMap provider '" + this.SiteMapProvider + "' not found.");
+                                       }
+                               }
+                               return provider;
+                       }
+                       set {
+                               provider = value;
+                               OnDataSourceChanged (EventArgs.Empty);
+                       }
+               }
+               
+               [DefaultValueAttribute ("")]
+               public virtual string SiteMapProvider {
+                       get { return ViewState.GetString ("SiteMapProvider", ""); }
+                       set {
+                               ViewState ["SiteMapProvider"] = value;
+                               OnDataSourceChanged (EventArgs.Empty);
+                       }
+               }
+               
+               [DefaultValueAttribute ("")]
+               [EditorAttribute ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
+               [UrlPropertyAttribute]
+               public virtual string StartingNodeUrl {
+                       get { return ViewState.GetString ("StartingNodeUrl", ""); }
+                       set {
+                               ViewState ["StartingNodeUrl"] = value;
+                               OnDataSourceChanged (EventArgs.Empty);
+                       }
+               }
+
+               [DefaultValue (0)]
+               public virtual int StartingNodeOffset {
+                       get { return ViewState.GetInt ("StartingNodeOffset", 0); }
+                       set { ViewState["StartingNodeOffset"] = value; }
                }
                
-               [MonoTODO]
-               public IList GetList ()
+               [DefaultValueAttribute (false)]
+               public virtual bool StartFromCurrentNode {
+                       get { return ViewState.GetBool ("StartFromCurrentNode", false); }
+                       set {
+                               ViewState ["StartFromCurrentNode"] = value;
+                               OnDataSourceChanged (EventArgs.Empty);
+                       }
+               }
+               
+               [DefaultValueAttribute (true)]
+               public virtual bool ShowStartingNode {
+                       get { return ViewState.GetBool ("ShowStartingNode", true); }
+                       set {
+                               ViewState ["ShowStartingNode"] = value;
+                               OnDataSourceChanged (EventArgs.Empty);
+                       }
+               }
+
+               public virtual DataSourceView GetView (string viewName)
+               {
+                       SiteMapNode node = GetStartNode (viewName);
+                       if (node == null)
+                               return new SiteMapDataSourceView (this, viewName, SiteMapNodeCollection.EmptyList);
+                       else if (ShowStartingNode)
+                               return new SiteMapDataSourceView (this, viewName, node);
+                       else
+                               return new SiteMapDataSourceView (this, viewName, node.ChildNodes);
+               }
+
+               protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
                {
-                       throw new NotImplementedException ();
+                       SiteMapNode node = GetStartNode (viewPath);
+                       if (node == null)
+                               return new SiteMapHierarchicalDataSourceView (SiteMapNodeCollection.EmptyList);
+                       else if (ShowStartingNode || node == null)
+                               return new SiteMapHierarchicalDataSourceView (node);
+                       else
+                               return new SiteMapHierarchicalDataSourceView (node.ChildNodes);
                }
                
-               [MonoTODO]
-               public bool ContainsListCollection {
-                       get { throw new NotImplementedException (); }
+               [MonoTODO ("handle StartNodeOffsets > 0")]
+               SiteMapNode GetStartNode (string viewPath)
+               {
+                       SiteMapNode starting_node;
+
+                       if (viewPath != null && viewPath.Length != 0) {
+                               string url = MapUrl (StartingNodeUrl);
+                               return Provider.FindSiteMapNode (url);
+                       }
+                       else if (StartFromCurrentNode) {
+                               if (StartingNodeUrl.Length != 0)
+                                       throw new InvalidOperationException ("StartingNodeUrl can't be set if StartFromCurrentNode is set to true.");
+                               starting_node = Provider.CurrentNode;
+                       }
+                       else if (StartingNodeUrl.Length != 0) {
+                               string url = MapUrl (StartingNodeUrl);
+                               SiteMapNode node = Provider.FindSiteMapNode (url);
+                               if (node == null) throw new ArgumentException ("Can't find a site map node for the url: " + StartingNodeUrl);
+
+                               starting_node = node;
+                       }
+                       else
+                               starting_node = Provider.RootNode;
+
+                       int i;
+                       if (StartingNodeOffset < 0) {
+                               for (i = StartingNodeOffset; i < 0; i ++) {
+                                       if (starting_node.ParentNode == null)
+                                               break;
+                                       starting_node = starting_node.ParentNode;
+                               }
+                       }
+                       else if (StartingNodeOffset > 0) {
+                       }
+
+                       return starting_node;
                }
                
-               [MonoTODO]
-               public event EventHandler DataSourceChanged {
-                       add { throw new NotImplementedException (); }
-                       remove { throw new NotImplementedException (); }
+               string MapUrl (string url)
+               {
+                       if (UrlUtils.IsRelativeUrl (url))
+                               return UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
+                       else
+                               return UrlUtils.ResolveVirtualPathFromAppAbsolute (url);
                }
        }
 }