corrected rendering: styles are applied correct
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / TreeNode.cs
index fe7716ef12ae2888976463a2192d630d83481bf4..5d6352b3e4ce9cf54a2b2903b16258ce40d145b4 100644 (file)
@@ -50,8 +50,6 @@ namespace System.Web.UI.WebControls
                string path;
                int depth = -1;
                
-               bool dataBound;
-               string dataPath;
                object dataItem;
                IHierarchyData hierarchyData;
 
@@ -95,6 +93,12 @@ namespace System.Web.UI.WebControls
                        Target = target;
                }
                
+               [MonoTODO]
+               protected TreeNode (TreeView owner, bool isRoot)
+               {
+                       throw new NotImplementedException ();
+               }
+
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                [Browsable (false)]
                public int Depth {
@@ -130,6 +134,8 @@ namespace System.Web.UI.WebControls
                                if (nodes != null)
                                        nodes.SetTree (tree);
                                ResetPathData ();
+                               if (PopulateOnDemand && !Populated && Expanded.HasValue && Expanded.Value)
+                                       Populate ();
                        }
                }
                
@@ -137,14 +143,14 @@ namespace System.Web.UI.WebControls
                [DefaultValue (false)]
                [Browsable (false)]
                public bool DataBound {
-                       get { return dataBound; }
+                       get { return ViewState ["DataBound"] == null ? false : (bool) ViewState ["DataBound"]; }
+                       private set { ViewState ["DataBound"] = value; }
                }
                
                [DefaultValue (null)]
                [Browsable (false)]
                public object DataItem {
                        get {
-                               if (!dataBound) throw new InvalidOperationException ("TreeNode is not data bound.");
                                return dataItem;
                        }
                }
@@ -153,14 +159,12 @@ namespace System.Web.UI.WebControls
                [DefaultValue ("")]
                [Browsable (false)]
                public string DataPath {
-                       get {
-                               if (!dataBound) throw new InvalidOperationException ("TreeNode is not data bound.");
-                               return dataPath;
-                       }
+                       get { return ViewState ["DataPath"] == null ? String.Empty : (String) ViewState ["DataPath"]; }
+                       private set { ViewState ["DataPath"] = value; }
                }
                
                [DefaultValue (false)]
-               public virtual bool Checked {
+               public bool Checked {
                        get {
                                object o = ViewState ["Checked"];
                                if (o != null) return (bool)o;
@@ -177,57 +181,39 @@ namespace System.Web.UI.WebControls
                [MergableProperty (false)]
                [Browsable (false)]
                [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
-               public virtual TreeNodeCollection ChildNodes {
+               public TreeNodeCollection ChildNodes {
                        get {
                                if (nodes == null) {
-                                       if (PopulateOnDemand && tree == null)
-                                               return null;
-
-                                       if (DataBound)
-                                               FillBoundChildren ();
-                                       else
-                                               nodes = new TreeNodeCollection (this);
+                                       nodes = new TreeNodeCollection (this);
                                                
                                        if (IsTrackingViewState)
                                                ((IStateManager)nodes).TrackViewState();
-                                       
-                                       if (PopulateOnDemand && !Populated) {
-                                               Populated = true;
-                                               Populate ();
-                                       }
                                }
                                return nodes;
                        }
                }
                
-               [DefaultValue (false)]
-               public virtual bool Expanded {
+               [DefaultValue (null)]
+               public bool? Expanded {
                        get {
                                object o = ViewState ["Expanded"];
-                               if (o != null) return (bool)o;
-                               return false;
+                               return (bool?)o;
                        }
                        set {
                                ViewState ["Expanded"] = value;
                                if (tree != null)
                                        tree.NotifyExpandedChanged (this);
+                               if (PopulateOnDemand && !Populated && value.HasValue && value.Value)
+                                       Populate ();
                        }
                }
 
                [Localizable (true)]
                [DefaultValue ("")]
-               public virtual string ImageToolTip {
+               public string ImageToolTip {
                        get {
                                object o = ViewState ["ImageToolTip"];
                                if (o != null) return (string)o;
-                               if (DataBound) {
-                                       TreeNodeBinding bin = GetBinding ();
-                                       if (bin != null) {
-                                               if (bin.ImageToolTipField != "")
-                                                       return (string) GetBoundPropertyValue (bin.ImageToolTipField);
-                                               return bin.ImageToolTip;
-                                       }
-                               }
                                return "";
                        }
                        set {
@@ -238,18 +224,10 @@ namespace System.Web.UI.WebControls
                [DefaultValue ("")]
                [UrlProperty]
                [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
-               public virtual string ImageUrl {
+               public string ImageUrl {
                        get {
                                object o = ViewState ["ImageUrl"];
                                if (o != null) return (string)o;
-                               if (DataBound) {
-                                       TreeNodeBinding bin = GetBinding ();
-                                       if (bin != null) {
-                                               if (bin.ImageUrlField != "")
-                                                       return (string) GetBoundPropertyValue (bin.ImageUrlField);
-                                               return bin.ImageUrl;
-                                       }
-                               }
                                return "";
                        }
                        set {
@@ -260,18 +238,10 @@ namespace System.Web.UI.WebControls
                [DefaultValue ("")]
                [UrlProperty]
                [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
-               public virtual string NavigateUrl {
+               public string NavigateUrl {
                        get {
                                object o = ViewState ["NavigateUrl"];
                                if (o != null) return (string)o;
-                               if (DataBound) {
-                                       TreeNodeBinding bin = GetBinding ();
-                                       if (bin != null) {
-                                               if (bin.NavigateUrlField != "")
-                                                       return (string) GetBoundPropertyValue (bin.NavigateUrlField);
-                                               return bin.NavigateUrl;
-                                       }
-                               }
                                return "";
                        }
                        set {
@@ -284,11 +254,6 @@ namespace System.Web.UI.WebControls
                        get {
                                object o = ViewState ["PopulateOnDemand"];
                                if (o != null) return (bool)o;
-                               if (DataBound) {
-                                       TreeNodeBinding bin = GetBinding ();
-                                       if (bin != null)
-                                               return bin.PopulateOnDemand;
-                               }
                                return false;
                        }
                        set {
@@ -301,11 +266,6 @@ namespace System.Web.UI.WebControls
                        get {
                                object o = ViewState ["SelectAction"];
                                if (o != null) return (TreeNodeSelectAction)o;
-                               if (DataBound) {
-                                       TreeNodeBinding bin = GetBinding ();
-                                       if (bin != null)
-                                               return bin.SelectAction;
-                               }
                                return TreeNodeSelectAction.Select;
                        }
                        set {
@@ -313,40 +273,22 @@ namespace System.Web.UI.WebControls
                        }
                }
 
-               [DefaultValue (false)]
-               public bool ShowCheckBox {
+               [DefaultValue (null)]
+               public bool? ShowCheckBox {
                        get {
                                object o = ViewState ["ShowCheckBox"];
-                               if (o != null) return (bool)o;
-                               if (DataBound) {
-                                       TreeNodeBinding bin = GetBinding ();
-                                       if (bin != null)
-                                               return bin.ShowCheckBox;
-                               }
-                               return false;
+                               return (bool?)o;
                        }
                        set {
                                ViewState ["ShowCheckBox"] = value;
                        }
                }
                
-               internal bool IsShowCheckBoxSet {
-                       get { return ViewState ["ShowCheckBox"] != null; }
-               }
-
                [DefaultValue ("")]
-               public virtual string Target {
+               public string Target {
                        get {
                                object o = ViewState ["Target"];
                                if(o != null) return (string)o;
-                               if (DataBound) {
-                                       TreeNodeBinding bin = GetBinding ();
-                                       if (bin != null) {
-                                               if (bin.TargetField != "")
-                                                       return (string) GetBoundPropertyValue (bin.TargetField);
-                                               return bin.Target;
-                                       }
-                               }
                                return "";
                        }
                        set {
@@ -357,27 +299,10 @@ namespace System.Web.UI.WebControls
                [Localizable (true)]
                [DefaultValue ("")]
                [WebSysDescription ("The display text of the tree node.")]
-               public virtual string Text {
+               public string Text {
                        get {
                                object o = ViewState ["Text"];
                                if (o != null) return (string)o;
-                               if (DataBound) {
-                                       TreeNodeBinding bin = GetBinding ();
-                                       if (bin != null) {
-                                               string text;
-                                               if (bin.TextField != "")
-                                                       text = (string) GetBoundPropertyValue (bin.TextField);
-                                               else if (bin.Text != "")
-                                                       text = bin.Text;
-                                               else
-                                                       text = GetDefaultBoundText ();
-                                                       
-                                               if (bin.FormatString.Length != 0)
-                                                       text = string.Format (bin.FormatString, text);
-                                               return text;
-                                       }
-                                       return GetDefaultBoundText ();
-                               }
                                return "";
                        }
                        set {
@@ -387,18 +312,10 @@ namespace System.Web.UI.WebControls
 
                [Localizable (true)]
                [DefaultValue ("")]
-               public virtual string ToolTip {
+               public string ToolTip {
                        get {
                                object o = ViewState ["ToolTip"];
                                if(o != null) return (string)o;
-                               if (DataBound) {
-                                       TreeNodeBinding bin = GetBinding ();
-                                       if (bin != null) {
-                                               if (bin.ToolTipField != "")
-                                                       return (string) GetBoundPropertyValue (bin.ToolTipField);
-                                               return bin.ToolTip;
-                                       }
-                               }
                                return "";
                        }
                        set {
@@ -408,20 +325,10 @@ namespace System.Web.UI.WebControls
 
                [Localizable (true)]
                [DefaultValue ("")]
-               public virtual string Value {
+               public string Value {
                        get {
                                object o = ViewState ["Value"];
                                if(o != null) return (string)o;
-                               if (DataBound) {
-                                       TreeNodeBinding bin = GetBinding ();
-                                       if (bin != null) {
-                                               if (bin.ValueField != "")
-                                                       return (string) GetBoundPropertyValue (bin.ValueField);
-                                               if (bin.Value != "")
-                                                       return bin.Value;
-                                       }
-                                       return GetDefaultBoundText ();
-                               }
                                return "";
                        }
                        set {
@@ -430,7 +337,7 @@ namespace System.Web.UI.WebControls
                }
                
                [DefaultValue (false)]
-               public virtual bool Selected {
+               public bool Selected {
                        get {
                                return SelectedFlag;
                        }
@@ -520,8 +427,12 @@ namespace System.Web.UI.WebControls
                        get { return nodes != null; }
                }
                
-               protected virtual void Populate ()
+               internal void Populate ()
                {
+                       if (tree == null)
+                               return;
+
+                       Populated = true;
                        tree.NotifyPopulateRequired (this);
                }
                
@@ -566,10 +477,20 @@ namespace System.Web.UI.WebControls
                
                public void ToggleExpandState ()
                {
-                       Expanded = !Expanded;
+#if TARGET_JVM //No support for Nullable<bool>.GetValueOrDefault() yet
+                       bool? value = Expanded;
+                       Expanded = value.HasValue ? !value.Value : true;
+#else
+                       Expanded = !Expanded.GetValueOrDefault(false);
+#endif
+               }
+
+               void IStateManager.LoadViewState (object savedState)
+               {
+                       LoadViewState (savedState);
                }
 
-               public void LoadViewState (object savedState)
+               protected virtual void LoadViewState (object savedState)
                {
                        if (savedState == null)
                                return;
@@ -584,7 +505,12 @@ namespace System.Web.UI.WebControls
                                ((IStateManager)ChildNodes).LoadViewState (states [1]);
                }
                
-               public object SaveViewState ()
+               object IStateManager.SaveViewState ()
+               {
+                       return SaveViewState ();
+               }
+
+               protected virtual object SaveViewState ()
                {
                        object[] states = new object[2];
                        states[0] = ViewState.SaveViewState();
@@ -596,8 +522,13 @@ namespace System.Web.UI.WebControls
                        }
                        return null;
                }
-               
-               public void TrackViewState ()
+
+               void IStateManager.TrackViewState ()
+               {
+                       TrackViewState ();
+               }
+
+               protected void TrackViewState ()
                {
                        if (marked) return;
                        marked = true;
@@ -607,7 +538,11 @@ namespace System.Web.UI.WebControls
                                ((IStateManager)nodes).TrackViewState ();
                }
                
-               public bool IsTrackingViewState
+               bool IStateManager.IsTrackingViewState {
+                       get { return IsTrackingViewState; }
+               }
+
+               protected bool IsTrackingViewState
                {
                        get { return marked; }
                }
@@ -615,13 +550,15 @@ namespace System.Web.UI.WebControls
                internal void SetDirty ()
                {
                        ViewState.SetDirty (true);
+                       if (nodes != null)
+                               nodes.SetDirty ();
                }
                
-               public object Clone ()
+               public virtual object Clone ()
                {
                        TreeNode nod = tree != null ? tree.CreateNode () : new TreeNode ();
                        foreach (DictionaryEntry e in ViewState)
-                               nod.ViewState [(string)e.Key] = e.Value;
+                               nod.ViewState [(string)e.Key] = ((StateItem)e.Value).Value;
                                
                        foreach (TreeNode c in ChildNodes)
                                nod.ChildNodes.Add ((TreeNode)c.Clone ());
@@ -632,9 +569,118 @@ namespace System.Web.UI.WebControls
                internal void Bind (IHierarchyData hierarchyData)
                {
                        this.hierarchyData = hierarchyData;
-                       dataBound = true;
-                       dataPath = hierarchyData.Path;
+                       DataBound = true;
+                       DataPath = hierarchyData.Path;
                        dataItem = hierarchyData.Item;
+                       
+                       TreeNodeBinding bin = GetBinding ();
+                       if (bin != null) {
+                       
+                               // Bind ImageToolTip property
+
+                               if (bin.ImageToolTipField.Length > 0) {
+                                       ImageToolTip = Convert.ToString (GetBoundPropertyValue (bin.ImageToolTipField));
+                                       if (ImageToolTip.Length == 0)
+                                               ImageToolTip = bin.ImageToolTip;
+                               }
+                               else if (bin.ImageToolTip.Length > 0)
+                                       ImageToolTip = bin.ImageToolTip;
+                                       
+                               // Bind ImageUrl property
+
+                               if (bin.ImageUrlField.Length > 0) {
+                                       ImageUrl = Convert.ToString (GetBoundPropertyValue (bin.ImageUrlField));
+                                       if (ImageUrl.Length == 0)
+                                               ImageUrl = bin.ImageUrl;
+                               }
+                               else if (bin.ImageUrl.Length > 0)
+                                       ImageUrl = bin.ImageUrl;
+                                       
+                               // Bind NavigateUrl property
+
+                               if (bin.NavigateUrlField.Length > 0) {
+                                       NavigateUrl = Convert.ToString (GetBoundPropertyValue (bin.NavigateUrlField));
+                                       if (NavigateUrl.Length == 0)
+                                               NavigateUrl = bin.NavigateUrl;
+                               }
+                               else if (bin.NavigateUrl.Length > 0)
+                                       NavigateUrl = bin.NavigateUrl;
+                                       
+                               // Bind PopulateOnDemand property
+                               
+                               if (bin.HasPropertyValue ("PopulateOnDemand"))
+                                       PopulateOnDemand = bin.PopulateOnDemand;
+                               
+                               // Bind SelectAction property
+                                       
+                               if (bin.HasPropertyValue ("SelectAction"))
+                                       SelectAction = bin.SelectAction;
+                               
+                               // Bind ShowCheckBox property
+                                       
+                               if (bin.HasPropertyValue ("ShowCheckBox"))
+                                       ShowCheckBox = bin.ShowCheckBox;
+                                       
+                               // Bind Target property
+
+                               if (bin.TargetField.Length > 0) {
+                                       Target = Convert.ToString (GetBoundPropertyValue (bin.TargetField));
+                                       if (Target.Length == 0)
+                                               Target = bin.Target;
+                               }
+                               else if (bin.Target.Length > 0)
+                                       Target = bin.Target;
+                                       
+                               // Bind Text property
+                                       
+                               if (bin.TextField.Length > 0) {
+                                       Text = Convert.ToString (GetBoundPropertyValue (bin.TextField));
+                                       if (bin.FormatString.Length > 0)
+                                               Text = string.Format (bin.FormatString, Text);
+                                       if (Text.Length == 0)
+                                               Text = bin.Text;
+                                       if (Text.Length == 0)
+                                               Text = bin.Value;
+                                       if (Text.Length == 0 && bin.ValueField.Length > 0)
+                                               Text = Convert.ToString (GetBoundPropertyValue (bin.ValueField));
+                               }
+                               else if (bin.Text.Length > 0)
+                                       Text = bin.Text;
+                               else if (bin.Value.Length > 0)
+                                       Text = bin.Value;
+                               else
+                                       Text = GetDefaultBoundText ();
+                                       
+                               // Bind ToolTip property
+
+                               if (bin.ToolTipField.Length > 0) {
+                                       ToolTip = Convert.ToString (GetBoundPropertyValue (bin.ToolTipField));
+                                       if (ToolTip.Length == 0)
+                                               ToolTip = bin.ToolTip;
+                               }
+                               else if (bin.ToolTip.Length > 0)
+                                       ToolTip = bin.ToolTip;
+                                       
+                               // Bind Value property
+
+                               if (bin.ValueField.Length > 0) {
+                                       Value = Convert.ToString (GetBoundPropertyValue (bin.ValueField));
+                                       if (Value.Length == 0)
+                                               Value = bin.Value;
+                                       if (Value.Length == 0)
+                                               Value = bin.Text;
+                                       if(Value.Length == 0 && bin.TextField.Length > 0)
+                                               Value = Convert.ToString (GetBoundPropertyValue (bin.TextField));
+                               }
+                               else if (bin.Value.Length > 0)
+                                       Value = bin.Value;
+                               else if (bin.Text.Length > 0)
+                                       Value = bin.Text;
+                               else
+                                       Value = GetDefaultBoundText ();
+                       } else {
+                               Text = Value = GetDefaultBoundText ();
+                       }
                }
                
                internal void SetDataItem (object item)
@@ -644,12 +690,12 @@ namespace System.Web.UI.WebControls
                
                internal void SetDataPath (string path)
                {
-                       dataPath = path;
+                       DataPath = path;
                }
                
                internal void SetDataBound (bool bound)
                {
-                       dataBound = bound;
+                       DataBound = bound;
                }
                
                string GetDefaultBoundText ()
@@ -667,15 +713,15 @@ namespace System.Web.UI.WebControls
                }
                                
                internal bool IsParentNode {
-                       get { return ChildNodes.Count > 0 && Parent != null; }
+                       get { return !IsRootNode && (ChildNodes.Count > 0 || PopulateOnDemand); }
                }
                
                internal bool IsLeafNode {
-                       get { return ChildNodes.Count == 0; }
+                       get { return !IsRootNode && ChildNodes.Count == 0 && !PopulateOnDemand; }
                }
                
                internal bool IsRootNode {
-                       get { return ChildNodes.Count > 0 && Parent == null; }
+                       get { return Depth == 0; }
                }
                
                TreeNodeBinding GetBinding ()
@@ -706,21 +752,6 @@ namespace System.Web.UI.WebControls
                                return prop.GetValue (dataItem);
                }
 
-               void FillBoundChildren ()
-               {
-                       nodes = new TreeNodeCollection (this);
-                       if (hierarchyData == null || !hierarchyData.HasChildren) return;
-                       if (tree.MaxDataBindDepth != -1 && Depth >= tree.MaxDataBindDepth) return;
-
-                       IHierarchicalEnumerable e = hierarchyData.GetChildren ();
-                       foreach (object obj in e) {
-                               IHierarchyData hdata = e.GetHierarchyData (obj);
-                               TreeNode node = tree != null ? tree.CreateNode () : new TreeNode ();
-                               node.Bind (hdata);
-                               nodes.Add (node);
-                       }
-               }
-               
                internal void BeginRenderText (HtmlTextWriter writer)
                {
                        RenderPreText (writer);