TreeView.cs 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055
  1. //
  2. // System.Web.UI.WebControls.TreeView.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.Collections;
  32. using System.Text;
  33. using System.ComponentModel;
  34. using System.Globalization;
  35. using System.Web.Handlers;
  36. using System.Collections.Specialized;
  37. using System.IO;
  38. using System.Security.Permissions;
  39. using System.Collections.Generic;
  40. using System.Web.Util;
  41. namespace System.Web.UI.WebControls
  42. {
  43. // CAS
  44. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  45. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  46. // attributes
  47. [SupportsEventValidation]
  48. [ControlValueProperty ("SelectedValue")]
  49. [DefaultEvent ("SelectedNodeChanged")]
  50. [Designer ("System.Web.UI.Design.WebControls.TreeViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  51. public class TreeView: HierarchicalDataBoundControl, IPostBackEventHandler, IPostBackDataHandler, ICallbackEventHandler
  52. {
  53. string activeSiteMapPath;
  54. bool stylesPrepared;
  55. Style hoverNodeStyle;
  56. TreeNodeStyle leafNodeStyle;
  57. TreeNodeStyle nodeStyle;
  58. TreeNodeStyle parentNodeStyle;
  59. TreeNodeStyle rootNodeStyle;
  60. TreeNodeStyle selectedNodeStyle;
  61. TreeNodeStyleCollection levelStyles;
  62. TreeNodeCollection nodes;
  63. TreeNodeBindingCollection dataBindings;
  64. TreeNode selectedNode;
  65. Hashtable bindings;
  66. int registeredStylesCounter = -1;
  67. List<Style> levelLinkStyles;
  68. Style controlLinkStyle;
  69. Style nodeLinkStyle;
  70. Style rootNodeLinkStyle;
  71. Style parentNodeLinkStyle;
  72. Style leafNodeLinkStyle;
  73. Style selectedNodeLinkStyle;
  74. Style hoverNodeLinkStyle;
  75. static readonly object TreeNodeCheckChangedEvent = new object();
  76. static readonly object SelectedNodeChangedEvent = new object();
  77. static readonly object TreeNodeCollapsedEvent = new object();
  78. static readonly object TreeNodeDataBoundEvent = new object();
  79. static readonly object TreeNodeExpandedEvent = new object();
  80. static readonly object TreeNodePopulateEvent = new object();
  81. static Hashtable imageStyles = new Hashtable ();
  82. class TreeViewExpandDepthConverter : TypeConverter
  83. {
  84. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  85. {
  86. if (sourceType == typeof (string) || sourceType == typeof (int))
  87. return true;
  88. return base.CanConvertFrom (context, sourceType);
  89. }
  90. public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
  91. {
  92. if (destinationType == typeof (string) || destinationType == typeof (int))
  93. return true;
  94. return base.CanConvertTo (context, destinationType);
  95. }
  96. public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  97. {
  98. if (destinationType != typeof (int) && destinationType != typeof (string))
  99. return base.ConvertTo (context, culture, value, destinationType);
  100. if (value is string) {
  101. if (destinationType == typeof (int)) {
  102. if (String.Compare ("FullyExpand", (string)value, StringComparison.OrdinalIgnoreCase) == 0)
  103. return -1;
  104. try {
  105. return Int32.Parse ((string)value);
  106. } catch (Exception) {
  107. return -1;
  108. }
  109. } else
  110. return value;
  111. }
  112. int val = (int)value;
  113. if (destinationType == typeof (string)) {
  114. if (val == -1)
  115. return "FullyExpand";
  116. return val.ToString ();
  117. }
  118. return value;
  119. }
  120. public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
  121. {
  122. if (!(value is string) && !(value is int))
  123. return base.ConvertFrom (context, culture, value);
  124. if (value is string) {
  125. if (String.Compare ("FullyExpand", (string)value, StringComparison.OrdinalIgnoreCase) == 0)
  126. return -1;
  127. try {
  128. return Int32.Parse ((string)value);
  129. } catch (Exception) {
  130. return null;
  131. }
  132. }
  133. return value;
  134. }
  135. }
  136. class ImageStyle
  137. {
  138. public ImageStyle (string expand, string collapse, string noExpand, string icon, string iconLeaf, string iconRoot) {
  139. Expand = expand;
  140. Collapse = collapse;
  141. NoExpand = noExpand;
  142. RootIcon = iconRoot;
  143. ParentIcon = icon;
  144. LeafIcon = iconLeaf;
  145. }
  146. public string Expand;
  147. public string Collapse;
  148. public string NoExpand;
  149. public string RootIcon;
  150. public string ParentIcon;
  151. public string LeafIcon;
  152. }
  153. static TreeView ()
  154. {
  155. imageStyles [TreeViewImageSet.Arrows] = new ImageStyle ("arrow_plus", "arrow_minus", "arrow_noexpand", null, null, null);
  156. imageStyles [TreeViewImageSet.BulletedList] = new ImageStyle (null, null, null, "dot_full", "dot_empty", "dot_full");
  157. imageStyles [TreeViewImageSet.BulletedList2] = new ImageStyle (null, null, null, "box_full", "box_empty", "box_full");
  158. imageStyles [TreeViewImageSet.BulletedList3] = new ImageStyle (null, null, null, "star_full", "star_empty", "star_full");
  159. imageStyles [TreeViewImageSet.BulletedList4] = new ImageStyle (null, null, null, "star_full", "star_empty", "dots");
  160. imageStyles [TreeViewImageSet.Contacts] = new ImageStyle ("TreeView_plus", "TreeView_minus", "contact", null, null, null);
  161. imageStyles [TreeViewImageSet.Events] = new ImageStyle (null, null, null, "warning", "warning", "warning");
  162. imageStyles [TreeViewImageSet.Inbox] = new ImageStyle (null, null, null, "inbox", "inbox", "inbox");
  163. imageStyles [TreeViewImageSet.Msdn] = new ImageStyle ("box_plus", "box_minus", "box_noexpand", null, null, null);
  164. imageStyles [TreeViewImageSet.Simple] = new ImageStyle (null, null, "box_full", null, null, null);
  165. imageStyles [TreeViewImageSet.Simple2] = new ImageStyle (null, null, "box_empty", null, null, null);
  166. // TODO
  167. imageStyles [TreeViewImageSet.News] = new ImageStyle ("TreeView_plus", "TreeView_minus", "TreeView_noexpand", null, null, null);
  168. imageStyles [TreeViewImageSet.Faq] = new ImageStyle ("TreeView_plus", "TreeView_minus", "TreeView_noexpand", null, null, null);
  169. imageStyles [TreeViewImageSet.WindowsHelp] = new ImageStyle ("TreeView_plus", "TreeView_minus", "TreeView_noexpand", null, null, null);
  170. imageStyles [TreeViewImageSet.XPFileExplorer] = new ImageStyle ("TreeView_plus", "TreeView_minus", "TreeView_noexpand", "folder", "file", "computer");
  171. }
  172. public event TreeNodeEventHandler TreeNodeCheckChanged {
  173. add { Events.AddHandler (TreeNodeCheckChangedEvent, value); }
  174. remove { Events.RemoveHandler (TreeNodeCheckChangedEvent, value); }
  175. }
  176. public event EventHandler SelectedNodeChanged {
  177. add { Events.AddHandler (SelectedNodeChangedEvent, value); }
  178. remove { Events.RemoveHandler (SelectedNodeChangedEvent, value); }
  179. }
  180. public event TreeNodeEventHandler TreeNodeCollapsed {
  181. add { Events.AddHandler (TreeNodeCollapsedEvent, value); }
  182. remove { Events.RemoveHandler (TreeNodeCollapsedEvent, value); }
  183. }
  184. public event TreeNodeEventHandler TreeNodeDataBound {
  185. add { Events.AddHandler (TreeNodeDataBoundEvent, value); }
  186. remove { Events.RemoveHandler (TreeNodeDataBoundEvent, value); }
  187. }
  188. public event TreeNodeEventHandler TreeNodeExpanded {
  189. add { Events.AddHandler (TreeNodeExpandedEvent, value); }
  190. remove { Events.RemoveHandler (TreeNodeExpandedEvent, value); }
  191. }
  192. public event TreeNodeEventHandler TreeNodePopulate {
  193. add { Events.AddHandler (TreeNodePopulateEvent, value); }
  194. remove { Events.RemoveHandler (TreeNodePopulateEvent, value); }
  195. }
  196. protected virtual void OnTreeNodeCheckChanged (TreeNodeEventArgs e)
  197. {
  198. if (Events != null) {
  199. TreeNodeEventHandler eh = (TreeNodeEventHandler) Events [TreeNodeCheckChangedEvent];
  200. if (eh != null) eh (this, e);
  201. }
  202. }
  203. protected virtual void OnSelectedNodeChanged (EventArgs e)
  204. {
  205. if (Events != null) {
  206. EventHandler eh = (EventHandler) Events [SelectedNodeChangedEvent];
  207. if (eh != null) eh (this, e);
  208. }
  209. }
  210. protected virtual void OnTreeNodeCollapsed (TreeNodeEventArgs e)
  211. {
  212. if (Events != null) {
  213. TreeNodeEventHandler eh = (TreeNodeEventHandler) Events [TreeNodeCollapsedEvent];
  214. if (eh != null) eh (this, e);
  215. }
  216. }
  217. protected virtual void OnTreeNodeDataBound (TreeNodeEventArgs e)
  218. {
  219. if (Events != null) {
  220. TreeNodeEventHandler eh = (TreeNodeEventHandler) Events [TreeNodeDataBoundEvent];
  221. if (eh != null) eh (this, e);
  222. }
  223. }
  224. protected virtual void OnTreeNodeExpanded (TreeNodeEventArgs e)
  225. {
  226. if (Events != null) {
  227. TreeNodeEventHandler eh = (TreeNodeEventHandler) Events [TreeNodeExpandedEvent];
  228. if (eh != null) eh (this, e);
  229. }
  230. }
  231. protected virtual void OnTreeNodePopulate (TreeNodeEventArgs e)
  232. {
  233. if (Events != null) {
  234. TreeNodeEventHandler eh = (TreeNodeEventHandler) Events [TreeNodePopulateEvent];
  235. if (eh != null) eh (this, e);
  236. }
  237. }
  238. [Localizable (true)]
  239. public string CollapseImageToolTip {
  240. get {
  241. return ViewState.GetString ("CollapseImageToolTip", "Collapse {0}");
  242. }
  243. set {
  244. ViewState["CollapseImageToolTip"] = value;
  245. }
  246. }
  247. [MonoTODO ("Implement support for this")]
  248. [WebCategory ("Behavior")]
  249. [WebSysDescription ("Whether the tree will automatically generate bindings.")]
  250. [DefaultValue (true)]
  251. public bool AutoGenerateDataBindings {
  252. get {
  253. return ViewState.GetBool ("AutoGenerateDataBindings", true);
  254. }
  255. set {
  256. ViewState["AutoGenerateDataBindings"] = value;
  257. }
  258. }
  259. [DefaultValue ("")]
  260. [WebSysDescription ("The url of the image to show when a node can be collapsed.")]
  261. [UrlProperty]
  262. [WebCategory ("Appearance")]
  263. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  264. public string CollapseImageUrl {
  265. get {
  266. return ViewState.GetString ("CollapseImageUrl", "");
  267. }
  268. set {
  269. ViewState["CollapseImageUrl"] = value;
  270. }
  271. }
  272. [WebCategory ("Data")]
  273. [PersistenceMode (PersistenceMode.InnerProperty)]
  274. [WebSysDescription ("Bindings for tree nodes.")]
  275. [Editor ("System.Web.UI.Design.WebControls.TreeViewBindingsEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  276. [DefaultValue (null)]
  277. [MergablePropertyAttribute (false)]
  278. public TreeNodeBindingCollection DataBindings {
  279. get {
  280. if (dataBindings == null) {
  281. dataBindings = new TreeNodeBindingCollection ();
  282. if (IsTrackingViewState)
  283. ((IStateManager)dataBindings).TrackViewState();
  284. }
  285. return dataBindings;
  286. }
  287. }
  288. [WebCategory ("Behavior")]
  289. [WebSysDescription ("Whether the tree view can use client-side script to expand and collapse nodes.")]
  290. [Themeable (false)]
  291. [DefaultValue (true)]
  292. public bool EnableClientScript {
  293. get {
  294. return ViewState.GetBool ("EnableClientScript", true);
  295. }
  296. set {
  297. ViewState["EnableClientScript"] = value;
  298. }
  299. }
  300. [DefaultValue (-1)]
  301. [WebCategory ("Behavior")]
  302. [WebSysDescription ("The initial expand depth.")]
  303. [TypeConverter ("System.Web.UI.WebControls.TreeView+TreeViewExpandDepthConverter, " + Consts.AssemblySystem_Web)]
  304. public int ExpandDepth {
  305. get {
  306. return ViewState.GetInt ("ExpandDepth", -1);
  307. }
  308. set {
  309. ViewState["ExpandDepth"] = value;
  310. }
  311. }
  312. [Localizable (true)]
  313. public string ExpandImageToolTip {
  314. get {
  315. return ViewState.GetString ("ExpandImageToolTip", "Expand {0}");
  316. }
  317. set {
  318. ViewState["ExpandImageToolTip"] = value;
  319. }
  320. }
  321. [DefaultValue ("")]
  322. [UrlProperty]
  323. [WebSysDescription ("The url of the image to show when a node can be expanded.")]
  324. [WebCategory ("Appearance")]
  325. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  326. public string ExpandImageUrl {
  327. get {
  328. return ViewState.GetString ("ExpandImageUrl", "");
  329. }
  330. set {
  331. ViewState["ExpandImageUrl"] = value;
  332. }
  333. }
  334. [PersistenceMode (PersistenceMode.InnerProperty)]
  335. [NotifyParentProperty (true)]
  336. [DefaultValue (null)]
  337. [WebCategory ("Styles")]
  338. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  339. public Style HoverNodeStyle {
  340. get {
  341. if (hoverNodeStyle == null) {
  342. hoverNodeStyle = new Style();
  343. if (IsTrackingViewState)
  344. hoverNodeStyle.TrackViewState();
  345. }
  346. return hoverNodeStyle;
  347. }
  348. }
  349. [DefaultValue (TreeViewImageSet.Custom)]
  350. public TreeViewImageSet ImageSet {
  351. get {
  352. return (TreeViewImageSet)ViewState.GetInt ("ImageSet", (int)TreeViewImageSet.Custom);
  353. }
  354. set {
  355. if (!Enum.IsDefined (typeof (TreeViewImageSet), value))
  356. throw new ArgumentOutOfRangeException ();
  357. ViewState["ImageSet"] = value;
  358. }
  359. }
  360. [PersistenceMode (PersistenceMode.InnerProperty)]
  361. [NotifyParentProperty (true)]
  362. [DefaultValue (null)]
  363. [WebCategory ("Styles")]
  364. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  365. public TreeNodeStyle LeafNodeStyle {
  366. get {
  367. if (leafNodeStyle == null) {
  368. leafNodeStyle = new TreeNodeStyle ();
  369. if (IsTrackingViewState)
  370. leafNodeStyle.TrackViewState();
  371. }
  372. return leafNodeStyle;
  373. }
  374. }
  375. [DefaultValue (null)]
  376. [WebCategory ("Styles")]
  377. [PersistenceMode (PersistenceMode.InnerProperty)]
  378. [Editor ("System.Web.UI.Design.WebControls.TreeNodeStyleCollectionEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  379. public TreeNodeStyleCollection LevelStyles {
  380. get {
  381. if (levelStyles == null) {
  382. levelStyles = new TreeNodeStyleCollection ();
  383. if (IsTrackingViewState)
  384. ((IStateManager)levelStyles).TrackViewState();
  385. }
  386. return levelStyles;
  387. }
  388. }
  389. [DefaultValue ("")]
  390. public string LineImagesFolder {
  391. get {
  392. return ViewState.GetString ("LineImagesFolder", "");
  393. }
  394. set {
  395. ViewState["LineImagesFolder"] = value;
  396. }
  397. }
  398. [DefaultValue (-1)]
  399. public int MaxDataBindDepth {
  400. get {
  401. return ViewState.GetInt ("MaxDataBindDepth", -1);
  402. }
  403. set {
  404. ViewState["MaxDataBindDepth"] = value;
  405. }
  406. }
  407. [DefaultValue (20)]
  408. public int NodeIndent {
  409. get {
  410. return ViewState.GetInt ("NodeIndent", 20);
  411. }
  412. set {
  413. ViewState["NodeIndent"] = value;
  414. }
  415. }
  416. [PersistenceMode (PersistenceMode.InnerProperty)]
  417. [Editor ("System.Web.UI.Design.WebControls.TreeNodeCollectionEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  418. [DefaultValueAttribute (null)]
  419. [MergablePropertyAttribute (false)]
  420. public TreeNodeCollection Nodes {
  421. get {
  422. if (nodes == null) {
  423. nodes = new TreeNodeCollection (this);
  424. if (IsTrackingViewState)
  425. ((IStateManager)nodes).TrackViewState();
  426. }
  427. return nodes;
  428. }
  429. }
  430. [PersistenceMode (PersistenceMode.InnerProperty)]
  431. [NotifyParentProperty (true)]
  432. [DefaultValue (null)]
  433. [WebCategory ("Styles")]
  434. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  435. public TreeNodeStyle NodeStyle {
  436. get {
  437. if (nodeStyle == null) {
  438. nodeStyle = new TreeNodeStyle ();
  439. if (IsTrackingViewState)
  440. nodeStyle.TrackViewState();
  441. }
  442. return nodeStyle;
  443. }
  444. }
  445. [DefaultValue (false)]
  446. public bool NodeWrap {
  447. get {
  448. return ViewState.GetBool ("NodeWrap", false);
  449. }
  450. set {
  451. ViewState ["NodeWrap"] = value;
  452. }
  453. }
  454. [UrlProperty]
  455. [DefaultValue ("")]
  456. [WebSysDescription ("The url of the image to show for leaf nodes.")]
  457. [WebCategory ("Appearance")]
  458. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  459. public string NoExpandImageUrl {
  460. get {
  461. return ViewState.GetString ("NoExpandImageUrl", "");
  462. }
  463. set {
  464. ViewState ["NoExpandImageUrl"] = value;
  465. }
  466. }
  467. [PersistenceMode (PersistenceMode.InnerProperty)]
  468. [NotifyParentProperty (true)]
  469. [DefaultValue (null)]
  470. [WebCategory ("Styles")]
  471. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  472. public TreeNodeStyle ParentNodeStyle {
  473. get {
  474. if (parentNodeStyle == null) {
  475. parentNodeStyle = new TreeNodeStyle ();
  476. if (IsTrackingViewState)
  477. parentNodeStyle.TrackViewState();
  478. }
  479. return parentNodeStyle;
  480. }
  481. }
  482. [DefaultValue ('/')]
  483. public char PathSeparator {
  484. get {
  485. return ViewState.GetChar ("PathSeparator", '/');
  486. }
  487. set {
  488. ViewState ["PathSeparator"] = value;
  489. }
  490. }
  491. [DefaultValue (true)]
  492. public bool PopulateNodesFromClient {
  493. get {
  494. return ViewState.GetBool ("PopulateNodesFromClient", true);
  495. }
  496. set {
  497. ViewState ["PopulateNodesFromClient"] = value;
  498. }
  499. }
  500. [PersistenceMode (PersistenceMode.InnerProperty)]
  501. [NotifyParentProperty (true)]
  502. [DefaultValue (null)]
  503. [WebCategory ("Styles")]
  504. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  505. public TreeNodeStyle RootNodeStyle {
  506. get {
  507. if (rootNodeStyle == null) {
  508. rootNodeStyle = new TreeNodeStyle ();
  509. if (IsTrackingViewState)
  510. rootNodeStyle.TrackViewState();
  511. }
  512. return rootNodeStyle;
  513. }
  514. }
  515. [PersistenceMode (PersistenceMode.InnerProperty)]
  516. [NotifyParentProperty (true)]
  517. [DefaultValue (null)]
  518. [WebCategory ("Styles")]
  519. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  520. public TreeNodeStyle SelectedNodeStyle {
  521. get {
  522. if (selectedNodeStyle == null) {
  523. selectedNodeStyle = new TreeNodeStyle ();
  524. if (IsTrackingViewState)
  525. selectedNodeStyle.TrackViewState();
  526. }
  527. return selectedNodeStyle;
  528. }
  529. }
  530. Style ControlLinkStyle {
  531. get {
  532. if (controlLinkStyle == null) {
  533. controlLinkStyle = new Style ();
  534. controlLinkStyle.AlwaysRenderTextDecoration = true;
  535. }
  536. return controlLinkStyle;
  537. }
  538. }
  539. Style NodeLinkStyle {
  540. get {
  541. if (nodeLinkStyle == null) {
  542. nodeLinkStyle = new Style ();
  543. }
  544. return nodeLinkStyle;
  545. }
  546. }
  547. Style RootNodeLinkStyle {
  548. get {
  549. if (rootNodeLinkStyle == null) {
  550. rootNodeLinkStyle = new Style ();
  551. }
  552. return rootNodeLinkStyle;
  553. }
  554. }
  555. Style ParentNodeLinkStyle {
  556. get {
  557. if (parentNodeLinkStyle == null) {
  558. parentNodeLinkStyle = new Style ();
  559. }
  560. return parentNodeLinkStyle;
  561. }
  562. }
  563. Style SelectedNodeLinkStyle {
  564. get {
  565. if (selectedNodeLinkStyle == null) {
  566. selectedNodeLinkStyle = new Style ();
  567. }
  568. return selectedNodeLinkStyle;
  569. }
  570. }
  571. Style LeafNodeLinkStyle {
  572. get {
  573. if (leafNodeLinkStyle == null) {
  574. leafNodeLinkStyle = new Style ();
  575. }
  576. return leafNodeLinkStyle;
  577. }
  578. }
  579. Style HoverNodeLinkStyle {
  580. get {
  581. if (hoverNodeLinkStyle == null) {
  582. hoverNodeLinkStyle = new Style ();
  583. }
  584. return hoverNodeLinkStyle;
  585. }
  586. }
  587. [DefaultValue (TreeNodeTypes.None)]
  588. public TreeNodeTypes ShowCheckBoxes {
  589. get {
  590. return (TreeNodeTypes)ViewState.GetInt ("ShowCheckBoxes", (int)TreeNodeTypes.None);
  591. }
  592. set {
  593. if ((int) value > 7)
  594. throw new ArgumentOutOfRangeException ();
  595. ViewState ["ShowCheckBoxes"] = value;
  596. }
  597. }
  598. [DefaultValue (true)]
  599. public bool ShowExpandCollapse {
  600. get {
  601. return ViewState.GetBool ("ShowExpandCollapse", true);
  602. }
  603. set {
  604. ViewState ["ShowExpandCollapse"] = value;
  605. }
  606. }
  607. [DefaultValue (false)]
  608. public bool ShowLines {
  609. get {
  610. return ViewState.GetBool ("ShowLines", false);
  611. }
  612. set {
  613. ViewState ["ShowLines"] = value;
  614. }
  615. }
  616. [Localizable (true)]
  617. public string SkipLinkText
  618. {
  619. get {
  620. return ViewState.GetString ("SkipLinkText", "Skip Navigation Links.");
  621. }
  622. set {
  623. ViewState ["SkipLinkText"] = value;
  624. }
  625. }
  626. [Browsable (false)]
  627. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  628. public TreeNode SelectedNode {
  629. get { return selectedNode; }
  630. }
  631. [Browsable (false)]
  632. [DefaultValue ("")]
  633. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  634. public string SelectedValue {
  635. get { return selectedNode != null ? selectedNode.Value : ""; }
  636. }
  637. [DefaultValue ("")]
  638. public string Target {
  639. get {
  640. return ViewState.GetString ("Target", "");
  641. }
  642. set {
  643. ViewState ["Target"] = value;
  644. }
  645. }
  646. [MonoTODO ("why override?")]
  647. public override bool Visible
  648. {
  649. get {
  650. return base.Visible;
  651. }
  652. set {
  653. base.Visible = value;
  654. }
  655. }
  656. [Browsable (false)]
  657. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  658. public TreeNodeCollection CheckedNodes {
  659. get {
  660. TreeNodeCollection col = new TreeNodeCollection ();
  661. FindCheckedNodes (Nodes, col);
  662. return col;
  663. }
  664. }
  665. void FindCheckedNodes (TreeNodeCollection nodeList, TreeNodeCollection result)
  666. {
  667. foreach (TreeNode node in nodeList) {
  668. if (node.Checked) result.Add (node, false);
  669. FindCheckedNodes (node.ChildNodes, result);
  670. }
  671. }
  672. public void ExpandAll ()
  673. {
  674. foreach (TreeNode node in Nodes)
  675. node.ExpandAll ();
  676. }
  677. public void CollapseAll ()
  678. {
  679. foreach (TreeNode node in Nodes)
  680. node.CollapseAll ();
  681. }
  682. public TreeNode FindNode (string valuePath)
  683. {
  684. if (valuePath == null) throw new ArgumentNullException ("valuePath");
  685. string[] path = valuePath.Split (PathSeparator);
  686. int n = 0;
  687. TreeNodeCollection col = Nodes;
  688. bool foundBranch = true;
  689. while (col.Count > 0 && foundBranch) {
  690. foundBranch = false;
  691. foreach (TreeNode node in col) {
  692. if (node.Value == path [n]) {
  693. if (++n == path.Length) return node;
  694. col = node.ChildNodes;
  695. foundBranch = true;
  696. break;
  697. }
  698. }
  699. }
  700. return null;
  701. }
  702. ImageStyle GetImageStyle ()
  703. {
  704. if (ImageSet != TreeViewImageSet.Custom)
  705. return (ImageStyle) imageStyles [ImageSet];
  706. else
  707. return null;
  708. }
  709. protected override HtmlTextWriterTag TagKey {
  710. get { return HtmlTextWriterTag.Div; }
  711. }
  712. protected internal virtual TreeNode CreateNode ()
  713. {
  714. return new TreeNode (this);
  715. }
  716. public sealed override void DataBind ()
  717. {
  718. base.DataBind ();
  719. }
  720. protected void SetNodeDataBound (TreeNode node, bool dataBound)
  721. {
  722. node.SetDataBound (dataBound);
  723. }
  724. protected void SetNodeDataPath (TreeNode node, string dataPath)
  725. {
  726. node.SetDataPath (dataPath);
  727. }
  728. protected void SetNodeDataItem (TreeNode node, object dataItem)
  729. {
  730. node.SetDataItem (dataItem);
  731. }
  732. protected internal override void OnInit (EventArgs e)
  733. {
  734. base.OnInit (e);
  735. }
  736. internal void SetSelectedNode (TreeNode node, bool loading)
  737. {
  738. if (selectedNode == node) return;
  739. if (selectedNode != null)
  740. selectedNode.SelectedFlag = false;
  741. selectedNode = node;
  742. if (!loading)
  743. OnSelectedNodeChanged (new TreeNodeEventArgs (selectedNode));
  744. }
  745. internal void NotifyCheckChanged (TreeNode node)
  746. {
  747. OnTreeNodeCheckChanged (new TreeNodeEventArgs (node));
  748. }
  749. internal void NotifyExpandedChanged (TreeNode node)
  750. {
  751. if (node.Expanded.HasValue && node.Expanded.Value)
  752. OnTreeNodeExpanded (new TreeNodeEventArgs (node));
  753. else if (node.Expanded.HasValue && node.IsParentNode)
  754. OnTreeNodeCollapsed (new TreeNodeEventArgs (node));
  755. }
  756. internal void NotifyPopulateRequired (TreeNode node)
  757. {
  758. OnTreeNodePopulate (new TreeNodeEventArgs (node));
  759. }
  760. protected override void TrackViewState()
  761. {
  762. EnsureDataBound ();
  763. base.TrackViewState();
  764. if (hoverNodeStyle != null) {
  765. hoverNodeStyle.TrackViewState();
  766. }
  767. if (leafNodeStyle != null) {
  768. leafNodeStyle.TrackViewState();
  769. }
  770. if (levelStyles != null && levelStyles.Count > 0) {
  771. ((IStateManager)levelStyles).TrackViewState();
  772. }
  773. if (nodeStyle != null) {
  774. nodeStyle.TrackViewState();
  775. }
  776. if (parentNodeStyle != null) {
  777. parentNodeStyle.TrackViewState();
  778. }
  779. if (rootNodeStyle != null) {
  780. rootNodeStyle.TrackViewState();
  781. }
  782. if (selectedNodeStyle != null) {
  783. selectedNodeStyle.TrackViewState();
  784. }
  785. if (dataBindings != null) {
  786. ((IStateManager)dataBindings).TrackViewState ();
  787. }
  788. if (nodes != null) {
  789. ((IStateManager)nodes).TrackViewState();;
  790. }
  791. }
  792. protected override object SaveViewState()
  793. {
  794. object[] states = new object [10];
  795. states[0] = base.SaveViewState();
  796. states[1] = (hoverNodeStyle == null ? null : hoverNodeStyle.SaveViewState());
  797. states[2] = (leafNodeStyle == null ? null : leafNodeStyle.SaveViewState());
  798. states[3] = (levelStyles == null ? null : ((IStateManager)levelStyles).SaveViewState());
  799. states[4] = (nodeStyle == null ? null : nodeStyle.SaveViewState());
  800. states[5] = (parentNodeStyle == null ? null : parentNodeStyle.SaveViewState());
  801. states[6] = (rootNodeStyle == null ? null : rootNodeStyle.SaveViewState());
  802. states[7] = (selectedNodeStyle == null ? null : selectedNodeStyle.SaveViewState());
  803. states[8] = (dataBindings == null ? null : ((IStateManager)dataBindings).SaveViewState());
  804. states[9] = (nodes == null ? null : ((IStateManager)nodes).SaveViewState());
  805. for (int i = states.Length - 1; i >= 0; i--) {
  806. if (states [i] != null)
  807. return states;
  808. }
  809. return null;
  810. }
  811. protected override void LoadViewState (object savedState)
  812. {
  813. if (savedState == null)
  814. return;
  815. object [] states = (object []) savedState;
  816. base.LoadViewState (states[0]);
  817. if (states[1] != null)
  818. HoverNodeStyle.LoadViewState (states[1]);
  819. if (states[2] != null)
  820. LeafNodeStyle.LoadViewState(states[2]);
  821. if (states[3] != null)
  822. ((IStateManager)LevelStyles).LoadViewState(states[3]);
  823. if (states[4] != null)
  824. NodeStyle.LoadViewState(states[4]);
  825. if (states[5] != null)
  826. ParentNodeStyle.LoadViewState(states[5]);
  827. if (states[6] != null)
  828. RootNodeStyle.LoadViewState(states[6]);
  829. if (states[7] != null)
  830. SelectedNodeStyle.LoadViewState(states[7]);
  831. if (states[8] != null)
  832. ((IStateManager)DataBindings).LoadViewState(states[8]);
  833. if (states[9] != null)
  834. ((IStateManager)Nodes).LoadViewState(states[9]);
  835. }
  836. protected virtual void RaisePostBackEvent (string eventArgument)
  837. {
  838. ValidateEvent (UniqueID, eventArgument);
  839. string[] args = eventArgument.Split ('|');
  840. TreeNode node = FindNodeByPos (args[1]);
  841. if (node == null) return;
  842. if (args [0] == "sel")
  843. HandleSelectEvent (node);
  844. else if (args [0] == "ec")
  845. HandleExpandCollapseEvent (node);
  846. }
  847. void HandleSelectEvent (TreeNode node)
  848. {
  849. switch (node.SelectAction) {
  850. case TreeNodeSelectAction.Select:
  851. node.Select ();
  852. break;
  853. case TreeNodeSelectAction.Expand:
  854. node.Expand ();
  855. break;
  856. case TreeNodeSelectAction.SelectExpand:
  857. node.Select ();
  858. node.Expand ();
  859. break;
  860. }
  861. }
  862. void HandleExpandCollapseEvent (TreeNode node)
  863. {
  864. node.ToggleExpandState ();
  865. }
  866. protected virtual void RaisePostDataChangedEvent ()
  867. {
  868. }
  869. TreeNode MakeNodeTree (string[] args)
  870. {
  871. string[] segments = args [0].Split ('_');
  872. TreeNode ret = null, node;
  873. StringBuilder sb = new StringBuilder ();
  874. foreach (string seg in segments) {
  875. int idx = Int32.Parse (seg);
  876. node = new TreeNode (seg);
  877. if (ret != null) {
  878. ret.ChildNodes.Add (node);
  879. node.Index = idx;
  880. Console.WriteLine (node.Path);
  881. }
  882. ret = node;
  883. }
  884. ret.Text = args [1];
  885. ret.Value = args [2];
  886. ret.ImageUrl = args [3];
  887. ret.NavigateUrl = args [4];
  888. ret.Target = args [5];
  889. ret.Tree = this;
  890. NotifyPopulateRequired (ret);
  891. return ret;
  892. }
  893. string callbackResult;
  894. protected virtual void RaiseCallbackEvent (string eventArgs)
  895. {
  896. string[] args = eventArgs.Split ('|');
  897. RequiresDataBinding = true;
  898. EnsureDataBound ();
  899. TreeNode node = MakeNodeTree (args);
  900. ArrayList levelLines = new ArrayList ();
  901. TreeNode nd = node;
  902. while (nd != null) {
  903. int childCount = nd.Parent != null ? nd.Parent.ChildNodes.Count : Nodes.Count;
  904. levelLines.Insert (0, (nd.Index < childCount - 1) ? this : null);
  905. nd = nd.Parent;
  906. }
  907. StringWriter sw = new StringWriter ();
  908. HtmlTextWriter writer = new HtmlTextWriter (sw);
  909. EnsureStylesPrepared ();
  910. node.Expanded = true;
  911. int num = node.ChildNodes.Count;
  912. for (int n=0; n<num; n++)
  913. RenderNode (writer, node.ChildNodes [n], node.Depth + 1, levelLines, true, n<num-1);
  914. string res = sw.ToString ();
  915. callbackResult = res.Length > 0 ? res : "*";
  916. }
  917. protected virtual string GetCallbackResult ()
  918. {
  919. return callbackResult;
  920. }
  921. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  922. {
  923. RaisePostBackEvent (eventArgument);
  924. }
  925. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  926. {
  927. return LoadPostData (postDataKey, postCollection);
  928. }
  929. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  930. {
  931. RaisePostDataChangedEvent ();
  932. }
  933. void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
  934. {
  935. RaiseCallbackEvent (eventArgs);
  936. }
  937. string ICallbackEventHandler.GetCallbackResult ()
  938. {
  939. return GetCallbackResult ();
  940. }
  941. protected override ControlCollection CreateControlCollection ()
  942. {
  943. return new EmptyControlCollection (this);
  944. }
  945. protected internal override void PerformDataBinding ()
  946. {
  947. base.PerformDataBinding ();
  948. InitializeDataBindings ();
  949. HierarchicalDataSourceView data = GetData ("");
  950. if (data == null)
  951. return;
  952. Nodes.Clear ();
  953. IHierarchicalEnumerable e = data.Select ();
  954. FillBoundChildrenRecursive (e, Nodes);
  955. }
  956. void FillBoundChildrenRecursive (IHierarchicalEnumerable hEnumerable, TreeNodeCollection nodeCollection)
  957. {
  958. if (hEnumerable == null)
  959. return;
  960. foreach (object obj in hEnumerable) {
  961. IHierarchyData hdata = hEnumerable.GetHierarchyData (obj);
  962. TreeNode child = new TreeNode ();
  963. nodeCollection.Add (child);
  964. child.Bind (hdata);
  965. OnTreeNodeDataBound (new TreeNodeEventArgs (child));
  966. if (MaxDataBindDepth >= 0 && child.Depth == MaxDataBindDepth)
  967. continue;
  968. if (hdata == null || !hdata.HasChildren)
  969. continue;
  970. IHierarchicalEnumerable e = hdata.GetChildren ();
  971. FillBoundChildrenRecursive (e, child.ChildNodes);
  972. }
  973. }
  974. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  975. {
  976. bool res = false;
  977. if (EnableClientScript && PopulateNodesFromClient) {
  978. string states = postCollection [ClientID + "_PopulatedStates"];
  979. if (states != null) {
  980. foreach (string id in states.Split ('|')) {
  981. if (String.IsNullOrEmpty(id))
  982. continue;
  983. TreeNode node = FindNodeByPos (id);
  984. if (node != null && node.PopulateOnDemand && !node.Populated)
  985. node.Populated = true;
  986. }
  987. }
  988. res = true;
  989. }
  990. UnsetCheckStates (Nodes, postCollection);
  991. SetCheckStates (postCollection);
  992. if (EnableClientScript) {
  993. string states = postCollection [ClientID + "_ExpandStates"];
  994. if (states != null) {
  995. string[] ids = states.Split ('|');
  996. UnsetExpandStates (Nodes, ids);
  997. SetExpandStates (ids);
  998. }
  999. else
  1000. UnsetExpandStates (Nodes, new string[0]);
  1001. res = true;
  1002. }
  1003. return res;
  1004. }
  1005. const string _OnPreRender_Script_Preamble =
  1006. "var {0} = new Object ();\n" +
  1007. "{0}.treeId = {1};\n" +
  1008. "{0}.uid = {2};\n" +
  1009. "{0}.showImage = {3};\n";
  1010. const string _OnPreRender_Script_ShowExpandCollapse =
  1011. "{0}.expandImage = {1};\n" +
  1012. "{0}.collapseImage = {2};\n";
  1013. const string _OnPreRender_Script_ShowExpandCollapse_Populate =
  1014. "{0}.noExpandImage = {1};\n";
  1015. const string _OnPreRender_Script_PopulateCallback =
  1016. "{0}.form = {1};\n" +
  1017. "{0}.PopulateNode = function (nodeId, nodeText, nodeValue, nodeImageUrl, nodeNavigateUrl, nodeTarget) {{\n" +
  1018. "\t{2}.__theFormPostData = \"\";\n" +
  1019. "\t{2}.__theFormPostCollection = new Array ();\n" +
  1020. "\t{2}.WebForm_InitCallback ();\n" +
  1021. "\tTreeView_PopulateNode (this.uid, this.treeId, nodeId, nodeText, nodeValue, nodeImageUrl, nodeNavigateUrl, nodeTarget)\n}};\n";
  1022. const string _OnPreRender_Script_CallbackOptions =
  1023. "{0}.populateFromClient = {1};\n" +
  1024. "{0}.expandAlt = {2};\n" +
  1025. "{0}.collapseAlt = {3};\n";
  1026. const string _OnPreRender_Script_HoverStyle =
  1027. "{0}.hoverClass = {1};\n" +
  1028. "{0}.hoverLinkClass = {2};\n";
  1029. protected internal override void OnPreRender (EventArgs e)
  1030. {
  1031. base.OnPreRender (e);
  1032. if (Page != null) {
  1033. if (Enabled)
  1034. Page.RegisterRequiresPostBack (this);
  1035. if (EnableClientScript && !Page.ClientScript.IsClientScriptIncludeRegistered (typeof(TreeView), "TreeView.js")) {
  1036. string url = Page.ClientScript.GetWebResourceUrl (typeof(TreeView), "TreeView.js");
  1037. Page.ClientScript.RegisterClientScriptInclude (typeof(TreeView), "TreeView.js", url);
  1038. }
  1039. }
  1040. string ctree = ClientID + "_data";
  1041. StringBuilder script = new StringBuilder ();
  1042. script.AppendFormat (_OnPreRender_Script_Preamble,
  1043. ctree,
  1044. ClientScriptManager.GetScriptLiteral (ClientID),
  1045. ClientScriptManager.GetScriptLiteral (UniqueID),
  1046. ClientScriptManager.GetScriptLiteral (ShowExpandCollapse));
  1047. if (ShowExpandCollapse) {
  1048. ImageStyle imageStyle = GetImageStyle ();
  1049. script.AppendFormat (_OnPreRender_Script_ShowExpandCollapse,
  1050. ctree,
  1051. ClientScriptManager.GetScriptLiteral (GetNodeImageUrl ("plus", imageStyle)),
  1052. ClientScriptManager.GetScriptLiteral (GetNodeImageUrl ("minus", imageStyle)));
  1053. if (PopulateNodesFromClient)
  1054. script.AppendFormat (_OnPreRender_Script_ShowExpandCollapse_Populate,
  1055. ctree,
  1056. ClientScriptManager.GetScriptLiteral (GetNodeImageUrl ("noexpand", imageStyle)));
  1057. }
  1058. if (Page != null) {
  1059. script.AppendFormat (_OnPreRender_Script_PopulateCallback,
  1060. ctree,
  1061. Page.theForm,
  1062. Page.WebFormScriptReference);
  1063. // Page.ClientScript.GetCallbackEventReference (
  1064. // "this.uid", "nodeId",
  1065. // "TreeView_PopulateCallback",
  1066. // "this.treeId + \" \" + nodeId", "TreeView_PopulateCallback", false));
  1067. script.AppendFormat (_OnPreRender_Script_CallbackOptions,
  1068. ctree,
  1069. ClientScriptManager.GetScriptLiteral (PopulateNodesFromClient),
  1070. ClientScriptManager.GetScriptLiteral (GetNodeImageToolTip (true, null)),
  1071. ClientScriptManager.GetScriptLiteral (GetNodeImageToolTip (false, null)));
  1072. if (!Page.IsPostBack) {
  1073. SetNodesExpandedToDepthRecursive (Nodes);
  1074. }
  1075. if (EnableClientScript) {
  1076. Page.ClientScript.RegisterHiddenField (ClientID + "_ExpandStates", GetExpandStates ());
  1077. // Make sure the basic script infrastructure is rendered
  1078. Page.ClientScript.RegisterWebFormClientScript ();
  1079. }
  1080. if (EnableClientScript && PopulateNodesFromClient) {
  1081. Page.ClientScript.RegisterHiddenField (ClientID + "_PopulatedStates", "|");
  1082. }
  1083. EnsureStylesPrepared ();
  1084. if (hoverNodeStyle != null) {
  1085. if (Page.Header == null)
  1086. throw new InvalidOperationException ("Using TreeView.HoverNodeStyle requires Page.Header to be non-null (e.g. <head runat=\"server\" />).");
  1087. RegisterStyle (HoverNodeStyle, HoverNodeLinkStyle);
  1088. script.AppendFormat (_OnPreRender_Script_HoverStyle,
  1089. ctree,
  1090. ClientScriptManager.GetScriptLiteral (HoverNodeStyle.RegisteredCssClass),
  1091. ClientScriptManager.GetScriptLiteral (HoverNodeLinkStyle.RegisteredCssClass));
  1092. }
  1093. Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script.ToString (), true);
  1094. script = null;
  1095. }
  1096. }
  1097. void EnsureStylesPrepared () {
  1098. if (stylesPrepared)
  1099. return;
  1100. stylesPrepared = true;
  1101. PrepareStyles ();
  1102. }
  1103. void PrepareStyles () {
  1104. // The order in which styles are defined matters when more than one class
  1105. // is assigned to an element
  1106. ControlLinkStyle.CopyTextStylesFrom (ControlStyle);
  1107. RegisterStyle (ControlLinkStyle);
  1108. if (nodeStyle != null)
  1109. RegisterStyle (NodeStyle, NodeLinkStyle);
  1110. if (rootNodeStyle != null)
  1111. RegisterStyle (RootNodeStyle, RootNodeLinkStyle);
  1112. if (parentNodeStyle != null)
  1113. RegisterStyle (ParentNodeStyle, ParentNodeLinkStyle);
  1114. if (leafNodeStyle != null)
  1115. RegisterStyle (LeafNodeStyle, LeafNodeLinkStyle);
  1116. if (levelStyles != null && levelStyles.Count > 0) {
  1117. levelLinkStyles = new List<Style> (levelStyles.Count);
  1118. foreach (Style style in levelStyles) {
  1119. Style linkStyle = new Style ();
  1120. levelLinkStyles.Add (linkStyle);
  1121. RegisterStyle (style, linkStyle);
  1122. }
  1123. }
  1124. if (selectedNodeStyle != null)
  1125. RegisterStyle (SelectedNodeStyle, SelectedNodeLinkStyle);
  1126. }
  1127. void SetNodesExpandedToDepthRecursive (TreeNodeCollection nodes) {
  1128. foreach (TreeNode node in nodes) {
  1129. if (!node.Expanded.HasValue) {
  1130. if (ExpandDepth < 0 || node.Depth < ExpandDepth)
  1131. node.Expanded = true;
  1132. }
  1133. SetNodesExpandedToDepthRecursive (node.ChildNodes);
  1134. }
  1135. }
  1136. string IncrementStyleClassName () {
  1137. registeredStylesCounter++;
  1138. return ClientID + "_" + registeredStylesCounter;
  1139. }
  1140. void RegisterStyle (Style baseStyle, Style linkStyle) {
  1141. linkStyle.CopyTextStylesFrom (baseStyle);
  1142. linkStyle.BorderStyle = BorderStyle.None;
  1143. baseStyle.Font.Reset ();
  1144. RegisterStyle (linkStyle);
  1145. RegisterStyle (baseStyle);
  1146. }
  1147. void RegisterStyle (Style baseStyle) {
  1148. if (Page.Header == null)
  1149. return;
  1150. string className = IncrementStyleClassName ().Trim ('_');
  1151. baseStyle.SetRegisteredCssClass (className);
  1152. Page.Header.StyleSheet.CreateStyleRule (baseStyle, this, "." + className);
  1153. }
  1154. string GetBindingKey (string dataMember, int depth)
  1155. {
  1156. return dataMember + " " + depth;
  1157. }
  1158. void InitializeDataBindings () {
  1159. if (dataBindings != null && dataBindings.Count > 0) {
  1160. bindings = new Hashtable ();
  1161. foreach (TreeNodeBinding bin in dataBindings) {
  1162. string key = GetBindingKey (bin.DataMember, bin.Depth);
  1163. if (!bindings.ContainsKey(key))
  1164. bindings [key] = bin;
  1165. }
  1166. }
  1167. else
  1168. bindings = null;
  1169. }
  1170. internal TreeNodeBinding FindBindingForNode (string type, int depth)
  1171. {
  1172. if (bindings == null)
  1173. return null;
  1174. TreeNodeBinding bin = (TreeNodeBinding) bindings [GetBindingKey (type, depth)];
  1175. if (bin != null) return bin;
  1176. bin = (TreeNodeBinding) bindings [GetBindingKey (type, -1)];
  1177. if (bin != null) return bin;
  1178. bin = (TreeNodeBinding) bindings [GetBindingKey ("", depth)];
  1179. if (bin != null) return bin;
  1180. return (TreeNodeBinding) bindings [GetBindingKey ("", -1)];
  1181. }
  1182. internal void DecorateNode(TreeNode node)
  1183. {
  1184. if (node == null)
  1185. return;
  1186. if (node.ImageUrl != null && node.ImageUrl.Length > 0)
  1187. return;
  1188. if (node.IsRootNode && rootNodeStyle != null) {
  1189. node.ImageUrl = rootNodeStyle.ImageUrl;
  1190. return;
  1191. }
  1192. if (node.IsParentNode && parentNodeStyle != null) {
  1193. node.ImageUrl = parentNodeStyle.ImageUrl;
  1194. return;
  1195. }
  1196. if (node.IsLeafNode && leafNodeStyle != null)
  1197. node.ImageUrl = leafNodeStyle.ImageUrl;
  1198. }
  1199. protected internal override void RenderContents (HtmlTextWriter writer)
  1200. {
  1201. SiteMapDataSource siteMap = GetDataSource () as SiteMapDataSource;
  1202. bool checkSitePath = IsBoundUsingDataSourceID && siteMap != null;
  1203. if (checkSitePath) {
  1204. IHierarchyData data = siteMap.Provider.CurrentNode;
  1205. if (data != null)
  1206. activeSiteMapPath = data.Path;
  1207. }
  1208. ArrayList levelLines = new ArrayList ();
  1209. int num = Nodes.Count;
  1210. for (int n=0; n<num; n++)
  1211. RenderNode (writer, Nodes [n], 0, levelLines, n>0, n<num-1);
  1212. }
  1213. protected override void AddAttributesToRender(HtmlTextWriter writer)
  1214. {
  1215. base.AddAttributesToRender (writer);
  1216. }
  1217. public override void RenderBeginTag (HtmlTextWriter writer)
  1218. {
  1219. if (SkipLinkText != "") {
  1220. writer.AddAttribute (HtmlTextWriterAttribute.Href, "#" + ClientID + "_SkipLink");
  1221. writer.RenderBeginTag (HtmlTextWriterTag.A);
  1222. Image img = new Image ();
  1223. ClientScriptManager csm = new ClientScriptManager (null);
  1224. img.ImageUrl = csm.GetWebResourceUrl (typeof (SiteMapPath), "transparent.gif");
  1225. img.Attributes.Add ("height", "0");
  1226. img.Attributes.Add ("width", "0");
  1227. img.AlternateText = SkipLinkText;
  1228. img.Render (writer);
  1229. writer.RenderEndTag ();
  1230. }
  1231. base.RenderBeginTag (writer);
  1232. }
  1233. public override void RenderEndTag (HtmlTextWriter writer)
  1234. {
  1235. base.RenderEndTag (writer);
  1236. if (SkipLinkText != "") {
  1237. writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID + "_SkipLink");
  1238. writer.RenderBeginTag (HtmlTextWriterTag.A);
  1239. writer.RenderEndTag ();
  1240. }
  1241. }
  1242. void RenderNode (HtmlTextWriter writer, TreeNode node, int level, ArrayList levelLines, bool hasPrevious, bool hasNext)
  1243. {
  1244. DecorateNode(node);
  1245. string nodeImage;
  1246. bool clientExpand = EnableClientScript && Events [TreeNodeCollapsedEvent] == null && Events [TreeNodeExpandedEvent] == null;
  1247. ImageStyle imageStyle = GetImageStyle ();
  1248. bool renderChildNodes = node.Expanded.HasValue && node.Expanded.Value;
  1249. if (clientExpand && !renderChildNodes)
  1250. renderChildNodes = (!node.PopulateOnDemand || node.Populated);
  1251. bool hasChildNodes;
  1252. if (renderChildNodes)
  1253. hasChildNodes = node.ChildNodes.Count > 0;
  1254. else
  1255. hasChildNodes = (node.PopulateOnDemand && !node.Populated) || node.ChildNodes.Count > 0;
  1256. writer.AddAttribute ("cellpadding", "0", false);
  1257. writer.AddAttribute ("cellspacing", "0", false);
  1258. writer.AddStyleAttribute ("border-width", "0");
  1259. writer.RenderBeginTag (HtmlTextWriterTag.Table);
  1260. Unit nodeSpacing = GetNodeSpacing (node);
  1261. if (nodeSpacing != Unit.Empty && (node.Depth > 0 || node.Index > 0))
  1262. RenderMenuItemSpacing (writer, nodeSpacing);
  1263. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  1264. // Vertical lines from previous levels
  1265. nodeImage = GetNodeImageUrl ("i", imageStyle);
  1266. for (int n=0; n<level; n++) {
  1267. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  1268. writer.AddStyleAttribute ("width", NodeIndent + "px");
  1269. writer.AddStyleAttribute ("height", "1px");
  1270. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  1271. if (ShowLines && levelLines [n] != null) {
  1272. writer.AddAttribute ("src", nodeImage);
  1273. writer.AddAttribute (HtmlTextWriterAttribute.Alt, "", false);
  1274. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  1275. writer.RenderEndTag ();
  1276. }
  1277. writer.RenderEndTag ();
  1278. writer.RenderEndTag (); // TD
  1279. }
  1280. // Node image + line
  1281. if (ShowExpandCollapse || ShowLines) {
  1282. bool buttonImage = false;
  1283. string tooltip = "";
  1284. string shape = "";
  1285. if (ShowLines) {
  1286. if (hasPrevious && hasNext) shape = "t";
  1287. else if (hasPrevious && !hasNext) shape = "l";
  1288. else if (!hasPrevious && hasNext) shape = "r";
  1289. else shape = "dash";
  1290. }
  1291. if (ShowExpandCollapse) {
  1292. if (hasChildNodes) {
  1293. buttonImage = true;
  1294. if (node.Expanded.HasValue && node.Expanded.Value) shape += "minus";
  1295. else shape += "plus";
  1296. tooltip = GetNodeImageToolTip (!(node.Expanded.HasValue && node.Expanded.Value), node.Text);
  1297. } else if (!ShowLines)
  1298. shape = "noexpand";
  1299. }
  1300. if (shape != "") {
  1301. nodeImage = GetNodeImageUrl (shape, imageStyle);
  1302. writer.RenderBeginTag (HtmlTextWriterTag.Td); // TD
  1303. if (buttonImage) {
  1304. if (!clientExpand || (!PopulateNodesFromClient && node.PopulateOnDemand && !node.Populated))
  1305. writer.AddAttribute ("href", GetClientEvent (node, "ec"));
  1306. else
  1307. writer.AddAttribute ("href", GetClientExpandEvent(node));
  1308. writer.RenderBeginTag (HtmlTextWriterTag.A); // Anchor
  1309. }
  1310. // tooltip is 'HtmlAttributeEncoded'
  1311. writer.AddAttribute ("alt", tooltip);
  1312. if (buttonImage && clientExpand)
  1313. writer.AddAttribute ("id", GetNodeClientId (node, "img"));
  1314. writer.AddAttribute ("src", nodeImage);
  1315. if (buttonImage)
  1316. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0");
  1317. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  1318. writer.RenderEndTag ();
  1319. if (buttonImage)
  1320. writer.RenderEndTag (); // Anchor
  1321. writer.RenderEndTag (); // TD
  1322. }
  1323. }
  1324. // Node icon
  1325. string imageUrl = node.ImageUrl.Length > 0 ? ResolveClientUrl (node.ImageUrl) : null;
  1326. if (String.IsNullOrEmpty (imageUrl) && imageStyle != null) {
  1327. if (imageStyle.RootIcon != null && node.IsRootNode)
  1328. imageUrl = GetNodeIconUrl (imageStyle.RootIcon);
  1329. else if (imageStyle.ParentIcon != null && node.IsParentNode)
  1330. imageUrl = GetNodeIconUrl (imageStyle.ParentIcon);
  1331. else if (imageStyle.LeafIcon != null && node.IsLeafNode)
  1332. imageUrl = GetNodeIconUrl (imageStyle.LeafIcon);
  1333. }
  1334. if (level < LevelStyles.Count && LevelStyles [level].ImageUrl != null)
  1335. imageUrl = ResolveClientUrl (LevelStyles [level].ImageUrl);
  1336. if (!String.IsNullOrEmpty (imageUrl)) {
  1337. writer.RenderBeginTag (HtmlTextWriterTag.Td); // TD
  1338. BeginNodeTag (writer, node, clientExpand);
  1339. writer.AddAttribute ("src", imageUrl);
  1340. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0");
  1341. writer.AddAttribute ("alt", node.ImageToolTip);
  1342. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  1343. writer.RenderEndTag (); // IMG
  1344. writer.RenderEndTag (); // style tag
  1345. writer.RenderEndTag (); // TD
  1346. }
  1347. if (!NodeWrap)
  1348. writer.AddStyleAttribute ("white-space", "nowrap");
  1349. bool nodeIsSelected = node == SelectedNode && selectedNodeStyle != null;
  1350. if (!nodeIsSelected && selectedNodeStyle != null) {
  1351. if (!String.IsNullOrEmpty (activeSiteMapPath))
  1352. nodeIsSelected = String.Compare (activeSiteMapPath,
  1353. node.NavigateUrl,
  1354. HttpRuntime.CaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0;
  1355. }
  1356. AddNodeStyle (writer, node, level, nodeIsSelected);
  1357. if (EnableClientScript) {
  1358. writer.AddAttribute ("onmouseout", "TreeView_UnhoverNode(this)", false);
  1359. writer.AddAttribute ("onmouseover", "TreeView_HoverNode('" + ClientID + "', this)");
  1360. }
  1361. writer.RenderBeginTag (HtmlTextWriterTag.Td); // TD
  1362. // Checkbox
  1363. if (node.ShowCheckBoxInternal) {
  1364. writer.AddAttribute ("name", ClientID + "_cs_" + node.Path);
  1365. writer.AddAttribute ("type", "checkbox", false);
  1366. writer.AddAttribute ("title", node.Text);
  1367. if (node.Checked) writer.AddAttribute ("checked", "checked", false);
  1368. writer.RenderBeginTag (HtmlTextWriterTag.Input); // INPUT
  1369. writer.RenderEndTag (); // INPUT
  1370. }
  1371. // Text
  1372. node.BeginRenderText (writer);
  1373. if (clientExpand)
  1374. writer.AddAttribute ("id", GetNodeClientId (node, "txt"));
  1375. AddNodeLinkStyle (writer, node, level, nodeIsSelected);
  1376. BeginNodeTag (writer, node, clientExpand);
  1377. writer.Write (node.Text);
  1378. writer.RenderEndTag (); // style tag
  1379. node.EndRenderText (writer);
  1380. writer.RenderEndTag (); // TD
  1381. writer.RenderEndTag (); // TR
  1382. if (nodeSpacing != Unit.Empty)
  1383. RenderMenuItemSpacing (writer, nodeSpacing);
  1384. writer.RenderEndTag (); // TABLE
  1385. // Children
  1386. if (hasChildNodes) {
  1387. if (level >= levelLines.Count) {
  1388. if (hasNext)
  1389. levelLines.Add (this);
  1390. else
  1391. levelLines.Add (null);
  1392. } else {
  1393. if (hasNext)
  1394. levelLines [level] = this;
  1395. else
  1396. levelLines [level] = null;
  1397. }
  1398. if (clientExpand) {
  1399. if (!(node.Expanded.HasValue && node.Expanded.Value))
  1400. writer.AddStyleAttribute ("display", "none");
  1401. else
  1402. writer.AddStyleAttribute ("display", "block");
  1403. writer.AddAttribute ("id", GetNodeClientId (node, null));
  1404. writer.RenderBeginTag (HtmlTextWriterTag.Span);
  1405. if (renderChildNodes) {
  1406. AddChildrenPadding (writer, node);
  1407. int num = node.ChildNodes.Count;
  1408. for (int n=0; n<num; n++)
  1409. RenderNode (writer, node.ChildNodes [n], level + 1, levelLines, true, n<num-1);
  1410. if (hasNext)
  1411. AddChildrenPadding (writer, node);
  1412. }
  1413. writer.RenderEndTag (); // SPAN
  1414. } else if (renderChildNodes) {
  1415. AddChildrenPadding (writer, node);
  1416. int num = node.ChildNodes.Count;
  1417. for (int n=0; n<num; n++)
  1418. RenderNode (writer, node.ChildNodes [n], level + 1, levelLines, true, n<num-1);
  1419. if (hasNext)
  1420. AddChildrenPadding (writer, node);
  1421. }
  1422. }
  1423. }
  1424. void AddChildrenPadding (HtmlTextWriter writer, TreeNode node)
  1425. {
  1426. int level = node.Depth;
  1427. Unit cnp = Unit.Empty;
  1428. if (levelStyles != null && level < levelStyles.Count)
  1429. cnp = levelStyles [level].ChildNodesPadding;
  1430. if (cnp.IsEmpty && nodeStyle != null)
  1431. cnp = nodeStyle.ChildNodesPadding;
  1432. double value;
  1433. if (cnp.IsEmpty || (value = cnp.Value) == 0 || cnp.Type != UnitType.Pixel)
  1434. return;
  1435. writer.RenderBeginTag (HtmlTextWriterTag.Table);
  1436. writer.AddAttribute ("height", ((int) value).ToString (), false);
  1437. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  1438. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  1439. writer.RenderEndTag (); // td
  1440. writer.RenderEndTag (); // tr
  1441. writer.RenderEndTag (); // table
  1442. }
  1443. void RenderMenuItemSpacing (HtmlTextWriter writer, Unit itemSpacing) {
  1444. writer.AddStyleAttribute ("height", itemSpacing.ToString ());
  1445. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  1446. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  1447. writer.RenderEndTag ();
  1448. writer.RenderEndTag ();
  1449. }
  1450. Unit GetNodeSpacing (TreeNode node) {
  1451. if (node.Selected && selectedNodeStyle != null && selectedNodeStyle.NodeSpacing != Unit.Empty) {
  1452. return selectedNodeStyle.NodeSpacing;
  1453. }
  1454. if (levelStyles != null && node.Depth < levelStyles.Count && levelStyles [node.Depth].NodeSpacing != Unit.Empty) {
  1455. return levelStyles [node.Depth].NodeSpacing;
  1456. }
  1457. if (node.IsLeafNode) {
  1458. if (leafNodeStyle != null && leafNodeStyle.NodeSpacing != Unit.Empty)
  1459. return leafNodeStyle.NodeSpacing;
  1460. }
  1461. else if (node.IsRootNode) {
  1462. if (rootNodeStyle != null && rootNodeStyle.NodeSpacing != Unit.Empty)
  1463. return rootNodeStyle.NodeSpacing;
  1464. }
  1465. else if (node.IsParentNode) {
  1466. if (parentNodeStyle != null && parentNodeStyle.NodeSpacing != Unit.Empty)
  1467. return parentNodeStyle.NodeSpacing;
  1468. }
  1469. if (nodeStyle != null)
  1470. return nodeStyle.NodeSpacing;
  1471. else
  1472. return Unit.Empty;
  1473. }
  1474. void AddNodeStyle (HtmlTextWriter writer, TreeNode node, int level, bool nodeIsSelected)
  1475. {
  1476. TreeNodeStyle style = new TreeNodeStyle ();
  1477. if (Page.Header != null) {
  1478. // styles are registered
  1479. if (nodeStyle != null) {
  1480. style.AddCssClass (nodeStyle.CssClass);
  1481. style.AddCssClass (nodeStyle.RegisteredCssClass);
  1482. }
  1483. if (node.IsLeafNode) {
  1484. if (leafNodeStyle != null) {
  1485. style.AddCssClass (leafNodeStyle.CssClass);
  1486. style.AddCssClass (leafNodeStyle.RegisteredCssClass);
  1487. }
  1488. } else if (node.IsRootNode) {
  1489. if (rootNodeStyle != null) {
  1490. style.AddCssClass (rootNodeStyle.CssClass);
  1491. style.AddCssClass (rootNodeStyle.RegisteredCssClass);
  1492. }
  1493. } else if (node.IsParentNode) {
  1494. if (parentNodeStyle != null) {
  1495. style.AddCssClass (parentNodeStyle.CssClass);
  1496. style.AddCssClass (parentNodeStyle.RegisteredCssClass);
  1497. }
  1498. }
  1499. if (levelStyles != null && levelStyles.Count > level) {
  1500. style.AddCssClass (levelStyles [level].CssClass);
  1501. style.AddCssClass (levelStyles [level].RegisteredCssClass);
  1502. }
  1503. if (nodeIsSelected) {
  1504. style.AddCssClass (selectedNodeStyle.CssClass);
  1505. style.AddCssClass (selectedNodeStyle.RegisteredCssClass);
  1506. }
  1507. } else {
  1508. // styles are not registered
  1509. if (nodeStyle != null) {
  1510. style.CopyFrom (nodeStyle);
  1511. }
  1512. if (node.IsLeafNode) {
  1513. if (leafNodeStyle != null) {
  1514. style.CopyFrom (leafNodeStyle);
  1515. }
  1516. }
  1517. else if (node.IsRootNode) {
  1518. if (rootNodeStyle != null) {
  1519. style.CopyFrom (rootNodeStyle);
  1520. }
  1521. }
  1522. else if (node.IsParentNode) {
  1523. if (parentNodeStyle != null) {
  1524. style.CopyFrom (parentNodeStyle);
  1525. }
  1526. }
  1527. if (levelStyles != null && levelStyles.Count > level) {
  1528. style.CopyFrom (levelStyles [level]);
  1529. }
  1530. if (nodeIsSelected) {
  1531. style.CopyFrom (selectedNodeStyle);
  1532. }
  1533. }
  1534. style.AddAttributesToRender (writer);
  1535. }
  1536. void AddNodeLinkStyle (HtmlTextWriter writer, TreeNode node, int level, bool nodeIsSelected)
  1537. {
  1538. Style style = new Style ();
  1539. if (Page.Header != null) {
  1540. // styles are registered
  1541. style.AddCssClass (ControlLinkStyle.RegisteredCssClass);
  1542. if (nodeStyle != null) {
  1543. style.AddCssClass (nodeLinkStyle.CssClass);
  1544. style.AddCssClass (nodeLinkStyle.RegisteredCssClass);
  1545. }
  1546. if (node.IsLeafNode) {
  1547. if (leafNodeStyle != null) {
  1548. style.AddCssClass (leafNodeLinkStyle.CssClass);
  1549. style.AddCssClass (leafNodeLinkStyle.RegisteredCssClass);
  1550. }
  1551. }
  1552. else if (node.IsRootNode) {
  1553. if (rootNodeStyle != null) {
  1554. style.AddCssClass (rootNodeLinkStyle.CssClass);
  1555. style.AddCssClass (rootNodeLinkStyle.RegisteredCssClass);
  1556. }
  1557. }
  1558. else if (node.IsParentNode) {
  1559. if (parentNodeStyle != null) {
  1560. style.AddCssClass (parentNodeLinkStyle.CssClass);
  1561. style.AddCssClass (parentNodeLinkStyle.RegisteredCssClass);
  1562. }
  1563. }
  1564. if (levelStyles != null && levelStyles.Count > level) {
  1565. style.AddCssClass (levelLinkStyles [level].CssClass);
  1566. style.AddCssClass (levelLinkStyles [level].RegisteredCssClass);
  1567. }
  1568. if (nodeIsSelected) {
  1569. style.AddCssClass (selectedNodeLinkStyle.CssClass);
  1570. style.AddCssClass (selectedNodeLinkStyle.RegisteredCssClass);
  1571. }
  1572. }
  1573. else {
  1574. // styles are not registered
  1575. style.CopyFrom (ControlLinkStyle);
  1576. if (nodeStyle != null) {
  1577. style.CopyFrom (nodeLinkStyle);
  1578. }
  1579. if (node.IsLeafNode) {
  1580. if (node.IsLeafNode && leafNodeStyle != null) {
  1581. style.CopyFrom (leafNodeLinkStyle);
  1582. }
  1583. }
  1584. else if (node.IsRootNode) {
  1585. if (node.IsRootNode && rootNodeStyle != null) {
  1586. style.CopyFrom (rootNodeLinkStyle);
  1587. }
  1588. }
  1589. else if (node.IsParentNode) {
  1590. if (node.IsParentNode && parentNodeStyle != null) {
  1591. style.CopyFrom (parentNodeLinkStyle);
  1592. }
  1593. }
  1594. if (levelStyles != null && levelStyles.Count > level) {
  1595. style.CopyFrom (levelLinkStyles [level]);
  1596. }
  1597. if (nodeIsSelected) {
  1598. style.CopyFrom (selectedNodeLinkStyle);
  1599. }
  1600. style.AlwaysRenderTextDecoration = true;
  1601. }
  1602. style.AddAttributesToRender (writer);
  1603. }
  1604. void BeginNodeTag (HtmlTextWriter writer, TreeNode node, bool clientExpand)
  1605. {
  1606. if(node.ToolTip.Length>0)
  1607. writer.AddAttribute ("title", node.ToolTip);
  1608. if (node.NavigateUrl != "") {
  1609. string target = node.Target.Length > 0 ? node.Target : Target;
  1610. #if TARGET_J2EE
  1611. string navUrl = ResolveClientUrl (node.NavigateUrl, String.Compare (target, "_blank", StringComparison.InvariantCultureIgnoreCase) != 0);
  1612. #else
  1613. string navUrl = ResolveClientUrl (node.NavigateUrl);
  1614. #endif
  1615. writer.AddAttribute ("href", navUrl);
  1616. if (target.Length > 0)
  1617. writer.AddAttribute ("target", target);
  1618. writer.RenderBeginTag (HtmlTextWriterTag.A);
  1619. }
  1620. else if (node.SelectAction != TreeNodeSelectAction.None) {
  1621. if (node.SelectAction == TreeNodeSelectAction.Expand && clientExpand)
  1622. writer.AddAttribute ("href", GetClientExpandEvent (node));
  1623. else
  1624. writer.AddAttribute ("href", GetClientEvent (node, "sel"));
  1625. writer.RenderBeginTag (HtmlTextWriterTag.A);
  1626. }
  1627. else
  1628. writer.RenderBeginTag (HtmlTextWriterTag.Span);
  1629. }
  1630. string GetNodeImageToolTip (bool expand, string txt) {
  1631. if (expand) {
  1632. if (ExpandImageToolTip != "")
  1633. return String.Format (ExpandImageToolTip, HttpUtility.HtmlAttributeEncode (txt));
  1634. else if (txt != null)
  1635. return "Expand " + HttpUtility.HtmlAttributeEncode (txt);
  1636. else
  1637. return "Expand {0}";
  1638. } else {
  1639. if (CollapseImageToolTip != "")
  1640. return String.Format (CollapseImageToolTip, HttpUtility.HtmlAttributeEncode (txt));
  1641. else if (txt != null)
  1642. return "Collapse " + HttpUtility.HtmlAttributeEncode (txt);
  1643. else
  1644. return "Collapse {0}";
  1645. }
  1646. }
  1647. string GetNodeClientId (TreeNode node, string sufix)
  1648. {
  1649. return ClientID + "_" + node.Path + (sufix != null ? "_" + sufix : "");
  1650. }
  1651. string GetNodeImageUrl (string shape, ImageStyle imageStyle)
  1652. {
  1653. if (ShowLines) {
  1654. if (!String.IsNullOrEmpty (LineImagesFolder))
  1655. return ResolveClientUrl (LineImagesFolder + "/" + shape + ".gif");
  1656. } else {
  1657. if (imageStyle != null) {
  1658. if (shape == "plus") {
  1659. if (!String.IsNullOrEmpty (imageStyle.Expand))
  1660. return GetNodeIconUrl (imageStyle.Expand);
  1661. }
  1662. else if (shape == "minus") {
  1663. if (!String.IsNullOrEmpty (imageStyle.Collapse))
  1664. return GetNodeIconUrl (imageStyle.Collapse);
  1665. }
  1666. else if (shape == "noexpand") {
  1667. if (!String.IsNullOrEmpty (imageStyle.NoExpand))
  1668. return GetNodeIconUrl (imageStyle.NoExpand);
  1669. }
  1670. }
  1671. else {
  1672. if (shape == "plus") {
  1673. if (!String.IsNullOrEmpty (ExpandImageUrl))
  1674. return ResolveClientUrl (ExpandImageUrl);
  1675. }
  1676. else if (shape == "minus") {
  1677. if (!String.IsNullOrEmpty (CollapseImageUrl))
  1678. return ResolveClientUrl (CollapseImageUrl);
  1679. }
  1680. else if (shape == "noexpand") {
  1681. if (!String.IsNullOrEmpty (NoExpandImageUrl))
  1682. return ResolveClientUrl (NoExpandImageUrl);
  1683. }
  1684. }
  1685. if (!String.IsNullOrEmpty (LineImagesFolder))
  1686. return ResolveClientUrl (LineImagesFolder + "/" + shape + ".gif");
  1687. }
  1688. return Page.ClientScript.GetWebResourceUrl (typeof (TreeView), "TreeView_" + shape + ".gif");
  1689. }
  1690. string GetNodeIconUrl (string icon)
  1691. {
  1692. return Page.ClientScript.GetWebResourceUrl (typeof (TreeView), icon + ".gif");
  1693. }
  1694. string GetClientEvent (TreeNode node, string ev)
  1695. {
  1696. return Page.ClientScript.GetPostBackClientHyperlink (this, ev + "|" + node.Path, true);
  1697. }
  1698. string GetClientExpandEvent (TreeNode node)
  1699. {
  1700. return String.Format ("javascript:TreeView_ToggleExpand ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
  1701. ClientID,
  1702. node.Path,
  1703. "", //HttpUtility.HtmlEncode (node.Text).Replace ("'", "\\'"),
  1704. HttpUtility.HtmlAttributeEncode (node.Value).Replace ("'", "\\'"),
  1705. HttpUtility.HtmlAttributeEncode (node.ImageUrl).Replace ("'", "\\'"),
  1706. HttpUtility.HtmlAttributeEncode (node.NavigateUrl).Replace ("'", "\\'"),
  1707. HttpUtility.HtmlAttributeEncode (node.Target).Replace ("'", "\\'"));
  1708. }
  1709. TreeNode FindNodeByPos (string path)
  1710. {
  1711. string[] indexes = path.Split ('_');
  1712. TreeNode node = null;
  1713. foreach (string index in indexes) {
  1714. int i = int.Parse (index);
  1715. if (node == null) {
  1716. if (i >= Nodes.Count) return null;
  1717. node = Nodes [i];
  1718. } else {
  1719. if (i >= node.ChildNodes.Count) return null;
  1720. node = node.ChildNodes [i];
  1721. }
  1722. }
  1723. return node;
  1724. }
  1725. void UnsetCheckStates (TreeNodeCollection col, NameValueCollection states)
  1726. {
  1727. foreach (TreeNode node in col) {
  1728. if (node.ShowCheckBoxInternal && node.Checked) {
  1729. if (states == null || states [ClientID + "_cs_" + node.Path] == null)
  1730. node.Checked = false;
  1731. }
  1732. if (node.HasChildData)
  1733. UnsetCheckStates (node.ChildNodes, states);
  1734. }
  1735. }
  1736. void SetCheckStates (NameValueCollection states)
  1737. {
  1738. if (states == null)
  1739. return;
  1740. string keyPrefix = ClientID + "_cs_";
  1741. foreach (string key in states) {
  1742. if (key.StartsWith (keyPrefix, StringComparison.Ordinal)) {
  1743. string id = key.Substring (keyPrefix.Length);
  1744. TreeNode node = FindNodeByPos (id);
  1745. if (node != null && !node.Checked)
  1746. node.Checked = true;
  1747. }
  1748. }
  1749. }
  1750. void UnsetExpandStates (TreeNodeCollection col, string[] states)
  1751. {
  1752. foreach (TreeNode node in col) {
  1753. if (node.Expanded.HasValue && node.Expanded.Value) {
  1754. bool expand = (Array.IndexOf (states, node.Path) != -1);
  1755. if (!expand) node.Expanded = false;
  1756. }
  1757. if (node.HasChildData)
  1758. UnsetExpandStates (node.ChildNodes, states);
  1759. }
  1760. }
  1761. void SetExpandStates (string[] states)
  1762. {
  1763. foreach (string id in states) {
  1764. if (id == null || id == "") continue;
  1765. TreeNode node = FindNodeByPos (id);
  1766. if (node != null)
  1767. node.Expanded = true;
  1768. }
  1769. }
  1770. string GetExpandStates ()
  1771. {
  1772. StringBuilder sb = new StringBuilder ("|");
  1773. foreach (TreeNode node in Nodes)
  1774. GetExpandStates (sb, node);
  1775. return sb.ToString ();
  1776. }
  1777. void GetExpandStates (StringBuilder sb, TreeNode node)
  1778. {
  1779. if (node.Expanded.HasValue && node.Expanded.Value) {
  1780. sb.Append (node.Path);
  1781. sb.Append ('|');
  1782. }
  1783. if (node.HasChildData) {
  1784. foreach (TreeNode child in node.ChildNodes)
  1785. GetExpandStates (sb, child);
  1786. }
  1787. }
  1788. }
  1789. }
  1790. #endif