TreeNode.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. bool dataBound;
  50. string dataPath;
  51. object dataItem;
  52. IHierarchyData hierarchyData;
  53. bool gotBinding;
  54. TreeNodeBinding binding;
  55. PropertyDescriptorCollection boundProperties;
  56. internal TreeNode (TreeView tree)
  57. {
  58. Tree = tree;
  59. }
  60. public TreeNode ()
  61. {
  62. }
  63. public TreeNode (string text)
  64. {
  65. Text = text;
  66. }
  67. public TreeNode (string text, string value)
  68. {
  69. Text = text;
  70. Value = value;
  71. }
  72. public TreeNode (string text, string value, string imageUrl)
  73. {
  74. Text = text;
  75. Value = value;
  76. ImageUrl = imageUrl;
  77. }
  78. public TreeNode (string text, string value, string imageUrl, string navigateUrl, string target)
  79. {
  80. Text = text;
  81. Value = value;
  82. ImageUrl = imageUrl;
  83. NavigateUrl = navigateUrl;
  84. Target = target;
  85. }
  86. [MonoTODO]
  87. protected TreeNode (TreeView owner, bool isRoot)
  88. {
  89. throw new NotImplementedException ();
  90. }
  91. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  92. [Browsable (false)]
  93. public int Depth {
  94. get {
  95. if (depth != -1) return depth;
  96. depth = 0;
  97. TreeNode nod = parent;
  98. while (nod != null) {
  99. depth++;
  100. nod = nod.parent;
  101. }
  102. return depth;
  103. }
  104. }
  105. void ResetPathData ()
  106. {
  107. path = null;
  108. depth = -1;
  109. gotBinding = false;
  110. }
  111. internal TreeView Tree {
  112. get { return tree; }
  113. set {
  114. if (SelectedFlag) {
  115. if (value != null)
  116. value.SetSelectedNode (this, false);
  117. else if (tree != null)
  118. tree.SetSelectedNode (null, false);
  119. }
  120. tree = value;
  121. if (nodes != null)
  122. nodes.SetTree (tree);
  123. ResetPathData ();
  124. }
  125. }
  126. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  127. [DefaultValue (false)]
  128. [Browsable (false)]
  129. public bool DataBound {
  130. get { return dataBound; }
  131. }
  132. [DefaultValue (null)]
  133. [Browsable (false)]
  134. public object DataItem {
  135. get {
  136. if (!dataBound) throw new InvalidOperationException ("TreeNode is not data bound.");
  137. return dataItem;
  138. }
  139. }
  140. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  141. [DefaultValue ("")]
  142. [Browsable (false)]
  143. public string DataPath {
  144. get {
  145. if (!dataBound) throw new InvalidOperationException ("TreeNode is not data bound.");
  146. return dataPath;
  147. }
  148. }
  149. [DefaultValue (false)]
  150. public bool Checked {
  151. get {
  152. object o = ViewState ["Checked"];
  153. if (o != null) return (bool)o;
  154. return false;
  155. }
  156. set {
  157. ViewState ["Checked"] = value;
  158. if (tree != null)
  159. tree.NotifyCheckChanged (this);
  160. }
  161. }
  162. [DefaultValue (null)]
  163. [MergableProperty (false)]
  164. [Browsable (false)]
  165. [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
  166. public TreeNodeCollection ChildNodes {
  167. get {
  168. if (nodes == null) {
  169. if (PopulateOnDemand && tree == null)
  170. return null;
  171. if (DataBound)
  172. FillBoundChildren ();
  173. else
  174. nodes = new TreeNodeCollection (this);
  175. if (IsTrackingViewState)
  176. ((IStateManager)nodes).TrackViewState();
  177. if (PopulateOnDemand && !Populated) {
  178. Populated = true;
  179. Populate ();
  180. }
  181. }
  182. return nodes;
  183. }
  184. }
  185. [DefaultValue (null)]
  186. public bool? Expanded {
  187. get {
  188. object o = ViewState ["Expanded"];
  189. if (o != null) return (bool)o;
  190. return true;
  191. }
  192. set {
  193. ViewState ["Expanded"] = value;
  194. if (tree != null)
  195. tree.NotifyExpandedChanged (this);
  196. }
  197. }
  198. [Localizable (true)]
  199. [DefaultValue ("")]
  200. public string ImageToolTip {
  201. get {
  202. object o = ViewState ["ImageToolTip"];
  203. if (o != null) return (string)o;
  204. if (DataBound) {
  205. TreeNodeBinding bin = GetBinding ();
  206. if (bin != null) {
  207. if (bin.ImageToolTipField != "")
  208. return (string) GetBoundPropertyValue (bin.ImageToolTipField);
  209. return bin.ImageToolTip;
  210. }
  211. }
  212. return "";
  213. }
  214. set {
  215. ViewState ["ImageToolTip"] = value;
  216. }
  217. }
  218. [DefaultValue ("")]
  219. [UrlProperty]
  220. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  221. public string ImageUrl {
  222. get {
  223. object o = ViewState ["ImageUrl"];
  224. if (o != null) return (string)o;
  225. if (DataBound) {
  226. TreeNodeBinding bin = GetBinding ();
  227. if (bin != null) {
  228. if (bin.ImageUrlField != "")
  229. return (string) GetBoundPropertyValue (bin.ImageUrlField);
  230. return bin.ImageUrl;
  231. }
  232. }
  233. return "";
  234. }
  235. set {
  236. ViewState ["ImageUrl"] = value;
  237. }
  238. }
  239. [DefaultValue ("")]
  240. [UrlProperty]
  241. [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  242. public string NavigateUrl {
  243. get {
  244. object o = ViewState ["NavigateUrl"];
  245. if (o != null) return (string)o;
  246. if (DataBound) {
  247. TreeNodeBinding bin = GetBinding ();
  248. if (bin != null) {
  249. if (bin.NavigateUrlField != "")
  250. return (string) GetBoundPropertyValue (bin.NavigateUrlField);
  251. return bin.NavigateUrl;
  252. }
  253. }
  254. return "";
  255. }
  256. set {
  257. ViewState ["NavigateUrl"] = value;
  258. }
  259. }
  260. [DefaultValue (false)]
  261. public bool PopulateOnDemand {
  262. get {
  263. object o = ViewState ["PopulateOnDemand"];
  264. if (o != null) return (bool)o;
  265. if (DataBound) {
  266. TreeNodeBinding bin = GetBinding ();
  267. if (bin != null)
  268. return bin.PopulateOnDemand;
  269. }
  270. return false;
  271. }
  272. set {
  273. ViewState ["PopulateOnDemand"] = value;
  274. }
  275. }
  276. [DefaultValue (TreeNodeSelectAction.Select)]
  277. public TreeNodeSelectAction SelectAction {
  278. get {
  279. object o = ViewState ["SelectAction"];
  280. if (o != null) return (TreeNodeSelectAction)o;
  281. if (DataBound) {
  282. TreeNodeBinding bin = GetBinding ();
  283. if (bin != null)
  284. return bin.SelectAction;
  285. }
  286. return TreeNodeSelectAction.Select;
  287. }
  288. set {
  289. ViewState ["SelectAction"] = value;
  290. }
  291. }
  292. [DefaultValue (null)]
  293. public bool? ShowCheckBox {
  294. get {
  295. object o = ViewState ["ShowCheckBox"];
  296. if (o != null) return (bool)o;
  297. if (DataBound) {
  298. TreeNodeBinding bin = GetBinding ();
  299. if (bin != null)
  300. return bin.ShowCheckBox;
  301. }
  302. return true;
  303. }
  304. set {
  305. ViewState ["ShowCheckBox"] = value;
  306. }
  307. }
  308. internal bool IsShowCheckBoxSet {
  309. get { return ViewState ["ShowCheckBox"] != null; }
  310. }
  311. [DefaultValue ("")]
  312. public string Target {
  313. get {
  314. object o = ViewState ["Target"];
  315. if(o != null) return (string)o;
  316. if (DataBound) {
  317. TreeNodeBinding bin = GetBinding ();
  318. if (bin != null) {
  319. if (bin.TargetField != "")
  320. return (string) GetBoundPropertyValue (bin.TargetField);
  321. return bin.Target;
  322. }
  323. }
  324. return "";
  325. }
  326. set {
  327. ViewState ["Target"] = value;
  328. }
  329. }
  330. [Localizable (true)]
  331. [DefaultValue ("")]
  332. [WebSysDescription ("The display text of the tree node.")]
  333. public string Text {
  334. get {
  335. object o = ViewState ["Text"];
  336. if (o != null) return (string)o;
  337. if (DataBound) {
  338. TreeNodeBinding bin = GetBinding ();
  339. if (bin != null) {
  340. string text;
  341. if (bin.TextField != "")
  342. text = (string) GetBoundPropertyValue (bin.TextField);
  343. else if (bin.Text != "")
  344. text = bin.Text;
  345. else
  346. text = GetDefaultBoundText ();
  347. if (bin.FormatString.Length != 0)
  348. text = string.Format (bin.FormatString, text);
  349. return text;
  350. }
  351. return GetDefaultBoundText ();
  352. }
  353. return "";
  354. }
  355. set {
  356. ViewState ["Text"] = value;
  357. }
  358. }
  359. [Localizable (true)]
  360. [DefaultValue ("")]
  361. public string ToolTip {
  362. get {
  363. object o = ViewState ["ToolTip"];
  364. if(o != null) return (string)o;
  365. if (DataBound) {
  366. TreeNodeBinding bin = GetBinding ();
  367. if (bin != null) {
  368. if (bin.ToolTipField != "")
  369. return (string) GetBoundPropertyValue (bin.ToolTipField);
  370. return bin.ToolTip;
  371. }
  372. }
  373. return "";
  374. }
  375. set {
  376. ViewState ["ToolTip"] = value;
  377. }
  378. }
  379. [Localizable (true)]
  380. [DefaultValue ("")]
  381. public string Value {
  382. get {
  383. object o = ViewState ["Value"];
  384. if(o != null) return (string)o;
  385. if (DataBound) {
  386. TreeNodeBinding bin = GetBinding ();
  387. if (bin != null) {
  388. if (bin.ValueField != "")
  389. return (string) GetBoundPropertyValue (bin.ValueField);
  390. if (bin.Value != "")
  391. return bin.Value;
  392. }
  393. return GetDefaultBoundText ();
  394. }
  395. return "";
  396. }
  397. set {
  398. ViewState ["Value"] = value;
  399. }
  400. }
  401. [DefaultValue (false)]
  402. public bool Selected {
  403. get {
  404. return SelectedFlag;
  405. }
  406. set {
  407. if (tree != null) {
  408. if (!value && tree.SelectedNode == this)
  409. tree.SetSelectedNode (null, false);
  410. else if (value)
  411. tree.SetSelectedNode (this, false);
  412. }
  413. else
  414. SelectedFlag = value;
  415. }
  416. }
  417. internal virtual bool SelectedFlag {
  418. get {
  419. object o = ViewState ["Selected"];
  420. if(o != null) return (bool)o;
  421. return false;
  422. }
  423. set {
  424. ViewState ["Selected"] = value;
  425. }
  426. }
  427. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  428. [Browsable (false)]
  429. public TreeNode Parent {
  430. get { return parent; }
  431. }
  432. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  433. [Browsable (false)]
  434. public string ValuePath {
  435. get {
  436. if (tree == null) return Value;
  437. StringBuilder sb = new StringBuilder (Value);
  438. TreeNode node = parent;
  439. while (node != null) {
  440. sb.Insert (0, tree.PathSeparator);
  441. sb.Insert (0, node.Value);
  442. node = node.Parent;
  443. }
  444. return sb.ToString ();
  445. }
  446. }
  447. internal int Index {
  448. get { return index; }
  449. set { index = value; ResetPathData (); }
  450. }
  451. internal void SetParent (TreeNode node) {
  452. parent = node;
  453. ResetPathData ();
  454. }
  455. internal string Path {
  456. get {
  457. if (path != null) return path;
  458. StringBuilder sb = new StringBuilder (index.ToString());
  459. TreeNode node = parent;
  460. while (node != null) {
  461. sb.Insert (0, '_');
  462. sb.Insert (0, node.Index.ToString ());
  463. node = node.Parent;
  464. }
  465. path = sb.ToString ();
  466. return path;
  467. }
  468. }
  469. internal bool Populated {
  470. get {
  471. object o = ViewState ["Populated"];
  472. if (o != null) return (bool) o;
  473. return false;
  474. }
  475. set {
  476. ViewState ["Populated"] = value;
  477. }
  478. }
  479. internal bool HasChildData {
  480. get { return nodes != null; }
  481. }
  482. internal void Populate ()
  483. {
  484. tree.NotifyPopulateRequired (this);
  485. }
  486. public void Collapse ()
  487. {
  488. Expanded = false;
  489. }
  490. public void CollapseAll ()
  491. {
  492. SetExpandedRec (false, -1);
  493. }
  494. public void Expand ()
  495. {
  496. Expanded = true;
  497. }
  498. internal void Expand (int depth)
  499. {
  500. SetExpandedRec (true, depth);
  501. }
  502. public void ExpandAll ()
  503. {
  504. SetExpandedRec (true, -1);
  505. }
  506. void SetExpandedRec (bool expanded, int depth)
  507. {
  508. Expanded = expanded;
  509. if (depth == 0) return;
  510. foreach (TreeNode nod in ChildNodes)
  511. nod.SetExpandedRec (expanded, depth - 1);
  512. }
  513. public void Select ()
  514. {
  515. Selected = true;
  516. }
  517. public void ToggleExpandState ()
  518. {
  519. Expanded = !Expanded;
  520. }
  521. void IStateManager.LoadViewState (object savedState)
  522. {
  523. LoadViewState (savedState);
  524. }
  525. protected virtual void LoadViewState (object savedState)
  526. {
  527. if (savedState == null)
  528. return;
  529. object[] states = (object[]) savedState;
  530. ViewState.LoadViewState (states [0]);
  531. if (tree != null && SelectedFlag)
  532. tree.SetSelectedNode (this, true);
  533. if (!PopulateOnDemand || Populated)
  534. ((IStateManager)ChildNodes).LoadViewState (states [1]);
  535. }
  536. object IStateManager.SaveViewState ()
  537. {
  538. return SaveViewState ();
  539. }
  540. protected virtual object SaveViewState ()
  541. {
  542. object[] states = new object[2];
  543. states[0] = ViewState.SaveViewState();
  544. states[1] = (nodes == null ? null : ((IStateManager)nodes).SaveViewState());
  545. for (int i = 0; i < states.Length; i++) {
  546. if (states [i] != null)
  547. return states;
  548. }
  549. return null;
  550. }
  551. void IStateManager.TrackViewState ()
  552. {
  553. TrackViewState ();
  554. }
  555. protected void TrackViewState ()
  556. {
  557. if (marked) return;
  558. marked = true;
  559. ViewState.TrackViewState();
  560. if (nodes != null)
  561. ((IStateManager)nodes).TrackViewState ();
  562. }
  563. bool IStateManager.IsTrackingViewState {
  564. get { return IsTrackingViewState; }
  565. }
  566. protected bool IsTrackingViewState
  567. {
  568. get { return marked; }
  569. }
  570. internal void SetDirty ()
  571. {
  572. ViewState.SetDirty (true);
  573. }
  574. public virtual object Clone ()
  575. {
  576. TreeNode nod = tree != null ? tree.CreateNode () : new TreeNode ();
  577. foreach (DictionaryEntry e in ViewState)
  578. nod.ViewState [(string)e.Key] = e.Value;
  579. foreach (TreeNode c in ChildNodes)
  580. nod.ChildNodes.Add ((TreeNode)c.Clone ());
  581. return nod;
  582. }
  583. internal void Bind (IHierarchyData hierarchyData)
  584. {
  585. this.hierarchyData = hierarchyData;
  586. dataBound = true;
  587. dataPath = hierarchyData.Path;
  588. dataItem = hierarchyData.Item;
  589. }
  590. internal void SetDataItem (object item)
  591. {
  592. dataItem = item;
  593. }
  594. internal void SetDataPath (string path)
  595. {
  596. dataPath = path;
  597. }
  598. internal void SetDataBound (bool bound)
  599. {
  600. dataBound = bound;
  601. }
  602. string GetDefaultBoundText ()
  603. {
  604. if (hierarchyData != null) return hierarchyData.ToString ();
  605. else if (dataItem != null) return dataItem.ToString ();
  606. else return string.Empty;
  607. }
  608. string GetDataItemType ()
  609. {
  610. if (hierarchyData != null) return hierarchyData.Type;
  611. else if (dataItem != null) return dataItem.GetType().ToString ();
  612. else return string.Empty;
  613. }
  614. internal bool IsParentNode {
  615. get { return ChildNodes.Count > 0 && Parent != null; }
  616. }
  617. internal bool IsLeafNode {
  618. get { return ChildNodes.Count == 0; }
  619. }
  620. internal bool IsRootNode {
  621. get { return ChildNodes.Count > 0 && Parent == null; }
  622. }
  623. TreeNodeBinding GetBinding ()
  624. {
  625. if (tree == null) return null;
  626. if (gotBinding) return binding;
  627. binding = tree.FindBindingForNode (GetDataItemType (), Depth);
  628. gotBinding = true;
  629. return binding;
  630. }
  631. object GetBoundPropertyValue (string name)
  632. {
  633. if (boundProperties == null) {
  634. if (hierarchyData != null)
  635. boundProperties = TypeDescriptor.GetProperties (hierarchyData);
  636. else
  637. boundProperties = TypeDescriptor.GetProperties (dataItem);
  638. }
  639. PropertyDescriptor prop = boundProperties.Find (name, true);
  640. if (prop == null)
  641. throw new InvalidOperationException ("Property '" + name + "' not found in data bound item");
  642. if (hierarchyData != null)
  643. return prop.GetValue (hierarchyData);
  644. else
  645. return prop.GetValue (dataItem);
  646. }
  647. void FillBoundChildren ()
  648. {
  649. nodes = new TreeNodeCollection (this);
  650. if (hierarchyData == null || !hierarchyData.HasChildren) return;
  651. if (tree.MaxDataBindDepth != -1 && Depth >= tree.MaxDataBindDepth) return;
  652. IHierarchicalEnumerable e = hierarchyData.GetChildren ();
  653. foreach (object obj in e) {
  654. IHierarchyData hdata = e.GetHierarchyData (obj);
  655. TreeNode node = tree != null ? tree.CreateNode () : new TreeNode ();
  656. node.Bind (hdata);
  657. nodes.Add (node);
  658. }
  659. }
  660. internal void BeginRenderText (HtmlTextWriter writer)
  661. {
  662. RenderPreText (writer);
  663. }
  664. internal void EndRenderText (HtmlTextWriter writer)
  665. {
  666. RenderPostText (writer);
  667. }
  668. protected virtual void RenderPreText (HtmlTextWriter writer)
  669. {
  670. }
  671. protected virtual void RenderPostText (HtmlTextWriter writer)
  672. {
  673. }
  674. }
  675. }
  676. #endif