DynamicDashboardEditor.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using InABox.Core;
  2. using InABox.DynamicGrid;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Diagnostics.CodeAnalysis;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Shapes;
  18. namespace InABox.Wpf.Dashboard.Editor;
  19. /// <summary>
  20. /// Interaction logic for DynamicDashboardEditor.xaml
  21. /// </summary>
  22. public partial class DynamicDashboardEditor : UserControl, INotifyPropertyChanged
  23. {
  24. private string _dashboardName = "";
  25. public string DashboardName
  26. {
  27. get => _dashboardName;
  28. set
  29. {
  30. _dashboardName = value;
  31. OnPropertyChanged();
  32. }
  33. }
  34. private string _dashboardGroup = "";
  35. public string DashboardGroup
  36. {
  37. get => _dashboardGroup;
  38. set
  39. {
  40. _dashboardGroup = value;
  41. OnPropertyChanged();
  42. }
  43. }
  44. private Tuple<string, Type>[] _presentationTypes;
  45. public Tuple<string, Type>[] PresentationTypes
  46. {
  47. get => _presentationTypes;
  48. [MemberNotNull(nameof(_presentationTypes))]
  49. set
  50. {
  51. _presentationTypes = value;
  52. OnPropertyChanged();
  53. }
  54. }
  55. private Type? _selectedPresentationType;
  56. public Type? SelectedPresentationType
  57. {
  58. get => _selectedPresentationType;
  59. set
  60. {
  61. _selectedPresentationType = value;
  62. OnPropertyChanged();
  63. if(value is not null && value.GetInterfaceDefinition(typeof(IDynamicDashboardDataPresenter<>)) is Type presentationInterface)
  64. {
  65. var propertiesType = presentationInterface.GenericTypeArguments[0];
  66. SetProperties(Activator.CreateInstance(propertiesType)!, value);
  67. }
  68. }
  69. }
  70. private DynamicDashboardDataComponent DataComponent;
  71. private object? _presenterProperties;
  72. private IDynamicDashboardDataPresenter? _presenter;
  73. public DynamicDashboardEditor(DynamicDashboard dashboard)
  74. {
  75. DataComponent = dashboard.DataComponent;
  76. if(dashboard.DataPresenter is not null)
  77. {
  78. _selectedPresentationType = dashboard.DataPresenter.GetType();
  79. }
  80. InitializeComponent();
  81. if(dashboard.DataPresenter is not null)
  82. {
  83. SetProperties(dashboard.DataPresenter.Properties, _selectedPresentationType!);
  84. }
  85. PresentationTypes = DynamicDashboardUtils.GetPresenterTypes()
  86. .Select(x => new Tuple<string, Type>(x.GetCaption(), x))
  87. .ToArray();
  88. }
  89. private void SetProperties(object properties, Type presentationType)
  90. {
  91. _presenterProperties = properties;
  92. _presenter = (Activator.CreateInstance(presentationType) as IDynamicDashboardDataPresenter)!;
  93. _presenter.Properties = _presenterProperties;
  94. _presenter.IsPreview = true;
  95. _presenter.DataComponent = DataComponent;
  96. PresentationEditorControl.Content = _presenter.Setup();
  97. PresentationEditorControl.Height = _presenter.PreviewHeight;
  98. RefreshData();
  99. }
  100. private void SelectData_Click(object sender, RoutedEventArgs e)
  101. {
  102. if (DynamicDashboardDataEditor.Execute(DataComponent))
  103. {
  104. RefreshData();
  105. }
  106. }
  107. private void SelectScripts_Click(object sender, RoutedEventArgs e)
  108. {
  109. var grid = new DynamicDashboardAdditionalTableGrid();
  110. grid.Items = DataComponent.AdditionalTables.ToList(x => new DynamicDashboardAdditionalTableEditItem(x));
  111. grid.Refresh(true, true);
  112. var dlg = new DynamicContentDialog(grid)
  113. {
  114. WindowStartupLocation = WindowStartupLocation.CenterScreen,
  115. Title = "Edit Additional Scripted Tables",
  116. CanSave = true
  117. };
  118. if(dlg.ShowDialog() == true)
  119. {
  120. DataComponent.AdditionalTables = grid.Items.Select(x => x.ToTable()).ToList();
  121. RefreshData();
  122. }
  123. }
  124. private void RefreshData()
  125. {
  126. if(_presenter is not null)
  127. {
  128. _presenter.DataComponent = DataComponent;
  129. DataComponent.RunQueryAsync(100).ContinueWith(data =>
  130. {
  131. if(data.Exception is not null)
  132. {
  133. MessageWindow.ShowError("Error loading data.", data.Exception);
  134. }
  135. else
  136. {
  137. _presenter.Refresh(data.Result);
  138. }
  139. }, TaskScheduler.FromCurrentSynchronizationContext());
  140. }
  141. }
  142. public DynamicDashboard GetDashboard()
  143. {
  144. var dashboard = new DynamicDashboard();
  145. dashboard.DataComponent = DataComponent;
  146. if(SelectedPresentationType is not null)
  147. {
  148. dashboard.DataPresenter = (Activator.CreateInstance(SelectedPresentationType) as IDynamicDashboardDataPresenter)!;
  149. dashboard.DataPresenter.Properties = _presenterProperties!;
  150. }
  151. else
  152. {
  153. dashboard.DataPresenter = null;
  154. }
  155. return dashboard;
  156. }
  157. }