InteractiveTree.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Linq;
  3. using Terminal.Gui;
  4. namespace UICatalog.Scenarios {
  5. [ScenarioMetadata (Name: "Interactive Tree", Description: "Create nodes and child nodes in TreeView.")]
  6. [ScenarioCategory ("Controls"), ScenarioCategory ("TreeView")]
  7. public class InteractiveTree : Scenario {
  8. TreeView treeView;
  9. public override void Setup ()
  10. {
  11. Win.Title = this.GetName ();
  12. Win.Y = 1; // menu
  13. Win.Height = Dim.Fill (1); // status bar
  14. var menu = new MenuBar (new MenuBarItem [] {
  15. new MenuBarItem ("_File", new MenuItem [] {
  16. new MenuItem ("_Quit", "", () => Quit()),
  17. })
  18. });
  19. Application.Top.Add (menu);
  20. treeView = new TreeView () {
  21. X = 0,
  22. Y = 0,
  23. Width = Dim.Fill (),
  24. Height = Dim.Fill (1),
  25. };
  26. treeView.KeyDown += TreeView_KeyPress;
  27. Win.Add (treeView);
  28. var statusBar = new StatusBar (new StatusItem [] {
  29. new StatusItem(Application.QuitKey, $"{Application.QuitKey} to Quit", () => Quit()),
  30. new StatusItem(KeyCode.CtrlMask | KeyCode.C, "~^C~ Add Child", () => AddChildNode()),
  31. new StatusItem(KeyCode.CtrlMask | KeyCode.T, "~^T~ Add Root", () => AddRootNode()),
  32. new StatusItem(KeyCode.CtrlMask | KeyCode.R, "~^R~ Rename Node", () => RenameNode()),
  33. });
  34. Application.Top.Add (statusBar);
  35. }
  36. private void TreeView_KeyPress (object sender, Key obj)
  37. {
  38. if (obj.KeyCode == KeyCode.Delete) {
  39. var toDelete = treeView.SelectedObject;
  40. if (toDelete == null) {
  41. return;
  42. }
  43. obj.Handled = true;
  44. // if it is a root object remove it
  45. if (treeView.Objects.Contains (toDelete)) {
  46. treeView.Remove (toDelete);
  47. } else {
  48. var parent = treeView.GetParent (toDelete);
  49. if (parent == null) {
  50. MessageBox.ErrorQuery ("Could not delete", $"Parent of '{toDelete}' was unexpectedly null", "Ok");
  51. } else {
  52. //update the model
  53. parent.Children.Remove (toDelete);
  54. //refresh the tree
  55. treeView.RefreshObject (parent);
  56. }
  57. }
  58. }
  59. }
  60. private void RenameNode ()
  61. {
  62. var node = treeView.SelectedObject;
  63. if (node != null) {
  64. if (GetText ("Text", "Enter text for node:", node.Text, out string entered)) {
  65. node.Text = entered;
  66. treeView.RefreshObject (node);
  67. }
  68. }
  69. }
  70. private void AddRootNode ()
  71. {
  72. if (GetText ("Text", "Enter text for node:", "", out string entered)) {
  73. treeView.AddObject (new TreeNode (entered));
  74. }
  75. }
  76. private void AddChildNode ()
  77. {
  78. var node = treeView.SelectedObject;
  79. if (node != null) {
  80. if (GetText ("Text", "Enter text for node:", "", out string entered)) {
  81. node.Children.Add (new TreeNode (entered));
  82. treeView.RefreshObject (node);
  83. }
  84. }
  85. }
  86. private bool GetText (string title, string label, string initialText, out string enteredText)
  87. {
  88. bool okPressed = false;
  89. var ok = new Button ("Ok", is_default: true);
  90. ok.Clicked += (s,e) => { okPressed = true; Application.RequestStop (); };
  91. var cancel = new Button ("Cancel");
  92. cancel.Clicked += (s,e) => { Application.RequestStop (); };
  93. var d = new Dialog (ok, cancel) { Title = title };
  94. var lbl = new Label () {
  95. X = 0,
  96. Y = 1,
  97. Text = label
  98. };
  99. var tf = new TextField () {
  100. Text = initialText,
  101. X = 0,
  102. Y = 2,
  103. Width = Dim.Fill ()
  104. };
  105. d.Add (lbl, tf);
  106. tf.SetFocus ();
  107. Application.Run (d);
  108. enteredText = okPressed ? tf.Text : null;
  109. return okPressed;
  110. }
  111. private void Quit ()
  112. {
  113. Application.RequestStop ();
  114. }
  115. }
  116. }