Merge pull request #2916 from ludovic-henry/fix-40306
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / TreeNode.cs
index 60df03f293d09e8f9ed1a74cc7e75f95f1e35c28..73c8d5fc401e4771f6d11c21fd86b140f15d66a0 100644 (file)
@@ -28,7 +28,6 @@
 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
 //
 
-#if NET_2_0
 
 using System;
 using System.Collections;
@@ -56,6 +55,8 @@ namespace System.Web.UI.WebControls
                bool gotBinding;
                TreeNodeBinding binding;
                PropertyDescriptorCollection boundProperties;
+               bool populating;
+               bool hadChildrenBeforePopulating;
                
                internal TreeNode (TreeView tree)
                {
@@ -93,7 +94,7 @@ namespace System.Web.UI.WebControls
                        Target = target;
                }
                
-               [MonoTODO]
+               [MonoTODO ("Not implemented")]
                protected TreeNode (TreeView owner, bool isRoot)
                {
                        throw new NotImplementedException ();
@@ -119,6 +120,10 @@ namespace System.Web.UI.WebControls
                        path = null;
                        depth = -1;
                        gotBinding = false;
+                       if (nodes != null) {
+                               foreach (TreeNode node in nodes)
+                                       node.ResetPathData ();
+                       }
                }
                
                internal TreeView Tree {
@@ -127,8 +132,6 @@ namespace System.Web.UI.WebControls
                                if (SelectedFlag) {
                                        if (value != null)
                                                value.SetSelectedNode (this, false);
-                                       else if (tree != null)
-                                               tree.SetSelectedNode (null, false);
                                }
                                tree = value;
                                if (nodes != null)
@@ -150,9 +153,7 @@ namespace System.Web.UI.WebControls
                [DefaultValue (null)]
                [Browsable (false)]
                public object DataItem {
-                       get {
-                               return dataItem;
-                       }
+                       get { return dataItem; }
                }
                
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
@@ -200,6 +201,9 @@ namespace System.Web.UI.WebControls
                                return (bool?)o;
                        }
                        set {
+                               bool? current = (bool?) ViewState ["Expanded"];
+                               if (current == value)
+                                       return;
                                ViewState ["Expanded"] = value;
                                if (tree != null)
                                        tree.NotifyExpandedChanged (this);
@@ -213,12 +217,11 @@ namespace System.Web.UI.WebControls
                public string ImageToolTip {
                        get {
                                object o = ViewState ["ImageToolTip"];
-                               if (o != null) return (string)o;
-                               return "";
-                       }
-                       set {
-                               ViewState ["ImageToolTip"] = value;
+                               if (o != null)
+                                       return (string)o;
+                               return String.Empty;
                        }
+                       set { ViewState ["ImageToolTip"] = value; }
                }
                
                [DefaultValue ("")]
@@ -227,12 +230,11 @@ namespace System.Web.UI.WebControls
                public string ImageUrl {
                        get {
                                object o = ViewState ["ImageUrl"];
-                               if (o != null) return (string)o;
-                               return "";
-                       }
-                       set {
-                               ViewState ["ImageUrl"] = value;
+                               if (o != null)
+                                       return (string)o;
+                               return String.Empty;
                        }
+                       set { ViewState ["ImageUrl"] = value; }
                }
 
                [DefaultValue ("")]
@@ -241,23 +243,37 @@ namespace System.Web.UI.WebControls
                public string NavigateUrl {
                        get {
                                object o = ViewState ["NavigateUrl"];
-                               if (o != null) return (string)o;
-                               return "";
+                               if (o != null)
+                                       return (string)o;
+                               return String.Empty;
                        }
+                       set { ViewState ["NavigateUrl"] = value; }
+               }
+
+               internal bool HadChildrenBeforePopulating {
+                       get { return hadChildrenBeforePopulating; }
                        set {
-                               ViewState ["NavigateUrl"] = value;
+                               if (populating)
+                                       return;
+
+                               hadChildrenBeforePopulating = value;
                        }
                }
-
+               
                [DefaultValue (false)]
                public bool PopulateOnDemand {
                        get {
                                object o = ViewState ["PopulateOnDemand"];
-                               if (o != null) return (bool)o;
+                               if (o != null)
+                                       return (bool)o;
                                return false;
                        }
                        set {
                                ViewState ["PopulateOnDemand"] = value;
+                               if (value && nodes != null && nodes.Count > 0)
+                                       HadChildrenBeforePopulating = true;
+                               else
+                                       HadChildrenBeforePopulating = false;
                        }
                }
 
@@ -265,12 +281,11 @@ namespace System.Web.UI.WebControls
                public TreeNodeSelectAction SelectAction {
                        get {
                                object o = ViewState ["SelectAction"];
-                               if (o != null) return (TreeNodeSelectAction)o;
+                               if (o != null)
+                                       return (TreeNodeSelectAction)o;
                                return TreeNodeSelectAction.Select;
                        }
-                       set {
-                               ViewState ["SelectAction"] = value;
-                       }
+                       set { ViewState ["SelectAction"] = value; }
                }
 
                [DefaultValue (null)]
@@ -279,8 +294,18 @@ namespace System.Web.UI.WebControls
                                object o = ViewState ["ShowCheckBox"];
                                return (bool?)o;
                        }
-                       set {
-                               ViewState ["ShowCheckBox"] = value;
+                       set { ViewState ["ShowCheckBox"] = value; }
+               }
+
+               internal bool ShowCheckBoxInternal {
+                       get {
+                               if (ShowCheckBox.HasValue)
+                                       return ShowCheckBox.Value;
+                               else
+                                       return (Tree.ShowCheckBoxes == TreeNodeTypes.All) ||
+                                                ((Tree.ShowCheckBoxes & TreeNodeTypes.Leaf) > 0 && IsLeafNode) ||
+                                                ((Tree.ShowCheckBoxes & TreeNodeTypes.Parent) > 0 && IsParentNode && Parent != null) ||
+                                                ((Tree.ShowCheckBoxes & TreeNodeTypes.Root) > 0 && Parent == null && ChildNodes.Count > 0);
                        }
                }
                
@@ -288,12 +313,11 @@ namespace System.Web.UI.WebControls
                public string Target {
                        get {
                                object o = ViewState ["Target"];
-                               if(o != null) return (string)o;
-                               return "";
-                       }
-                       set {
-                               ViewState ["Target"] = value;
+                               if(o != null)
+                                       return (string)o;
+                               return String.Empty;
                        }
+                       set { ViewState ["Target"] = value; }
                }
 
                [Localizable (true)]
@@ -302,12 +326,13 @@ namespace System.Web.UI.WebControls
                public string Text {
                        get {
                                object o = ViewState ["Text"];
-                               if (o != null) return (string)o;
-                               return "";
-                       }
-                       set {
-                               ViewState ["Text"] = value;
+                               if (o == null)
+                                       o = ViewState ["Value"];
+                               if (o != null)
+                                       return (string)o;
+                               return String.Empty;
                        }
+                       set { ViewState ["Text"] = value; }
                }
 
                [Localizable (true)]
@@ -315,12 +340,11 @@ namespace System.Web.UI.WebControls
                public string ToolTip {
                        get {
                                object o = ViewState ["ToolTip"];
-                               if(o != null) return (string)o;
-                               return "";
-                       }
-                       set {
-                               ViewState ["ToolTip"] = value;
+                               if(o != null)
+                                       return (string)o;
+                               return String.Empty;
                        }
+                       set { ViewState ["ToolTip"] = value; }
                }
 
                [Localizable (true)]
@@ -328,40 +352,38 @@ namespace System.Web.UI.WebControls
                public string Value {
                        get {
                                object o = ViewState ["Value"];
-                               if(o != null) return (string)o;
-                               return "";
-                       }
-                       set {
-                               ViewState ["Value"] = value;
+                               if (o == null)
+                                       o = ViewState ["Text"];
+                               if(o != null)
+                                       return (string)o;
+                               return String.Empty;
                        }
+                       set { ViewState ["Value"] = value; }
                }
                
                [DefaultValue (false)]
                public bool Selected {
-                       get {
-                               return SelectedFlag;
-                       }
+                       get { return SelectedFlag; }
                        set {
+                               SelectedFlag = value;
+                               
                                if (tree != null) {
                                        if (!value && tree.SelectedNode == this)
                                                tree.SetSelectedNode (null, false);
                                        else if (value)
                                                tree.SetSelectedNode (this, false);
                                }
-                               else
-                                       SelectedFlag = value;
                        }
                }
                
                internal virtual bool SelectedFlag {
                        get {
                                object o = ViewState ["Selected"];
-                               if(o != null) return (bool)o;
+                               if(o != null)
+                                       return (bool)o;
                                return false;
                        }
-                       set {
-                               ViewState ["Selected"] = value;
-                       }
+                       set { ViewState ["Selected"] = value; }
                }
                
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
@@ -392,14 +414,16 @@ namespace System.Web.UI.WebControls
                        set { index = value; ResetPathData (); }
                }
                
-               internal void SetParent (TreeNode node) {
+               internal void SetParent (TreeNode node)
+               {
                        parent = node;
                        ResetPathData ();
                }
                
                internal string Path {
                        get {
-                               if (path != null) return path;
+                               if (path != null)
+                                       return path;
                                StringBuilder sb = new StringBuilder (index.ToString());
                                TreeNode node = parent;
                                while (node != null) {
@@ -415,12 +439,11 @@ namespace System.Web.UI.WebControls
                internal bool Populated {
                        get {
                                object o = ViewState ["Populated"];
-                               if (o != null) return (bool) o;
+                               if (o != null)
+                                       return (bool) o;
                                return false;
                        }
-                       set {
-                               ViewState ["Populated"] = value;
-                       }
+                       set { ViewState ["Populated"] = value; }
                }
 
                internal bool HasChildData {
@@ -432,8 +455,10 @@ namespace System.Web.UI.WebControls
                        if (tree == null)
                                return;
 
-                       Populated = true;
+                       populating = true;
                        tree.NotifyPopulateRequired (this);
+                       populating = false;
+                       Populated = true;
                }
                
                public void Collapse ()
@@ -464,7 +489,8 @@ namespace System.Web.UI.WebControls
                void SetExpandedRec (bool expanded, int depth)
                {
                        Expanded = expanded;
-                       if (depth == 0) return;
+                       if (depth == 0)
+                               return;
                        
                        foreach (TreeNode nod in ChildNodes)
                                nod.SetExpandedRec (expanded, depth - 1);
@@ -477,12 +503,7 @@ namespace System.Web.UI.WebControls
                
                public void ToggleExpandState ()
                {
-#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)
@@ -542,8 +563,7 @@ namespace System.Web.UI.WebControls
                        get { return IsTrackingViewState; }
                }
 
-               protected bool IsTrackingViewState
-               {
+               protected bool IsTrackingViewState {
                        get { return marked; }
                }
                
@@ -565,6 +585,11 @@ namespace System.Web.UI.WebControls
                                
                        return nod;
                }
+
+               object ICloneable.Clone ()
+               {
+                       return Clone ();
+               }
                
                internal void Bind (IHierarchyData hierarchyData)
                {
@@ -582,8 +607,7 @@ namespace System.Web.UI.WebControls
                                        ImageToolTip = Convert.ToString (GetBoundPropertyValue (bin.ImageToolTipField));
                                        if (ImageToolTip.Length == 0)
                                                ImageToolTip = bin.ImageToolTip;
-                               }
-                               else if (bin.ImageToolTip.Length > 0)
+                               } else if (bin.ImageToolTip.Length > 0)
                                        ImageToolTip = bin.ImageToolTip;
                                        
                                // Bind ImageUrl property
@@ -592,8 +616,7 @@ namespace System.Web.UI.WebControls
                                        ImageUrl = Convert.ToString (GetBoundPropertyValue (bin.ImageUrlField));
                                        if (ImageUrl.Length == 0)
                                                ImageUrl = bin.ImageUrl;
-                               }
-                               else if (bin.ImageUrl.Length > 0)
+                               } else if (bin.ImageUrl.Length > 0)
                                        ImageUrl = bin.ImageUrl;
                                        
                                // Bind NavigateUrl property
@@ -602,8 +625,7 @@ namespace System.Web.UI.WebControls
                                        NavigateUrl = Convert.ToString (GetBoundPropertyValue (bin.NavigateUrlField));
                                        if (NavigateUrl.Length == 0)
                                                NavigateUrl = bin.NavigateUrl;
-                               }
-                               else if (bin.NavigateUrl.Length > 0)
+                               } else if (bin.NavigateUrl.Length > 0)
                                        NavigateUrl = bin.NavigateUrl;
                                        
                                // Bind PopulateOnDemand property
@@ -627,29 +649,24 @@ namespace System.Web.UI.WebControls
                                        Target = Convert.ToString (GetBoundPropertyValue (bin.TargetField));
                                        if (Target.Length == 0)
                                                Target = bin.Target;
-                               }
-                               else if (bin.Target.Length > 0)
+                               } else if (bin.Target.Length > 0)
                                        Target = bin.Target;
                                        
                                // Bind Text property
-                                       
+                               string text = null;
                                if (bin.TextField.Length > 0) {
-                                       Text = Convert.ToString (GetBoundPropertyValue (bin.TextField));
+                                       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));
+                                               text = string.Format (bin.FormatString, text);
                                }
-                               else if (bin.Text.Length > 0)
-                                       Text = bin.Text;
-                               else if (bin.Value.Length > 0)
-                                       Text = bin.Value;
-                               else
-                                       Text = GetDefaultBoundText ();
+                               if (String.IsNullOrEmpty (text)) {
+                                       if (bin.Text.Length > 0)
+                                               text = bin.Text;
+                                       else if (bin.Value.Length > 0)
+                                               text = bin.Value;
+                               }
+                               if (!String.IsNullOrEmpty (text))
+                                       Text = text;
                                        
                                // Bind ToolTip property
 
@@ -657,30 +674,33 @@ namespace System.Web.UI.WebControls
                                        ToolTip = Convert.ToString (GetBoundPropertyValue (bin.ToolTipField));
                                        if (ToolTip.Length == 0)
                                                ToolTip = bin.ToolTip;
-                               }
-                               else if (bin.ToolTip.Length > 0)
+                               } else if (bin.ToolTip.Length > 0)
                                        ToolTip = bin.ToolTip;
                                        
                                // Bind Value property
-
+                               string value = null;
                                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));
+                                       value = Convert.ToString (GetBoundPropertyValue (bin.ValueField));
                                }
-                               else if (bin.Value.Length > 0)
-                                       Value = bin.Value;
-                               else if (bin.Text.Length > 0)
-                                       Value = bin.Text;
-                               else
-                                       Value = GetDefaultBoundText ();
+                               if (String.IsNullOrEmpty (value)) {
+                                       if (bin.Value.Length > 0)
+                                               value = bin.Value;
+                                       else if (bin.Text.Length > 0)
+                                               value = bin.Text;
+                               }
+                               if (!String.IsNullOrEmpty (value))
+                                       Value = value;
                        } else {
                                Text = Value = GetDefaultBoundText ();
                        }
+
+                       INavigateUIData navigateUIData = hierarchyData as INavigateUIData;
+                       if (navigateUIData != null) {
+                               SelectAction = TreeNodeSelectAction.None;
+                               Text = navigateUIData.ToString ();
+                               NavigateUrl = navigateUIData.NavigateUrl;
+                               ToolTip = navigateUIData.Description;
+                       }
                }
                
                internal void SetDataItem (object item)
@@ -700,16 +720,22 @@ namespace System.Web.UI.WebControls
                
                string GetDefaultBoundText ()
                {
-                       if (hierarchyData != null) return hierarchyData.ToString ();
-                       else if (dataItem != null) return dataItem.ToString ();
-                       else return string.Empty;
+                       if (hierarchyData != null)
+                               return hierarchyData.ToString ();
+                       else if (dataItem != null)
+                               return dataItem.ToString ();
+                       else
+                               return string.Empty;
                }
                
                string GetDataItemType ()
                {
-                       if (hierarchyData != null) return hierarchyData.Type;
-                       else if (dataItem != null) return dataItem.GetType().ToString ();
-                       else return string.Empty;
+                       if (hierarchyData != null)
+                               return hierarchyData.Type;
+                       else if (dataItem != null)
+                               return dataItem.GetType().ToString ();
+                       else
+                               return string.Empty;
                }
                                
                internal bool IsParentNode {
@@ -726,8 +752,10 @@ namespace System.Web.UI.WebControls
                
                TreeNodeBinding GetBinding ()
                {
-                       if (tree == null) return null;
-                       if (gotBinding) return binding;
+                       if (tree == null)
+                               return null;
+                       if (gotBinding)
+                               return binding;
                        binding = tree.FindBindingForNode (GetDataItemType (), Depth);
                        gotBinding = true;
                        return binding;
@@ -771,5 +799,3 @@ namespace System.Web.UI.WebControls
                }
        }
 }
-
-#endif