Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

529 lines
13 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. using UDDI.Diagnostics;
  7. namespace UDDI.Web
  8. {
  9. public class TreeView : UserControl
  10. {
  11. protected string binhex = @"!""#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr";
  12. protected string clientScript = @"
  13. <script language='javascript'>
  14. <!--
  15. var binhex = ""{binhex}"";
  16. function ToggleBranch( id, block, expanderImage, nodeImage, plus, minus, imageUrl, openImageUrl )
  17. {
  18. if( null == block.style )
  19. return;
  20. if( ""none"" == block.style.display )
  21. {
  22. block.style.display = """";
  23. expanderImage.src = minus;
  24. nodeImage.src = openImageUrl;
  25. }
  26. else
  27. {
  28. block.style.display = ""none"";
  29. expanderImage.src = plus;
  30. nodeImage.src = imageUrl;
  31. }
  32. var node = document.getElementById( ""{id}:state"" );
  33. var state = node.value;
  34. var index = Math.floor( id / 6 );
  35. var bit = 1 << ( id % 6 );
  36. while( index >= state.length )
  37. state += binhex.charAt( 0 );
  38. var digit = binhex.indexOf( state.charAt( index ) ) ^ bit;
  39. node.value =
  40. state.substring( 0, index ) +
  41. binhex.charAt( digit ) +
  42. state.substring( index + 1 );
  43. }
  44. //-->
  45. </script>";
  46. protected string root;
  47. protected TreeNodeCollection nodes;
  48. protected TreeNode selectedNode = null;
  49. protected int count;
  50. protected string selectedID = null;
  51. protected override void OnInit( EventArgs e )
  52. {
  53. root = ( "/" == Request.ApplicationPath ) ? "" : Request.ApplicationPath;
  54. nodes = new TreeNodeCollection( this );
  55. if( !Page.IsClientScriptBlockRegistered( "UDDI.Web.TreeView" ) )
  56. {
  57. clientScript = clientScript.Replace( "{root}", root );
  58. clientScript = clientScript.Replace( "{id}", this.UniqueID );
  59. clientScript = clientScript.Replace( "{binhex}", binhex.Replace( "\"", "\\\"" ) );
  60. Page.RegisterClientScriptBlock(
  61. "UDDI.Web.TreeView",
  62. clientScript );
  63. }
  64. }
  65. public TreeNodeCollection Nodes
  66. {
  67. get { return nodes; }
  68. }
  69. public TreeNode SelectedNode
  70. {
  71. get { return selectedNode; }
  72. set { selectedNode = value; }
  73. }
  74. protected void LoadTreeStateForNode( TreeNode node, string viewState )
  75. {
  76. int index = count / 6;
  77. int bit = 1 << ( count % 6 );
  78. bool expanded = false;
  79. if( index < viewState.Length )
  80. expanded = ( binhex.IndexOf( viewState[ index ] ) & bit ) > 0;
  81. if( expanded )
  82. node.Expand();
  83. count ++;
  84. foreach( TreeNode child in node.Nodes )
  85. LoadTreeStateForNode( child, viewState );
  86. }
  87. protected void SaveTreeStateForNode( TreeNode node, ref string viewState )
  88. {
  89. if( node.IsExpanded )
  90. {
  91. int index = count / 6;
  92. int bit = 1 << ( count % 6 );
  93. while( index >= viewState.Length )
  94. viewState += binhex[ 0 ];
  95. viewState =
  96. viewState.Substring( 0, index ) +
  97. binhex[ binhex.IndexOf( viewState[ index ] ) | bit ] +
  98. viewState.Substring( index + 1 );
  99. }
  100. count ++;
  101. foreach( TreeNode child in node.Nodes )
  102. SaveTreeStateForNode( child, ref viewState );
  103. }
  104. protected override void Render( HtmlTextWriter output )
  105. {
  106. string treeState = Request[ this.UniqueID + ":state" ];
  107. if( null != treeState )
  108. {
  109. count = 0;
  110. foreach( TreeNode child in nodes )
  111. LoadTreeStateForNode( child, treeState );
  112. }
  113. if( null != selectedNode )
  114. selectedNode.EnsureVisible();
  115. treeState = "";
  116. count = 0;
  117. foreach( TreeNode child in nodes )
  118. SaveTreeStateForNode( child, ref treeState );
  119. count = 0;
  120. foreach( TreeNode node in Nodes )
  121. RenderNode( output, node );
  122. output.WriteLine( "<script language='javascript'>" );
  123. if( null != selectedID )
  124. output.WriteLine( " var selectedNode = document.getElementById( \"" + selectedID + "\" );" );
  125. else
  126. output.WriteLine( " var selectedNode = null;" );
  127. output.WriteLine( "</script>" );
  128. output.WriteLine( "<input type='hidden' name='" + this.UniqueID + ":state' value='" + treeState + "'>" );
  129. }
  130. protected void RenderNode( HtmlTextWriter output, TreeNode node )
  131. {
  132. string root = ( "/" == Request.ApplicationPath ) ? "" : Request.ApplicationPath;
  133. string text = HttpUtility.HtmlEncode( node.Text );
  134. string image;
  135. string id = this.UniqueID + ":" + count;
  136. string oncontextmenu = null;
  137. string onclick = null;
  138. if( null != node.OnContextMenu )
  139. oncontextmenu = " oncontextmenu='" + node.OnContextMenu.Replace( "'", "\"" ).Replace( "[[node]]", "document.getElementById( \"" + id + "\" )" ) + "'";
  140. if( null != node.OnClick )
  141. onclick = " onclick='" + node.OnClick.Replace( "'", "\"" ).Replace( "[[node]]", "document.getElementById( \"" + id + "\" )" ) + "'";
  142. if( node.IsSelected )
  143. output.Write( "<a name='#top'>" );
  144. output.Write( "<nobr>" );
  145. //
  146. // Generate ancestor lines.
  147. //
  148. string lines = "";
  149. TreeNode ancestor = node.Parent;
  150. while( null != ancestor )
  151. {
  152. image = "line-ns.gif";
  153. if( null == ancestor.Parent || ancestor.Index >= ancestor.Parent.Nodes.Count - 1 )
  154. image = "blank.gif";
  155. lines = "<img src='" + root + "/images/" + image + "' border='0' width='16' height='18' align='absmiddle'>" + lines;
  156. ancestor = ancestor.Parent;
  157. }
  158. output.Write( lines );
  159. //
  160. // Generate expand/collapse image.
  161. //
  162. bool north = false;
  163. bool south = false;
  164. if( null == node.Parent )
  165. {
  166. if( node.Index > 0 )
  167. north = true;
  168. if( node.Index < node.TreeView.Nodes.Count - 1 )
  169. south = true;
  170. }
  171. else
  172. {
  173. north = true;
  174. if( node.Index < node.Parent.Nodes.Count - 1 )
  175. south = true;
  176. }
  177. //
  178. // Determine which expander image we should use.
  179. //
  180. string dir = ( north ? "n" : "" ) + "e" + ( south ? "s" : "" );
  181. if( node.Nodes.Count > 0 )
  182. {
  183. if( node.IsExpanded )
  184. image = "minus-";
  185. else
  186. image = "plus-";
  187. output.Write( "<img id='_expander" + count + "' src='" + root + "/images/" + image + dir + ".gif' border='0' width='16' height='18' align='absmiddle' onclick='" );
  188. output.Write( "ToggleBranch( " + count + ", _branch" + count + ", _expander" + count + ", _nodeImage" + count + ", \"../images/plus-" + dir + ".gif\", \"../images/minus-" + dir + ".gif\", \"" + node.ImageUrl + "\", \"" + ( null == node.OpenImageUrl ? node.ImageUrl : node.OpenImageUrl ) + "\")'>" );
  189. }
  190. else
  191. output.Write( "<img src='" + root + "/images/line-" + dir + ".gif' border='0' width='16' height='18' align='absmiddle'>" );
  192. //
  193. // Display the node image (or a blank image if none was specified).
  194. //
  195. if( null != node.ImageUrl )
  196. {
  197. output.Write( "<img id='_nodeImage" + count + "' src='" );
  198. if( node.IsExpanded && null != node.OpenImageUrl )
  199. output.Write( node.OpenImageUrl );
  200. else
  201. output.Write( node.ImageUrl );
  202. output.Write( "' border='0' width='16' height='16' align='absmiddle'" );
  203. output.Write( " title='" + node.Tooltip.Replace( "'", "\'" ) + "'" );
  204. output.Write( oncontextmenu );
  205. output.Write( onclick );
  206. output.Write( ">" );
  207. }
  208. else
  209. output.Write( "<img id='_nodeImage" + count + "' src='" + root + "/images/blank.gif' border='0' width='16' height='16' align='absmiddle'>" );
  210. //
  211. // Display the node text.
  212. //
  213. output.Write( "<img src='" + root + "/images/spacer.gif' border='0' width='4' height='16' align='absmiddle'>" );
  214. output.Write( "<span id='" + id + "' key='" + node.Key + "'" );
  215. if( node.IsSelected )
  216. {
  217. output.Write( " class='selected' " );
  218. selectedID = id;
  219. }
  220. else
  221. output.Write( " class='node'" );
  222. output.Write( " title='" + node.Tooltip.Replace( "'", "\'" ) + "'" );
  223. output.Write( oncontextmenu );
  224. output.Write( onclick );
  225. output.Write( ">" );
  226. output.Write( text + "</span></nobr><br><div id='_branch" + count + "' style='display: " + ( node.IsExpanded ? "\"\"" : "\"none\"" ) + "'>\n" );
  227. count ++;
  228. //
  229. // Render this node's children.
  230. //
  231. foreach( TreeNode child in node.Nodes )
  232. RenderNode( output, child );
  233. output.WriteLine( "</div>" );
  234. }
  235. }
  236. /// ********************************************************************
  237. /// public class TreeNode
  238. /// --------------------------------------------------------------------
  239. /// <summary>
  240. /// </summary>
  241. /// ********************************************************************
  242. ///
  243. public class TreeNode
  244. {
  245. internal protected TreeNode parent;
  246. internal protected TreeView treeView;
  247. internal protected TreeNodeCollection nodes;
  248. protected bool expanded = false;
  249. public string Text;
  250. public string Key;
  251. public string ImageUrl;
  252. public string OpenImageUrl;
  253. public string OnClick;
  254. public string OnContextMenu;
  255. public string Tooltip;
  256. public TreeNode()
  257. : this( null, null, null, null )
  258. {
  259. }
  260. public TreeNode( string text )
  261. : this( text, null, null, null )
  262. {
  263. }
  264. public TreeNode( string text, string key )
  265. : this( text, key, null, null )
  266. {
  267. }
  268. public TreeNode( string text, string key, string imageUrl )
  269. : this( text, key, null, null )
  270. {
  271. }
  272. public TreeNode( string text, string key, string imageUrl, string openImageUrl )
  273. {
  274. this.Text = text;
  275. this.Key = key;
  276. this.ImageUrl = imageUrl;
  277. this.OpenImageUrl = openImageUrl;
  278. this.nodes = new TreeNodeCollection( this );
  279. }
  280. public string FullPath
  281. {
  282. get
  283. {
  284. string path = this.Text;
  285. TreeNode node = this;
  286. while( null != node.Parent )
  287. {
  288. node = node.Parent;
  289. path = path + "\\" + node.Text;
  290. }
  291. return path;
  292. }
  293. }
  294. public int Index
  295. {
  296. get
  297. {
  298. if( null == parent )
  299. return treeView.Nodes.IndexOf( this );
  300. return parent.Nodes.IndexOf( this );
  301. }
  302. }
  303. public bool IsExpanded
  304. {
  305. get { return expanded; }
  306. }
  307. public bool IsSelected
  308. {
  309. get
  310. {
  311. if( null == treeView )
  312. return false;
  313. return treeView.SelectedNode == this;
  314. }
  315. }
  316. public TreeNodeCollection Nodes
  317. {
  318. get { return nodes; }
  319. }
  320. public TreeNode Parent
  321. {
  322. get { return parent; }
  323. }
  324. public UDDI.Web.TreeView TreeView
  325. {
  326. get { return treeView; }
  327. }
  328. public void Collapse()
  329. {
  330. expanded = false;
  331. }
  332. public void EnsureVisible()
  333. {
  334. TreeNode node = this;
  335. while( null != node.Parent )
  336. {
  337. node = node.Parent;
  338. node.Expand();
  339. }
  340. }
  341. public void Expand()
  342. {
  343. expanded = true;
  344. }
  345. public void Remove()
  346. {
  347. if( null != parent )
  348. parent.Nodes.RemoveAt( Index );
  349. }
  350. public void Select()
  351. {
  352. treeView.SelectedNode = this;
  353. }
  354. public void Toggle()
  355. {
  356. expanded = !expanded;
  357. }
  358. }
  359. /// ********************************************************************
  360. /// public class TreeNodeCollection
  361. /// --------------------------------------------------------------------
  362. /// <summary>
  363. /// </summary>
  364. /// ********************************************************************
  365. ///
  366. public class TreeNodeCollection : CollectionBase
  367. {
  368. protected TreeNode parent;
  369. protected TreeView treeView;
  370. internal protected TreeNodeCollection( TreeNode parent )
  371. {
  372. this.parent = parent;
  373. this.treeView = parent.TreeView;
  374. }
  375. internal protected TreeNodeCollection( TreeView treeView )
  376. {
  377. this.parent = null;
  378. this.treeView = treeView;
  379. }
  380. public TreeNode this[ int index ]
  381. {
  382. get { return (TreeNode)List[ index ]; }
  383. set { List[ index ] = value; }
  384. }
  385. public TreeNode Add( string text )
  386. {
  387. return Add( text, null, null, null );
  388. }
  389. public TreeNode Add( string text, string key )
  390. {
  391. return Add( text, key, null, null );
  392. }
  393. public TreeNode Add( string text, string key, string imageUrl )
  394. {
  395. return Add( text, key, imageUrl, null );
  396. }
  397. public TreeNode Add( string text, string key, string imageUrl, string openImageUrl )
  398. {
  399. TreeNode node = new TreeNode( text, key, imageUrl, openImageUrl );
  400. node.parent = this.parent;
  401. node.treeView = this.treeView;
  402. node.Nodes.treeView = this.treeView;
  403. List.Add( node );
  404. return node;
  405. }
  406. public int Add( TreeNode node )
  407. {
  408. node.parent = this.parent;
  409. node.treeView = this.treeView;
  410. node.Nodes.treeView = this.treeView;
  411. return List.Add( node );
  412. }
  413. public int IndexOf( TreeNode node )
  414. {
  415. return List.IndexOf( node );
  416. }
  417. public void Remove( TreeNode node )
  418. {
  419. List.Remove( node );
  420. }
  421. }
  422. }