TableEditor.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using Terminal.Gui;
  5. using Terminal.Gui.Views;
  6. namespace UICatalog.Scenarios {
  7. [ScenarioMetadata (Name: "TableEditor", Description: "A Terminal.Gui DataTable editor via TableView")]
  8. [ScenarioCategory ("Controls")]
  9. [ScenarioCategory ("Dialogs")]
  10. [ScenarioCategory ("Text")]
  11. [ScenarioCategory ("Dialogs")]
  12. [ScenarioCategory ("TopLevel")]
  13. public class TableEditor : Scenario
  14. {
  15. TableView tableView;
  16. public override void Setup ()
  17. {
  18. var dt = BuildDemoDataTable(30,1000);
  19. Win.Title = this.GetName() + "-" + dt.TableName ?? "Untitled";
  20. Win.Y = 1; // menu
  21. Win.Height = Dim.Fill (1); // status bar
  22. Top.LayoutSubviews ();
  23. this.tableView = new TableView (dt) {
  24. X = 0,
  25. Y = 0,
  26. Width = Dim.Fill (),
  27. Height = Dim.Fill (),
  28. };
  29. tableView.CanFocus = true;
  30. tableView.KeyPress += KeyPressed;
  31. Win.Add (tableView);
  32. }
  33. private void KeyPressed (View.KeyEventEventArgs obj)
  34. {
  35. if(obj.KeyEvent.Key == Key.Enter) {
  36. EditCurrentCell();
  37. }
  38. }
  39. private void EditCurrentCell ()
  40. {
  41. var oldValue = tableView.Table.Rows[tableView.SelectedRow][tableView.SelectedColumn].ToString();
  42. bool okPressed = false;
  43. var ok = new Button ("Ok", is_default: true);
  44. ok.Clicked += () => { okPressed = true; Application.RequestStop (); };
  45. var cancel = new Button ("Cancel");
  46. cancel.Clicked += () => { Application.RequestStop (); };
  47. var d = new Dialog ("Enter new value", 60, 20, ok, cancel);
  48. var lbl = new Label() {
  49. X = 0,
  50. Y = 1,
  51. Text = tableView.Table.Columns[tableView.SelectedColumn].ColumnName
  52. };
  53. var tf = new TextField()
  54. {
  55. Text = oldValue,
  56. X = 0,
  57. Y = 2,
  58. Width = Dim.Fill()
  59. };
  60. d.Add (lbl,tf);
  61. tf.SetFocus();
  62. Application.Run (d);
  63. if(okPressed) {
  64. try {
  65. tableView.Table.Rows[tableView.SelectedRow][tableView.SelectedColumn] = string.IsNullOrWhiteSpace(tf.Text.ToString()) ? DBNull.Value : (object)tf.Text;
  66. }
  67. catch(Exception ex) {
  68. MessageBox.ErrorQuery(60,20,"Failed to set text", ex.Message,"Ok");
  69. }
  70. tableView.RefreshViewport();
  71. }
  72. }
  73. /// <summary>
  74. /// Generates a new demo <see cref="DataTable"/> with the given number of <paramref name="cols"/> (min 5) and <paramref name="rows"/>
  75. /// </summary>
  76. /// <param name="cols"></param>
  77. /// <param name="rows"></param>
  78. /// <returns></returns>
  79. public static DataTable BuildDemoDataTable(int cols, int rows)
  80. {
  81. var dt = new DataTable();
  82. dt.Columns.Add(new DataColumn("StrCol",typeof(string)));
  83. dt.Columns.Add(new DataColumn("DateCol",typeof(DateTime)));
  84. dt.Columns.Add(new DataColumn("IntCol",typeof(int)));
  85. dt.Columns.Add(new DataColumn("DoubleCol",typeof(double)));
  86. dt.Columns.Add(new DataColumn("NullsCol",typeof(string)));
  87. for(int i=0;i< cols -5; i++) {
  88. dt.Columns.Add("Column" + (i+4));
  89. }
  90. var r = new Random(100);
  91. for(int i=0;i< rows;i++) {
  92. List<object> row = new List<object>(){
  93. "Some long text that is super cool",
  94. new DateTime(2000+i,12,25),
  95. r.Next(i),
  96. r.NextDouble()*i,
  97. DBNull.Value
  98. };
  99. for(int j=0;j< cols -5; j++) {
  100. row.Add("SomeValue" + r.Next(100));
  101. }
  102. dt.Rows.Add(row.ToArray());
  103. }
  104. return dt;
  105. }
  106. }
  107. }