TreeNode.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. //
  2. // System.Web.UI.WebControls.TreeNode.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2004 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  29. //
  30. #if NET_2_0
  31. using System;
  32. using System.Collections;
  33. using System.Text;
  34. using System.ComponentModel;
  35. using System.Web.UI;
  36. namespace System.Web.UI.WebControls
  37. {
  38. [ParseChildrenAttribute (true, "ChildNodes")]
  39. public class TreeNode: IStateManager, ICloneable
  40. {
  41. StateBag ViewState = new StateBag ();
  42. TreeNodeCollection nodes;
  43. bool marked;
  44. TreeView tree;
  45. TreeNode parent;
  46. int index;
  47. string path;
  48. int depth = -1;
  49. object dataItem;
  50. IHierarchyData hierarchyData;
  51. bool gotBinding;
  52. TreeNodeBinding binding;
  53. PropertyDescriptorCollection boundProperties;
  54. internal TreeNode (TreeView tree)
  55. {
  56. Tree = tree;
  57. }
  58. public TreeNode ()
  59. {
  60. }
  61. public TreeNode (string text)
  62. {
  63. Text = text;
  64. }
  65. public TreeNode (string text, string value)
  66. {
  67. Text = text;
  68. Value = value;
  69. }
  70. public TreeNode (string text, string value, string imageUrl)
  71. {
  72. Text = text;
  73. Value = value;
  74. ImageUrl = imageUrl;
  75. }
  76. public TreeNode (string text, string value, string imageUrl, string navigateUrl, string target)
  77. {
  78. Text = text;
  79. Value = value;
  80. ImageUrl = imageUrl;
  81. NavigateUrl = navigateUrl;
  82. Target = target;
  83. }
  84. [MonoTODO ("Not implemented")]
  85. protected TreeNode (TreeView owner, bool isRoot)
  86. {
  87. throw new NotImplementedException ();
  88. }
  89. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  90. [Browsable (false)]
  91. public int Depth {
  92. get {
  93. if (depth != -1) return depth;
  94. depth = 0;
  95. TreeNode nod = parent;
  96. while (nod != null) {
  97. depth++;
  98. nod = nod.parent;
  99. }
  100. return depth;
  101. }
  102. }
  103. void ResetPathData ()
  104. {
  105. path = null;
  106. depth = -1;
  107. gotBinding = false;
  108. }
  109. internal TreeView Tree {
  110. get { return tree; }
  111. set {
  112. if (SelectedFlag) {
  113. if (value != null)
  114. value.SetSelectedNode (this, false);
  115. if (tree != null)
  116. tree.SetSelectedNode (null, false);
  117. }
  118. tree = value;
  119. if (nodes != null)
  120. nodes.SetTree (tree);
  121. ResetPathData ();
  122. if (PopulateOnDemand && !Populated && Expanded.HasValue && Expanded.Value)
  123. Populate ();
  124. }
  125. }
  126. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  127. [DefaultValue (false)]
  128. [Browsable (false)]
  129. public bool DataBound {
  130. get { return ViewState ["DataBound"] == null ? false : (bool) ViewState ["DataBound"]; }
  131. private set { ViewState ["DataBound"] = value; }
  132. }
  133. [DefaultValue (null)]
  134. [Browsable (false)]
  135. public object DataItem {
  136. get {
  137. return dataItem;
  138. }
  139. }
  140. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  141. [DefaultValue ("")]
  142. [Browsable (false)]
  143. public string DataPath {
  144. get { return ViewState ["DataPath"] == null ? String.Empty : (String) ViewState ["DataPath"]; }
  145. private set { ViewState ["DataPath"] = value; }
  146. }
  147. [DefaultValue (false)]
  148. public bool Checked {
  149. get {
  150. object o = ViewState ["Checked"];
  151. if (o != null) return (bool)o;
  152. return false;
  153. }
  154. set {
  155. ViewState ["Checked"] = value;
  156. if (tree != null)
  157. tree.NotifyCheckChanged (this);
  158. }
  159. }
  160. [DefaultValue (null)]
  161. [MergableProperty (false)]
  162. [Browsable (false)]
  163. [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
  164. public TreeNodeCollection ChildNodes {
  165. get {
  166. if (nodes == null) {
  167. nodes = new TreeNodeCollection (this);
  168. if (IsTrackingViewState)
  169. ((IStateManager)nodes).TrackViewState();
  170. }
  171. return nodes;
  172. }
  173. }
  174. [DefaultValue (null)]
  175. public bool? Expanded {
  176. get {
  177. object o = ViewState ["Expanded"];
  178. return (bool?)o;
  179. }
  180. set {
  181. bool? current = (bool?) ViewState ["Expanded"];
  182. if (current == value)
  183. return;
  184. ViewState ["Expanded"] = value;
  185. if (tree != null)
  186. tree.NotifyExpandedChanged (this);
  187. if (PopulateOnDemand && !Populated && value.HasValue && value.Value)
  188. Populate ();
  189. }
  190. }
  191. [Localizable (true)]
  192. [DefaultValue ("")]
  193. public string ImageToolTip {
  194. get {
  195. object o = ViewState ["ImageToolTip"];
  196. if (o != null) return (string)o;
  197. return "";
  198. }
  199. set {
  200. ViewState ["ImageToolTip"] = value;
  201. }
  202. }
  203. [DefaultValue ("")]
  204. [UrlProperty]
  205. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  206. public string ImageUrl {
  207. get {
  208. object o = ViewState ["ImageUrl"];
  209. if (o != null) return (string)o;
  210. return "";
  211. }
  212. set {
  213. ViewState ["ImageUrl"] = value;
  214. }
  215. }
  216. [DefaultValue ("")]
  217. [UrlProperty]
  218. [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  219. public string NavigateUrl {
  220. get {
  221. object o = ViewState ["NavigateUrl"];
  222. if (o != null) return (string)o;
  223. return "";
  224. }
  225. set {
  226. ViewState ["NavigateUrl"] = value;
  227. }
  228. }
  229. [DefaultValue (false)]
  230. public bool PopulateOnDemand {
  231. get {
  232. object o = ViewState ["PopulateOnDemand"];
  233. if (o != null) return (bool)o;
  234. return false;
  235. }
  236. set {
  237. ViewState ["PopulateOnDemand"] = value;
  238. }
  239. }
  240. [DefaultValue (TreeNodeSelectAction.Select)]
  241. public TreeNodeSelectAction SelectAction {
  242. get {
  243. object o = ViewState ["SelectAction"];
  244. if (o != null) return (TreeNodeSelectAction)o;
  245. return TreeNodeSelectAction.Select;
  246. }
  247. set {
  248. ViewState ["SelectAction"] = value;
  249. }
  250. }
  251. [DefaultValue (null)]
  252. public bool? ShowCheckBox {
  253. get {
  254. object o = ViewState ["ShowCheckBox"];
  255. return (bool?)o;
  256. }
  257. set {
  258. ViewState ["ShowCheckBox"] = value;
  259. }
  260. }
  261. [DefaultValue ("")]
  262. public string Target {
  263. get {
  264. object o = ViewState ["Target"];
  265. if(o != null) return (string)o;
  266. return "";
  267. }
  268. set {
  269. ViewState ["Target"] = value;
  270. }
  271. }
  272. [Localizable (true)]
  273. [DefaultValue ("")]
  274. [WebSysDescription ("The display text of the tree node.")]
  275. public string Text {
  276. get {
  277. object o = ViewState ["Text"];
  278. if (o != null) return (string)o;
  279. return "";
  280. }
  281. set {
  282. ViewState ["Text"] = value;
  283. }
  284. }
  285. [Localizable (true)]
  286. [DefaultValue ("")]
  287. public string ToolTip {
  288. get {
  289. object o = ViewState ["ToolTip"];
  290. if(o != null) return (string)o;
  291. return "";
  292. }
  293. set {
  294. ViewState ["ToolTip"] = value;
  295. }
  296. }
  297. [Localizable (true)]
  298. [DefaultValue ("")]
  299. public string Value {
  300. get {
  301. object o = ViewState ["Value"];
  302. if(o != null) return (string)o;
  303. return "";
  304. }
  305. set {
  306. ViewState ["Value"] = value;
  307. }
  308. }
  309. [DefaultValue (false)]
  310. public bool Selected {
  311. get {
  312. return SelectedFlag;
  313. }
  314. set {
  315. SelectedFlag = value;
  316. if (tree != null) {
  317. if (!value && tree.SelectedNode == this)
  318. tree.SetSelectedNode (null, false);
  319. else if (value)
  320. tree.SetSelectedNode (this, false);
  321. }
  322. }
  323. }
  324. internal virtual bool SelectedFlag {
  325. get {
  326. object o = ViewState ["Selected"];
  327. if(o != null) return (bool)o;
  328. return false;
  329. }
  330. set {
  331. ViewState ["Selected"] = value;
  332. }
  333. }
  334. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  335. [Browsable (false)]
  336. public TreeNode Parent {
  337. get { return parent; }
  338. }
  339. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  340. [Browsable (false)]
  341. public string ValuePath {
  342. get {
  343. if (tree == null) return Value;
  344. StringBuilder sb = new StringBuilder (Value);
  345. TreeNode node = parent;
  346. while (node != null) {
  347. sb.Insert (0, tree.PathSeparator);
  348. sb.Insert (0, node.Value);
  349. node = node.Parent;
  350. }
  351. return sb.ToString ();
  352. }
  353. }
  354. internal int Index {
  355. get { return index; }
  356. set { index = value; ResetPathData (); }
  357. }
  358. internal void SetParent (TreeNode node) {
  359. parent = node;
  360. ResetPathData ();
  361. }
  362. internal string Path {
  363. get {
  364. if (path != null) return path;
  365. StringBuilder sb = new StringBuilder (index.ToString());
  366. TreeNode node = parent;
  367. while (node != null) {
  368. sb.Insert (0, '_');
  369. sb.Insert (0, node.Index.ToString ());
  370. node = node.Parent;
  371. }
  372. path = sb.ToString ();
  373. return path;
  374. }
  375. }
  376. internal bool Populated {
  377. get {
  378. object o = ViewState ["Populated"];
  379. if (o != null) return (bool) o;
  380. return false;
  381. }
  382. set {
  383. ViewState ["Populated"] = value;
  384. }
  385. }
  386. internal bool HasChildData {
  387. get { return nodes != null; }
  388. }
  389. internal void Populate ()
  390. {
  391. if (tree == null)
  392. return;
  393. Populated = true;
  394. tree.NotifyPopulateRequired (this);
  395. }
  396. public void Collapse ()
  397. {
  398. Expanded = false;
  399. }
  400. public void CollapseAll ()
  401. {
  402. SetExpandedRec (false, -1);
  403. }
  404. public void Expand ()
  405. {
  406. Expanded = true;
  407. }
  408. internal void Expand (int depth)
  409. {
  410. SetExpandedRec (true, depth);
  411. }
  412. public void ExpandAll ()
  413. {
  414. SetExpandedRec (true, -1);
  415. }
  416. void SetExpandedRec (bool expanded, int depth)
  417. {
  418. Expanded = expanded;
  419. if (depth == 0) return;
  420. foreach (TreeNode nod in ChildNodes)
  421. nod.SetExpandedRec (expanded, depth - 1);
  422. }
  423. public void Select ()
  424. {
  425. Selected = true;
  426. }
  427. public void ToggleExpandState ()
  428. {
  429. #if TARGET_JVM //No support for Nullable<bool>.GetValueOrDefault() yet
  430. bool? value = Expanded;
  431. Expanded = value.HasValue ? !value.Value : true;
  432. #else
  433. Expanded = !Expanded.GetValueOrDefault(false);
  434. #endif
  435. }
  436. void IStateManager.LoadViewState (object savedState)
  437. {
  438. LoadViewState (savedState);
  439. }
  440. protected virtual void LoadViewState (object savedState)
  441. {
  442. if (savedState == null)
  443. return;
  444. object[] states = (object[]) savedState;
  445. ViewState.LoadViewState (states [0]);
  446. if (tree != null && SelectedFlag)
  447. tree.SetSelectedNode (this, true);
  448. if (!PopulateOnDemand || Populated)
  449. ((IStateManager)ChildNodes).LoadViewState (states [1]);
  450. }
  451. object IStateManager.SaveViewState ()
  452. {
  453. return SaveViewState ();
  454. }
  455. protected virtual object SaveViewState ()
  456. {
  457. object[] states = new object[2];
  458. states[0] = ViewState.SaveViewState();
  459. states[1] = (nodes == null ? null : ((IStateManager)nodes).SaveViewState());
  460. for (int i = 0; i < states.Length; i++) {
  461. if (states [i] != null)
  462. return states;
  463. }
  464. return null;
  465. }
  466. void IStateManager.TrackViewState ()
  467. {
  468. TrackViewState ();
  469. }
  470. protected void TrackViewState ()
  471. {
  472. if (marked) return;
  473. marked = true;
  474. ViewState.TrackViewState();
  475. if (nodes != null)
  476. ((IStateManager)nodes).TrackViewState ();
  477. }
  478. bool IStateManager.IsTrackingViewState {
  479. get { return IsTrackingViewState; }
  480. }
  481. protected bool IsTrackingViewState
  482. {
  483. get { return marked; }
  484. }
  485. internal void SetDirty ()
  486. {
  487. ViewState.SetDirty (true);
  488. if (nodes != null)
  489. nodes.SetDirty ();
  490. }
  491. public virtual object Clone ()
  492. {
  493. TreeNode nod = tree != null ? tree.CreateNode () : new TreeNode ();
  494. foreach (DictionaryEntry e in ViewState)
  495. nod.ViewState [(string)e.Key] = ((StateItem)e.Value).Value;
  496. foreach (TreeNode c in ChildNodes)
  497. nod.ChildNodes.Add ((TreeNode)c.Clone ());
  498. return nod;
  499. }
  500. object ICloneable.Clone () {
  501. return Clone ();
  502. }
  503. internal void Bind (IHierarchyData hierarchyData)
  504. {
  505. this.hierarchyData = hierarchyData;
  506. DataBound = true;
  507. DataPath = hierarchyData.Path;
  508. dataItem = hierarchyData.Item;
  509. TreeNodeBinding bin = GetBinding ();
  510. if (bin != null) {
  511. // Bind ImageToolTip property
  512. if (bin.ImageToolTipField.Length > 0) {
  513. ImageToolTip = Convert.ToString (GetBoundPropertyValue (bin.ImageToolTipField));
  514. if (ImageToolTip.Length == 0)
  515. ImageToolTip = bin.ImageToolTip;
  516. }
  517. else if (bin.ImageToolTip.Length > 0)
  518. ImageToolTip = bin.ImageToolTip;
  519. // Bind ImageUrl property
  520. if (bin.ImageUrlField.Length > 0) {
  521. ImageUrl = Convert.ToString (GetBoundPropertyValue (bin.ImageUrlField));
  522. if (ImageUrl.Length == 0)
  523. ImageUrl = bin.ImageUrl;
  524. }
  525. else if (bin.ImageUrl.Length > 0)
  526. ImageUrl = bin.ImageUrl;
  527. // Bind NavigateUrl property
  528. if (bin.NavigateUrlField.Length > 0) {
  529. NavigateUrl = Convert.ToString (GetBoundPropertyValue (bin.NavigateUrlField));
  530. if (NavigateUrl.Length == 0)
  531. NavigateUrl = bin.NavigateUrl;
  532. }
  533. else if (bin.NavigateUrl.Length > 0)
  534. NavigateUrl = bin.NavigateUrl;
  535. // Bind PopulateOnDemand property
  536. if (bin.HasPropertyValue ("PopulateOnDemand"))
  537. PopulateOnDemand = bin.PopulateOnDemand;
  538. // Bind SelectAction property
  539. if (bin.HasPropertyValue ("SelectAction"))
  540. SelectAction = bin.SelectAction;
  541. // Bind ShowCheckBox property
  542. if (bin.HasPropertyValue ("ShowCheckBox"))
  543. ShowCheckBox = bin.ShowCheckBox;
  544. // Bind Target property
  545. if (bin.TargetField.Length > 0) {
  546. Target = Convert.ToString (GetBoundPropertyValue (bin.TargetField));
  547. if (Target.Length == 0)
  548. Target = bin.Target;
  549. }
  550. else if (bin.Target.Length > 0)
  551. Target = bin.Target;
  552. // Bind Text property
  553. if (bin.TextField.Length > 0) {
  554. Text = Convert.ToString (GetBoundPropertyValue (bin.TextField));
  555. if (bin.FormatString.Length > 0)
  556. Text = string.Format (bin.FormatString, Text);
  557. if (Text.Length == 0)
  558. Text = bin.Text;
  559. if (Text.Length == 0)
  560. Text = bin.Value;
  561. if (Text.Length == 0 && bin.ValueField.Length > 0)
  562. Text = Convert.ToString (GetBoundPropertyValue (bin.ValueField));
  563. }
  564. else if (bin.Text.Length > 0)
  565. Text = bin.Text;
  566. else if (bin.Value.Length > 0)
  567. Text = bin.Value;
  568. else
  569. Text = GetDefaultBoundText ();
  570. // Bind ToolTip property
  571. if (bin.ToolTipField.Length > 0) {
  572. ToolTip = Convert.ToString (GetBoundPropertyValue (bin.ToolTipField));
  573. if (ToolTip.Length == 0)
  574. ToolTip = bin.ToolTip;
  575. }
  576. else if (bin.ToolTip.Length > 0)
  577. ToolTip = bin.ToolTip;
  578. // Bind Value property
  579. if (bin.ValueField.Length > 0) {
  580. Value = Convert.ToString (GetBoundPropertyValue (bin.ValueField));
  581. if (Value.Length == 0)
  582. Value = bin.Value;
  583. if (Value.Length == 0)
  584. Value = bin.Text;
  585. if(Value.Length == 0 && bin.TextField.Length > 0)
  586. Value = Convert.ToString (GetBoundPropertyValue (bin.TextField));
  587. }
  588. else if (bin.Value.Length > 0)
  589. Value = bin.Value;
  590. else if (bin.Text.Length > 0)
  591. Value = bin.Text;
  592. else
  593. Value = GetDefaultBoundText ();
  594. } else {
  595. Text = Value = GetDefaultBoundText ();
  596. }
  597. }
  598. internal void SetDataItem (object item)
  599. {
  600. dataItem = item;
  601. }
  602. internal void SetDataPath (string path)
  603. {
  604. DataPath = path;
  605. }
  606. internal void SetDataBound (bool bound)
  607. {
  608. DataBound = bound;
  609. }
  610. string GetDefaultBoundText ()
  611. {
  612. if (hierarchyData != null) return hierarchyData.ToString ();
  613. else if (dataItem != null) return dataItem.ToString ();
  614. else return string.Empty;
  615. }
  616. string GetDataItemType ()
  617. {
  618. if (hierarchyData != null) return hierarchyData.Type;
  619. else if (dataItem != null) return dataItem.GetType().ToString ();
  620. else return string.Empty;
  621. }
  622. internal bool IsParentNode {
  623. get { return ChildNodes.Count > 0 || (PopulateOnDemand && !Populated); }
  624. }
  625. internal bool IsLeafNode {
  626. get { return !IsParentNode; }
  627. }
  628. internal bool IsRootNode {
  629. get { return Depth == 0; }
  630. }
  631. TreeNodeBinding GetBinding ()
  632. {
  633. if (tree == null) return null;
  634. if (gotBinding) return binding;
  635. binding = tree.FindBindingForNode (GetDataItemType (), Depth);
  636. gotBinding = true;
  637. return binding;
  638. }
  639. object GetBoundPropertyValue (string name)
  640. {
  641. if (boundProperties == null) {
  642. if (hierarchyData != null)
  643. boundProperties = TypeDescriptor.GetProperties (hierarchyData);
  644. else
  645. boundProperties = TypeDescriptor.GetProperties (dataItem);
  646. }
  647. PropertyDescriptor prop = boundProperties.Find (name, true);
  648. if (prop == null)
  649. throw new InvalidOperationException ("Property '" + name + "' not found in data bound item");
  650. if (hierarchyData != null)
  651. return prop.GetValue (hierarchyData);
  652. else
  653. return prop.GetValue (dataItem);
  654. }
  655. internal void BeginRenderText (HtmlTextWriter writer)
  656. {
  657. RenderPreText (writer);
  658. }
  659. internal void EndRenderText (HtmlTextWriter writer)
  660. {
  661. RenderPostText (writer);
  662. }
  663. protected virtual void RenderPreText (HtmlTextWriter writer)
  664. {
  665. }
  666. protected virtual void RenderPostText (HtmlTextWriter writer)
  667. {
  668. }
  669. }
  670. }
  671. #endif