TreeNode.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. IHierarchyData hierarchyData;
  50. bool gotBinding;
  51. TreeNodeBinding binding;
  52. PropertyDescriptorCollection boundProperties;
  53. public TreeNode ()
  54. {
  55. }
  56. public TreeNode (string text)
  57. {
  58. Text = text;
  59. }
  60. public TreeNode (string text, string value)
  61. {
  62. Text = text;
  63. Value = value;
  64. }
  65. public TreeNode (string text, string value, string imageUrl)
  66. {
  67. Text = text;
  68. Value = value;
  69. ImageUrl = imageUrl;
  70. }
  71. public TreeNode (string text, string value, string imageUrl, string navigateUrl, string target)
  72. {
  73. Text = text;
  74. Value = value;
  75. ImageUrl = imageUrl;
  76. NavigateUrl = navigateUrl;
  77. Target = target;
  78. }
  79. public int Depth {
  80. get {
  81. if (depth != -1) return depth;
  82. depth = 0;
  83. TreeNode nod = parent;
  84. while (nod != null) {
  85. depth++;
  86. nod = nod.parent;
  87. }
  88. return depth;
  89. }
  90. }
  91. void ResetPathData ()
  92. {
  93. path = null;
  94. depth = -1;
  95. gotBinding = false;
  96. }
  97. internal TreeView Tree {
  98. get { return tree; }
  99. set {
  100. if (SelectedFlag) {
  101. if (value != null)
  102. value.SetSelectedNode (this);
  103. else if (tree != null)
  104. tree.SetSelectedNode (null);
  105. }
  106. tree = value;
  107. if (nodes != null)
  108. nodes.SetTree (tree);
  109. ResetPathData ();
  110. }
  111. }
  112. public bool DataBound {
  113. get { return hierarchyData != null; }
  114. }
  115. public object DataItem {
  116. get {
  117. if (hierarchyData == null) throw new InvalidOperationException ("TreeNode is not data bound.");
  118. return hierarchyData.Item;
  119. }
  120. }
  121. public string DataPath {
  122. get {
  123. if (hierarchyData == null) throw new InvalidOperationException ("TreeNode is not data bound.");
  124. return hierarchyData.Path;
  125. }
  126. }
  127. public virtual bool Checked {
  128. get {
  129. object o = ViewState ["Checked"];
  130. if (o != null) return (bool)o;
  131. return false;
  132. }
  133. set {
  134. ViewState ["Checked"] = value;
  135. if (tree != null)
  136. tree.NotifyCheckChanged (this);
  137. }
  138. }
  139. [DefaultValue (null)]
  140. [MergableProperty (false)]
  141. [Browsable (false)]
  142. [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
  143. public virtual TreeNodeCollection ChildNodes {
  144. get {
  145. if (nodes == null) {
  146. if (DataBound)
  147. FillBoundChildren ();
  148. else
  149. nodes = new TreeNodeCollection (this);
  150. if (IsTrackingViewState)
  151. ((IStateManager)nodes).TrackViewState();
  152. }
  153. return nodes;
  154. }
  155. }
  156. public virtual bool Expanded {
  157. get {
  158. object o = ViewState ["Expanded"];
  159. if (o != null) return (bool)o;
  160. return false;
  161. }
  162. set {
  163. ViewState ["Expanded"] = value;
  164. if (tree != null)
  165. tree.NotifyExpandedChanged (this);
  166. }
  167. }
  168. public virtual string ImageToolTip {
  169. get {
  170. object o = ViewState ["ImageToolTip"];
  171. if (o != null) return (string)o;
  172. if (DataBound) {
  173. TreeNodeBinding bin = GetBinding ();
  174. if (bin != null) {
  175. if (bin.ImageToolTipField != "")
  176. return (string) GetBoundPropertyValue (bin.ImageToolTipField);
  177. return bin.ImageToolTip;
  178. }
  179. }
  180. return "";
  181. }
  182. set {
  183. ViewState ["ImageToolTip"] = value;
  184. }
  185. }
  186. public virtual string ImageUrl {
  187. get {
  188. object o = ViewState ["ImageUrl"];
  189. if (o != null) return (string)o;
  190. return "";
  191. }
  192. set {
  193. ViewState ["ImageUrl"] = value;
  194. }
  195. }
  196. public virtual string NavigateUrl {
  197. get {
  198. object o = ViewState ["NavigateUrl"];
  199. if (o != null) return (string)o;
  200. return "";
  201. }
  202. set {
  203. ViewState ["NavigateUrl"] = value;
  204. }
  205. }
  206. public bool PopulateOnDemand {
  207. get {
  208. object o = ViewState ["PopulateOnDemand"];
  209. if (o != null) return (bool)o;
  210. return false;
  211. }
  212. set {
  213. ViewState ["PopulateOnDemand"] = value;
  214. }
  215. }
  216. public TreeNodeSelectAction SelectAction {
  217. get {
  218. object o = ViewState ["SelectAction"];
  219. if (o != null) return (TreeNodeSelectAction)o;
  220. return TreeNodeSelectAction.Select;
  221. }
  222. set {
  223. ViewState ["SelectAction"] = value;
  224. }
  225. }
  226. public bool ShowCheckBox {
  227. get {
  228. object o = ViewState ["ShowCheckBox"];
  229. if (o != null) return (bool)o;
  230. return false;
  231. }
  232. set {
  233. ViewState ["ShowCheckBox"] = value;
  234. }
  235. }
  236. public virtual string Target {
  237. get {
  238. object o = ViewState ["Target"];
  239. if(o != null) return (string)o;
  240. return "";
  241. }
  242. set {
  243. ViewState ["Target"] = value;
  244. }
  245. }
  246. [Localizable (true)]
  247. [DefaultValue ("")]
  248. [WebSysDescription ("The display text of the tree node.")]
  249. public virtual string Text {
  250. get {
  251. object o = ViewState ["Text"];
  252. if(o != null) return (string)o;
  253. if (DataBound) {
  254. TreeNodeBinding bin = GetBinding ();
  255. if (bin != null) {
  256. if (bin.TextField != "")
  257. return (string) GetBoundPropertyValue (bin.TextField);
  258. if (bin.Text != "")
  259. return bin.Text;
  260. }
  261. return hierarchyData.ToString ();
  262. }
  263. return "";
  264. }
  265. set {
  266. ViewState ["Text"] = value;
  267. }
  268. }
  269. public virtual string ToolTip {
  270. get {
  271. object o = ViewState ["ToolTip"];
  272. if(o != null) return (string)o;
  273. return "";
  274. }
  275. set {
  276. ViewState ["ToolTip"] = value;
  277. }
  278. }
  279. public virtual string Value {
  280. get {
  281. object o = ViewState ["Value"];
  282. if(o != null) return (string)o;
  283. if (DataBound) {
  284. TreeNodeBinding bin = GetBinding ();
  285. if (bin != null) {
  286. if (bin.ValueField != "")
  287. return (string) GetBoundPropertyValue (bin.ValueField);
  288. if (bin.Value != "")
  289. return bin.Value;
  290. }
  291. return hierarchyData.ToString ();
  292. }
  293. return "";
  294. }
  295. set {
  296. ViewState ["Value"] = value;
  297. }
  298. }
  299. public virtual bool Selected {
  300. get {
  301. return SelectedFlag;
  302. }
  303. set {
  304. if (tree != null) {
  305. if (!value && tree.SelectedNode == this)
  306. tree.SetSelectedNode (null);
  307. else if (value)
  308. tree.SetSelectedNode (this);
  309. }
  310. else
  311. SelectedFlag = value;
  312. }
  313. }
  314. internal virtual bool SelectedFlag {
  315. get {
  316. object o = ViewState ["Selected"];
  317. if(o != null) return (bool)o;
  318. return false;
  319. }
  320. set {
  321. ViewState ["Selected"] = value;
  322. }
  323. }
  324. public TreeNode Parent {
  325. get { return parent; }
  326. }
  327. public string ValuePath {
  328. get {
  329. if (tree == null) return Value;
  330. StringBuilder sb = new StringBuilder (Value);
  331. TreeNode node = parent;
  332. while (node != null) {
  333. sb.Insert (0, tree.PathSeparator);
  334. sb.Insert (0, node.Value);
  335. node = node.Parent;
  336. }
  337. return sb.ToString ();
  338. }
  339. }
  340. internal int Index {
  341. get { return index; }
  342. set { index = value; ResetPathData (); }
  343. }
  344. internal void SetParent (TreeNode node) {
  345. parent = node;
  346. ResetPathData ();
  347. }
  348. internal string Path {
  349. get {
  350. if (path != null) return path;
  351. StringBuilder sb = new StringBuilder (index.ToString());
  352. TreeNode node = parent;
  353. while (node != null) {
  354. sb.Insert (0, '_');
  355. sb.Insert (0, node.Index.ToString ());
  356. node = node.Parent;
  357. }
  358. path = sb.ToString ();
  359. return path;
  360. }
  361. }
  362. internal bool HasChildData {
  363. get { return nodes != null; }
  364. }
  365. public void Collapse ()
  366. {
  367. Expanded = false;
  368. }
  369. public void CollapseAll ()
  370. {
  371. SetExpandedRec (false, -1);
  372. }
  373. public void Expand ()
  374. {
  375. Expanded = true;
  376. }
  377. internal void Expand (int depth)
  378. {
  379. SetExpandedRec (true, depth);
  380. }
  381. public void ExpandAll ()
  382. {
  383. SetExpandedRec (true, -1);
  384. }
  385. void SetExpandedRec (bool expanded, int depth)
  386. {
  387. Expanded = expanded;
  388. if (depth == 0) return;
  389. foreach (TreeNode nod in ChildNodes)
  390. nod.SetExpandedRec (expanded, depth - 1);
  391. }
  392. public void Select ()
  393. {
  394. Selected = true;
  395. }
  396. public void ToggleExpandState ()
  397. {
  398. Expanded = !Expanded;
  399. }
  400. public void LoadViewState (object savedState)
  401. {
  402. if (savedState == null)
  403. return;
  404. object[] states = (object[]) savedState;
  405. ViewState.LoadViewState (states [0]);
  406. if (tree != null && SelectedFlag)
  407. tree.SetSelectedNode (this);
  408. ((IStateManager)ChildNodes).LoadViewState (states [1]);
  409. }
  410. public object SaveViewState ()
  411. {
  412. object[] states = new object[2];
  413. states[0] = ViewState.SaveViewState();
  414. states[1] = (nodes == null ? null : ((IStateManager)nodes).SaveViewState());
  415. for (int i = 0; i < states.Length; i++) {
  416. if (states [i] != null)
  417. return states;
  418. }
  419. return null;
  420. }
  421. public void TrackViewState ()
  422. {
  423. marked = true;
  424. ViewState.TrackViewState();
  425. if (nodes != null)
  426. ((IStateManager)nodes).TrackViewState ();
  427. }
  428. public bool IsTrackingViewState
  429. {
  430. get { return marked; }
  431. }
  432. public object Clone ()
  433. {
  434. TreeNode nod = new TreeNode ();
  435. foreach (DictionaryEntry e in ViewState)
  436. nod.ViewState [(string)e.Key] = e.Value;
  437. foreach (TreeNode c in ChildNodes)
  438. nod.ChildNodes.Add ((TreeNode)c.Clone ());
  439. return nod;
  440. }
  441. internal void Bind (IHierarchyData hierarchyData)
  442. {
  443. this.hierarchyData = hierarchyData;
  444. }
  445. internal bool IsParentNode {
  446. get { return ChildNodes.Count > 0 && Parent != null; }
  447. }
  448. internal bool IsLeafNode {
  449. get { return ChildNodes.Count == 0; }
  450. }
  451. internal bool IsRootNode {
  452. get { return ChildNodes.Count > 0 && Parent == null; }
  453. }
  454. TreeNodeBinding GetBinding ()
  455. {
  456. if (tree == null) return null;
  457. if (gotBinding) return binding;
  458. binding = tree.FindBindingForNode (hierarchyData.Type, Depth);
  459. gotBinding = true;
  460. return binding;
  461. }
  462. object GetBoundPropertyValue (string name)
  463. {
  464. if (boundProperties == null) {
  465. ICustomTypeDescriptor desc = hierarchyData as ICustomTypeDescriptor;
  466. if (desc == null)
  467. throw new InvalidOperationException ("Property '" + name + "' not found in data bound item");
  468. boundProperties = desc.GetProperties ();
  469. }
  470. PropertyDescriptor prop = boundProperties.Find (name, true);
  471. if (prop == null)
  472. throw new InvalidOperationException ("Property '" + name + "' not found in data bound item");
  473. return prop.GetValue (hierarchyData);
  474. }
  475. void FillBoundChildren ()
  476. {
  477. nodes = new TreeNodeCollection (this);
  478. if (!hierarchyData.HasChildren) return;
  479. if (tree.MaxDataBindDepth != -1 && Depth >= tree.MaxDataBindDepth) return;
  480. IHierarchicalEnumerable e = hierarchyData.GetChildren ();
  481. foreach (object obj in e) {
  482. IHierarchyData hdata = e.GetHierarchyData (obj);
  483. TreeNode node = new TreeNode ();
  484. node.Bind (hdata);
  485. nodes.Add (node);
  486. }
  487. }
  488. }
  489. }
  490. #endif