DynamicDashboardGridPresenter.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using InABox.Core;
  2. using InABox.DynamicGrid;
  3. using InABox.Wpf.Dashboard.Editor;
  4. using InABox.WPF;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Media;
  14. namespace InABox.Wpf.Dashboard;
  15. public class DynamicDashboardGridPresenterProperties : BaseObject
  16. {
  17. /// <summary>
  18. /// The key of the query that will provide the data for this presenter.
  19. /// </summary>
  20. public string Table { get; set; } = "";
  21. public DynamicGridColumns? Columns { get; set; }
  22. }
  23. [Caption("Grid")]
  24. public class DynamicDashboardGridPresenter : IDynamicDashboardDataPresenter<DynamicDashboardGridPresenterProperties>
  25. {
  26. public DynamicDashboardGridPresenterProperties Properties { get; set; } = null!;
  27. private DynamicDashboardDataComponent _dataComponent = null!;
  28. public DynamicDashboardDataComponent DataComponent
  29. {
  30. get => _dataComponent;
  31. set
  32. {
  33. _dataComponent = value;
  34. UpdateData();
  35. }
  36. }
  37. public int PreviewHeight => 300;
  38. public bool IsPreview { get; set; }
  39. private CoreTableGrid? Grid;
  40. private readonly ContentControl Content = new();
  41. private DynamicDashboardData? _data;
  42. public FrameworkElement? Setup()
  43. {
  44. UpdateData();
  45. return Content;
  46. }
  47. private void UpdateData()
  48. {
  49. if (Properties.Table.IsNullOrWhiteSpace() || !DataComponent.TryGetTable(Properties.Table, out var table))
  50. {
  51. Properties.Table = DataComponent.Tables.FirstOrDefault()?.Key ?? "";
  52. }
  53. if (!DataComponent.TryGetTable(Properties.Table, out table))
  54. {
  55. var border = new Border
  56. {
  57. Background = Colors.DimGray.ToBrush()
  58. };
  59. border.ContextMenuOpening += Border_ContextMenuOpening;
  60. Content.Content = border;
  61. return;
  62. }
  63. Grid = new DataGrid(this)
  64. {
  65. ColumnSchema = new DataGridColumnSchema(this)
  66. };
  67. Grid.Refresh(true, false);
  68. Content.Content = Grid as FrameworkElement;
  69. }
  70. private void CustomiseMenu(ContextMenu menu)
  71. {
  72. if (!IsPreview) return;
  73. var tableItem = menu.AddItem("Select Table", null, null);
  74. foreach(var table in DataComponent.Tables)
  75. {
  76. tableItem.AddCheckMenuItem(table.Key, table.Key, SelectTable_Click, isChecked: Properties.Table == table.Key);
  77. }
  78. if(tableItem.Items.Count <= 1)
  79. {
  80. menu.Items.Remove(tableItem);
  81. }
  82. menu.AddItem("Select Data", null, SelectData_Click);
  83. }
  84. private void SelectData_Click()
  85. {
  86. if (DynamicDashboardDataEditor.Execute(DataComponent))
  87. {
  88. UpdateData();
  89. DataComponent.RunQueryAsync(100).ContinueWith(data =>
  90. {
  91. if(data.Exception is not null)
  92. {
  93. MessageWindow.ShowError("Error loading data.", data.Exception);
  94. }
  95. else
  96. {
  97. Refresh(data.Result);
  98. }
  99. }, TaskScheduler.FromCurrentSynchronizationContext());
  100. }
  101. }
  102. private void SelectTable_Click(string key, bool isChecked)
  103. {
  104. if (!isChecked) return;
  105. Properties.Table = key;
  106. Properties.Columns = null;
  107. UpdateData();
  108. if(_data is not null)
  109. {
  110. Refresh(_data);
  111. }
  112. }
  113. private void Border_ContextMenuOpening(object sender, ContextMenuEventArgs e)
  114. {
  115. var menu = new ContextMenu();
  116. CustomiseMenu(menu);
  117. menu.IsOpen = true;
  118. e.Handled = true;
  119. }
  120. public void Refresh(DynamicDashboardData data)
  121. {
  122. _data = data;
  123. if (!DataComponent.TryGetTable(Properties.Table, out var _table) || Grid is null)
  124. {
  125. return;
  126. }
  127. if(data.TryGetData(Properties.Table, out var table))
  128. {
  129. Grid.Table = table;
  130. }
  131. else
  132. {
  133. Grid.Table.Rows.Clear();
  134. }
  135. Grid.Refresh(false, true);
  136. }
  137. public void Shutdown(CancelEventArgs? cancel)
  138. {
  139. }
  140. private class DataGridColumnSchema(DynamicDashboardGridPresenter presenter) : IDynamicGridColumnSchema
  141. {
  142. public IEnumerable<string> ColumnNames
  143. {
  144. get
  145. {
  146. if (presenter.DataComponent.TryGetTable(presenter.Properties.Table, out var table))
  147. {
  148. return table.GetColumns(presenter.DataComponent).Where(x => EditorUtils.GetEditor(x.DataType) is not null).Select(x => x.ColumnName);
  149. }
  150. else
  151. {
  152. return [];
  153. }
  154. }
  155. }
  156. public DynamicGridColumn GetColumn(string column)
  157. {
  158. var table = presenter.DataComponent.GetTable(presenter.Properties.Table);
  159. var coreCol = table.GetColumns(presenter.DataComponent).First(x => x.ColumnName == column);
  160. return DynamicGridColumn.FromCoreColumn(coreCol)!;
  161. }
  162. public string? GetComment(string column)
  163. {
  164. return null;
  165. }
  166. public bool IsVisible(string column)
  167. {
  168. return true;
  169. }
  170. }
  171. private class DataGrid(DynamicDashboardGridPresenter presenter) : CoreTableGrid
  172. {
  173. protected override void DoReconfigure(DynamicGridOptions options)
  174. {
  175. base.DoReconfigure(options);
  176. options.SelectColumns = true;
  177. }
  178. public override DynamicGridColumns GenerateColumns()
  179. {
  180. if(presenter.Properties.Columns is not null)
  181. {
  182. var columns = new DynamicGridColumns();
  183. columns.AddRange(presenter.Properties.Columns.Select(x => x.Copy()));
  184. return columns;
  185. }
  186. else
  187. {
  188. return base.GenerateColumns();
  189. }
  190. }
  191. protected override void LoadColumnsMenu(ContextMenu menu)
  192. {
  193. base.LoadColumnsMenu(menu);
  194. menu.AddSeparatorIfNeeded();
  195. presenter.CustomiseMenu(menu);
  196. menu.RemoveUnnecessarySeparators();
  197. }
  198. protected override void SaveColumns(DynamicGridColumns columns)
  199. {
  200. base.SaveColumns(columns);
  201. presenter.Properties.Columns = columns.Count > 0 ? columns : null;
  202. }
  203. protected override DynamicGridColumns LoadColumns()
  204. {
  205. return presenter.Properties.Columns ?? base.LoadColumns();
  206. }
  207. }
  208. }