InteractiveTree.cs 3.6 KB

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