| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803 |
- //
- // System.Web.UI.WebControls.TreeNode.cs
- //
- // Authors:
- // Lluis Sanchez Gual ([email protected])
- //
- // (C) 2004 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
- //
- #if NET_2_0
- using System;
- using System.Collections;
- using System.Text;
- using System.ComponentModel;
- using System.Web.UI;
- namespace System.Web.UI.WebControls
- {
- [ParseChildrenAttribute (true, "ChildNodes")]
- public class TreeNode: IStateManager, ICloneable
- {
- StateBag ViewState = new StateBag ();
- TreeNodeCollection nodes;
- bool marked;
- TreeView tree;
- TreeNode parent;
- int index;
- string path;
- int depth = -1;
-
- object dataItem;
- IHierarchyData hierarchyData;
- bool gotBinding;
- TreeNodeBinding binding;
- PropertyDescriptorCollection boundProperties;
-
- internal TreeNode (TreeView tree)
- {
- Tree = tree;
- }
-
- public TreeNode ()
- {
- }
-
- public TreeNode (string text)
- {
- Text = text;
- }
-
- public TreeNode (string text, string value)
- {
- Text = text;
- Value = value;
- }
-
- public TreeNode (string text, string value, string imageUrl)
- {
- Text = text;
- Value = value;
- ImageUrl = imageUrl;
- }
-
- public TreeNode (string text, string value, string imageUrl, string navigateUrl, string target)
- {
- Text = text;
- Value = value;
- ImageUrl = imageUrl;
- NavigateUrl = navigateUrl;
- Target = target;
- }
-
- [MonoTODO ("Not implemented")]
- protected TreeNode (TreeView owner, bool isRoot)
- {
- throw new NotImplementedException ();
- }
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- [Browsable (false)]
- public int Depth {
- get {
- if (depth != -1) return depth;
- depth = 0;
- TreeNode nod = parent;
- while (nod != null) {
- depth++;
- nod = nod.parent;
- }
- return depth;
- }
- }
-
- void ResetPathData ()
- {
- path = null;
- depth = -1;
- gotBinding = false;
- if (nodes != null) {
- foreach (TreeNode node in nodes)
- node.ResetPathData ();
- }
- }
-
- internal TreeView Tree {
- get { return tree; }
- set {
- if (SelectedFlag) {
- if (value != null)
- value.SetSelectedNode (this, false);
- if (tree != null)
- tree.SetSelectedNode (null, false);
- }
- tree = value;
- if (nodes != null)
- nodes.SetTree (tree);
- ResetPathData ();
- if (PopulateOnDemand && !Populated && Expanded.HasValue && Expanded.Value)
- Populate ();
- }
- }
-
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- [DefaultValue (false)]
- [Browsable (false)]
- public bool DataBound {
- get { return ViewState ["DataBound"] == null ? false : (bool) ViewState ["DataBound"]; }
- private set { ViewState ["DataBound"] = value; }
- }
-
- [DefaultValue (null)]
- [Browsable (false)]
- public object DataItem {
- get {
- return dataItem;
- }
- }
-
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- [DefaultValue ("")]
- [Browsable (false)]
- public string DataPath {
- get { return ViewState ["DataPath"] == null ? String.Empty : (String) ViewState ["DataPath"]; }
- private set { ViewState ["DataPath"] = value; }
- }
-
- [DefaultValue (false)]
- public bool Checked {
- get {
- object o = ViewState ["Checked"];
- if (o != null) return (bool)o;
- return false;
- }
- set {
- ViewState ["Checked"] = value;
- if (tree != null)
- tree.NotifyCheckChanged (this);
- }
- }
- [DefaultValue (null)]
- [MergableProperty (false)]
- [Browsable (false)]
- [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
- public TreeNodeCollection ChildNodes {
- get {
- if (nodes == null) {
- nodes = new TreeNodeCollection (this);
-
- if (IsTrackingViewState)
- ((IStateManager)nodes).TrackViewState();
- }
- return nodes;
- }
- }
-
- [DefaultValue (null)]
- public bool? Expanded {
- get {
- object o = ViewState ["Expanded"];
- return (bool?)o;
- }
- set {
- bool? current = (bool?) ViewState ["Expanded"];
- if (current == value)
- return;
- ViewState ["Expanded"] = value;
- if (tree != null)
- tree.NotifyExpandedChanged (this);
- if (PopulateOnDemand && !Populated && value.HasValue && value.Value)
- Populate ();
- }
- }
- [Localizable (true)]
- [DefaultValue ("")]
- public string ImageToolTip {
- get {
- object o = ViewState ["ImageToolTip"];
- if (o != null) return (string)o;
- return "";
- }
- set {
- ViewState ["ImageToolTip"] = value;
- }
- }
-
- [DefaultValue ("")]
- [UrlProperty]
- [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
- public string ImageUrl {
- get {
- object o = ViewState ["ImageUrl"];
- if (o != null) return (string)o;
- return "";
- }
- set {
- ViewState ["ImageUrl"] = value;
- }
- }
- [DefaultValue ("")]
- [UrlProperty]
- [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
- public string NavigateUrl {
- get {
- object o = ViewState ["NavigateUrl"];
- if (o != null) return (string)o;
- return "";
- }
- set {
- ViewState ["NavigateUrl"] = value;
- }
- }
- [DefaultValue (false)]
- public bool PopulateOnDemand {
- get {
- object o = ViewState ["PopulateOnDemand"];
- if (o != null) return (bool)o;
- return false;
- }
- set {
- ViewState ["PopulateOnDemand"] = value;
- }
- }
- [DefaultValue (TreeNodeSelectAction.Select)]
- public TreeNodeSelectAction SelectAction {
- get {
- object o = ViewState ["SelectAction"];
- if (o != null) return (TreeNodeSelectAction)o;
- return TreeNodeSelectAction.Select;
- }
- set {
- ViewState ["SelectAction"] = value;
- }
- }
- [DefaultValue (null)]
- public bool? ShowCheckBox {
- get {
- object o = ViewState ["ShowCheckBox"];
- return (bool?)o;
- }
- 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);
- }
- }
-
- [DefaultValue ("")]
- public string Target {
- get {
- object o = ViewState ["Target"];
- if(o != null) return (string)o;
- return "";
- }
- set {
- ViewState ["Target"] = value;
- }
- }
- [Localizable (true)]
- [DefaultValue ("")]
- [WebSysDescription ("The display text of the tree node.")]
- public string Text {
- get {
- object o = ViewState ["Text"];
- if (o == null)
- o = ViewState ["Value"];
- if (o != null) return (string)o;
- return String.Empty;
- }
- set {
- ViewState ["Text"] = value;
- }
- }
- [Localizable (true)]
- [DefaultValue ("")]
- public string ToolTip {
- get {
- object o = ViewState ["ToolTip"];
- if(o != null) return (string)o;
- return "";
- }
- set {
- ViewState ["ToolTip"] = value;
- }
- }
- [Localizable (true)]
- [DefaultValue ("")]
- public string Value {
- get {
- object o = ViewState ["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;
- }
- set {
- SelectedFlag = value;
-
- if (tree != null) {
- if (!value && tree.SelectedNode == this)
- tree.SetSelectedNode (null, false);
- else if (value)
- tree.SetSelectedNode (this, false);
- }
- }
- }
-
- internal virtual bool SelectedFlag {
- get {
- object o = ViewState ["Selected"];
- if(o != null) return (bool)o;
- return false;
- }
- set {
- ViewState ["Selected"] = value;
- }
- }
-
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- [Browsable (false)]
- public TreeNode Parent {
- get { return parent; }
- }
-
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- [Browsable (false)]
- public string ValuePath {
- get {
- if (tree == null) return Value;
-
- StringBuilder sb = new StringBuilder (Value);
- TreeNode node = parent;
- while (node != null) {
- sb.Insert (0, tree.PathSeparator);
- sb.Insert (0, node.Value);
- node = node.Parent;
- }
- return sb.ToString ();
- }
- }
-
- internal int Index {
- get { return index; }
- set { index = value; ResetPathData (); }
- }
-
- internal void SetParent (TreeNode node) {
- parent = node;
- ResetPathData ();
- }
-
- internal string Path {
- get {
- if (path != null) return path;
- StringBuilder sb = new StringBuilder (index.ToString());
- TreeNode node = parent;
- while (node != null) {
- sb.Insert (0, '_');
- sb.Insert (0, node.Index.ToString ());
- node = node.Parent;
- }
- path = sb.ToString ();
- return path;
- }
- }
-
- internal bool Populated {
- get {
- object o = ViewState ["Populated"];
- if (o != null) return (bool) o;
- return false;
- }
- set {
- ViewState ["Populated"] = value;
- }
- }
- internal bool HasChildData {
- get { return nodes != null; }
- }
-
- internal void Populate ()
- {
- if (tree == null)
- return;
- Populated = true;
- tree.NotifyPopulateRequired (this);
- }
-
- public void Collapse ()
- {
- Expanded = false;
- }
- public void CollapseAll ()
- {
- SetExpandedRec (false, -1);
- }
- public void Expand ()
- {
- Expanded = true;
- }
- internal void Expand (int depth)
- {
- SetExpandedRec (true, depth);
- }
- public void ExpandAll ()
- {
- SetExpandedRec (true, -1);
- }
-
- void SetExpandedRec (bool expanded, int depth)
- {
- Expanded = expanded;
- if (depth == 0) return;
-
- foreach (TreeNode nod in ChildNodes)
- nod.SetExpandedRec (expanded, depth - 1);
- }
-
- public void Select ()
- {
- Selected = true;
- }
-
- 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)
- {
- LoadViewState (savedState);
- }
- protected virtual void LoadViewState (object savedState)
- {
- if (savedState == null)
- return;
- object[] states = (object[]) savedState;
- ViewState.LoadViewState (states [0]);
-
- if (tree != null && SelectedFlag)
- tree.SetSelectedNode (this, true);
-
- if (!PopulateOnDemand || Populated)
- ((IStateManager)ChildNodes).LoadViewState (states [1]);
- }
-
- object IStateManager.SaveViewState ()
- {
- return SaveViewState ();
- }
- protected virtual object SaveViewState ()
- {
- object[] states = new object[2];
- states[0] = ViewState.SaveViewState();
- states[1] = (nodes == null ? null : ((IStateManager)nodes).SaveViewState());
-
- for (int i = 0; i < states.Length; i++) {
- if (states [i] != null)
- return states;
- }
- return null;
- }
- void IStateManager.TrackViewState ()
- {
- TrackViewState ();
- }
- protected void TrackViewState ()
- {
- if (marked) return;
- marked = true;
- ViewState.TrackViewState();
- if (nodes != null)
- ((IStateManager)nodes).TrackViewState ();
- }
-
- bool IStateManager.IsTrackingViewState {
- get { return IsTrackingViewState; }
- }
- protected bool IsTrackingViewState
- {
- get { return marked; }
- }
-
- internal void SetDirty ()
- {
- ViewState.SetDirty (true);
- if (nodes != null)
- nodes.SetDirty ();
- }
-
- public virtual object Clone ()
- {
- TreeNode nod = tree != null ? tree.CreateNode () : new TreeNode ();
- foreach (DictionaryEntry e in ViewState)
- nod.ViewState [(string)e.Key] = ((StateItem)e.Value).Value;
-
- foreach (TreeNode c in ChildNodes)
- nod.ChildNodes.Add ((TreeNode)c.Clone ());
-
- return nod;
- }
- object ICloneable.Clone () {
- return Clone ();
- }
-
- internal void Bind (IHierarchyData hierarchyData)
- {
- this.hierarchyData = hierarchyData;
- 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
- string text = null;
- if (bin.TextField.Length > 0) {
- text = Convert.ToString (GetBoundPropertyValue (bin.TextField));
- if (bin.FormatString.Length > 0)
- text = string.Format (bin.FormatString, text);
- }
- 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
- 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
- string value = null;
- if (bin.ValueField.Length > 0) {
- value = Convert.ToString (GetBoundPropertyValue (bin.ValueField));
- }
- 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)
- {
- dataItem = item;
- }
-
- internal void SetDataPath (string path)
- {
- DataPath = path;
- }
-
- internal void SetDataBound (bool bound)
- {
- DataBound = bound;
- }
-
- string GetDefaultBoundText ()
- {
- 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;
- }
-
- internal bool IsParentNode {
- get { return ChildNodes.Count > 0 || (PopulateOnDemand && !Populated); }
- }
-
- internal bool IsLeafNode {
- get { return !IsParentNode; }
- }
-
- internal bool IsRootNode {
- get { return Depth == 0; }
- }
-
- TreeNodeBinding GetBinding ()
- {
- if (tree == null) return null;
- if (gotBinding) return binding;
- binding = tree.FindBindingForNode (GetDataItemType (), Depth);
- gotBinding = true;
- return binding;
- }
-
- object GetBoundPropertyValue (string name)
- {
- if (boundProperties == null) {
- if (hierarchyData != null)
- boundProperties = TypeDescriptor.GetProperties (hierarchyData);
- else
- boundProperties = TypeDescriptor.GetProperties (dataItem);
- }
-
- PropertyDescriptor prop = boundProperties.Find (name, true);
- if (prop == null)
- throw new InvalidOperationException ("Property '" + name + "' not found in data bound item");
-
- if (hierarchyData != null)
- return prop.GetValue (hierarchyData);
- else
- return prop.GetValue (dataItem);
- }
- internal void BeginRenderText (HtmlTextWriter writer)
- {
- RenderPreText (writer);
- }
-
- internal void EndRenderText (HtmlTextWriter writer)
- {
- RenderPostText (writer);
- }
-
- protected virtual void RenderPreText (HtmlTextWriter writer)
- {
- }
-
- protected virtual void RenderPostText (HtmlTextWriter writer)
- {
- }
- }
- }
- #endif
|