MasterDetailPanel.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. using System.Windows.Threading;
  8. using InABox.Configuration;
  9. using InABox.Core;
  10. using InABox.DynamicGrid;
  11. namespace InABox.Wpf;
  12. public abstract class MasterDetailPanel<TMaster, TMasterGrid, TSettings> : MasterDetailPanel, IPanel<TMaster>
  13. where TMaster : Entity, IRemotable, IPersistent, new()
  14. where TMasterGrid : DynamicDataGrid<TMaster>, new()
  15. where TSettings : BaseObject, IUserConfigurationSettings, IMasterDetailSettings, new()
  16. {
  17. private DispatcherTimer? timer;
  18. private readonly List<IMasterDetailPage<TMaster>> _pages = new();
  19. private DateTime _lastselection = DateTime.MaxValue;
  20. private int _currentPage = -1;
  21. private readonly TMasterGrid _masterGrid;
  22. private IDataModelSource? _modelsource;
  23. protected TSettings Settings { get; private set; } = new();
  24. private void LoadSettings()
  25. {
  26. Settings = new UserConfiguration<TSettings>().Load();
  27. _splitPanel.AnchorWidth = Settings.AnchorWidth;
  28. _splitPanel.View = Settings.ViewType;
  29. AfterLoadSettings(Settings);
  30. }
  31. protected void SaveSettings()
  32. {
  33. BeforeSaveSettings(Settings);
  34. Settings.AnchorWidth = _splitPanel.AnchorWidth;
  35. Settings.ViewType = _splitPanel.View;
  36. Settings.MasterID = _masterGrid.SelectedRows.FirstOrDefault()?.Get<TMaster, Guid>(x => x.ID) ?? Guid.Empty;
  37. new UserConfiguration<TSettings>().Save(Settings);
  38. }
  39. protected abstract void CreatePages();
  40. protected abstract void AfterLoadSettings(TSettings settings);
  41. protected abstract void BeforeSaveSettings(TSettings settings);
  42. protected abstract string MasterColumnsTag { get; }
  43. public abstract string SectionName { get; }
  44. public abstract void CreateToolbarButtons(IPanelHost host);
  45. protected override void DoSplitPanelChanged()
  46. {
  47. SaveSettings();
  48. var newTag = GetMasterColumnsTag();
  49. if (!String.Equals(_masterGrid.ColumnsTag, newTag))
  50. {
  51. _masterGrid.ColumnsTag = newTag;
  52. _masterGrid.Refresh(true, true);
  53. }
  54. }
  55. private string GetMasterColumnsTag()
  56. {
  57. var newTag = Settings.ViewType == DynamicSplitPanelView.Master
  58. ? $"{MasterColumnsTag}:Master"
  59. : $"{MasterColumnsTag}:Combined";
  60. return newTag;
  61. }
  62. protected MasterDetailPanel()
  63. {
  64. _masterGrid = new TMasterGrid();
  65. _masterGrid.OnSelectItem += MasterGrid_OnSelectItem;
  66. _masterGrid.BeforeRefresh += MasterGrid_BeforeRefresh;
  67. _masterGrid.AfterRefresh += MasterGrid_AfterRefresh;
  68. }
  69. public void Setup()
  70. {
  71. LoadSettings();
  72. _masterGrid.ColumnsTag = GetMasterColumnsTag();
  73. _splitPanel.Master = _masterGrid;
  74. _splitPanel.MasterCaption = MasterCaption;
  75. _splitPanel.DetailCaption = DetailCaption;
  76. _header.Content = MasterCaption;
  77. CreatePages();
  78. timer = new DispatcherTimer();
  79. timer.Tick += Timer_Tick;
  80. timer.Interval = new TimeSpan(0, 0, 0, 0, 100);
  81. }
  82. protected void CreatePage<TPage>(Func<bool> isAllowed, string caption)
  83. where TPage : class, IMasterDetailPage<TMaster>
  84. {
  85. if (isAllowed())
  86. {
  87. var header = new DynamicTabItem() { Header = caption };
  88. _tabControl.Items.Add(header);
  89. if (Activator.CreateInstance(typeof(TPage), header) is TPage page)
  90. _pages.Add(page);
  91. }
  92. }
  93. protected void CreatePage<TPage>(string caption)
  94. where TPage : class, IMasterDetailPage<TMaster>
  95. {
  96. CreatePage<TPage>(() => true, caption);
  97. }
  98. public void Shutdown(CancelEventArgs? cancel)
  99. {
  100. if (timer != null)
  101. timer.IsEnabled = false;
  102. }
  103. public void Refresh()
  104. {
  105. _masterGrid.Refresh(timer?.IsEnabled == false, true);
  106. _lastselection = DateTime.MinValue;
  107. if (timer != null)
  108. timer.IsEnabled = true;
  109. }
  110. public DataModel DataModel(Selection selection)
  111. {
  112. if (_modelsource == null)
  113. {
  114. var row = _masterGrid.SelectedRows.FirstOrDefault();
  115. var filter = row != null
  116. ? new Filter<TMaster>(x => x.ID).IsEqualTo(row.Get<TMaster, Guid>(x => x.ID))
  117. : new Filter<TMaster>().None();
  118. return new AutoDataModel<TMaster>(filter);
  119. }
  120. return _modelsource.DataModel(selection);
  121. }
  122. public event DataModelUpdateEvent? OnUpdateDataModel;
  123. public bool IsReady { get; set; }
  124. private IMasterDetailPage<TMaster>? SelectedPage => _pages.FirstOrDefault(x => x.Tab == _tabControl.SelectedTab);
  125. public Dictionary<string, object[]> Selected()
  126. {
  127. return SelectedPage != null
  128. ? SelectedPage.Selected()
  129. : new Dictionary<string, object[]> { { typeof(TMaster).EntityName(), _masterGrid.SelectedRows } };
  130. }
  131. public void Heartbeat(TimeSpan time)
  132. {
  133. }
  134. protected override void DoPagesChanged()
  135. {
  136. _lastselection = DateTime.MinValue;
  137. }
  138. private void Timer_Tick(object? sender, EventArgs e)
  139. {
  140. if (_lastselection < DateTime.Now.AddMilliseconds(-500) && !_masterGridRefreshing)
  141. {
  142. _lastselection = DateTime.MaxValue;
  143. var master = _masterGrid.SelectedRows.FirstOrDefault()?.ToObject<TMaster>() ?? new TMaster();
  144. var dataModelSource = SelectedPage?.Refresh(master);
  145. if (_tabControl.SelectedIndex != _currentPage)
  146. {
  147. if(dataModelSource is not null)
  148. {
  149. _modelsource = dataModelSource;
  150. OnUpdateDataModel?.Invoke(dataModelSource.SectionName, dataModelSource.DataModel(Selection.None));
  151. }
  152. _currentPage = _tabControl.SelectedIndex;
  153. }
  154. }
  155. }
  156. private bool _masterGridRefreshing;
  157. private void MasterGrid_BeforeRefresh(object sender, BeforeRefreshEventArgs args)
  158. {
  159. _masterGridRefreshing = true;
  160. }
  161. private void MasterGrid_AfterRefresh(object sender, AfterRefreshEventArgs args)
  162. {
  163. _masterGrid.SelectedRows = Settings.MasterID == Guid.Empty
  164. ? Array.Empty<CoreRow>()
  165. : _masterGrid.Data.Rows.Where(r => r.Get<TMaster, Guid>(c => c.ID) == Settings.MasterID).ToArray();
  166. _masterGridRefreshing = false;
  167. }
  168. private void MasterGrid_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  169. {
  170. if (!_masterGridRefreshing)
  171. {
  172. _lastselection = DateTime.Now;
  173. SaveSettings();
  174. }
  175. }
  176. }
  177. public abstract class MasterDetailPanel : UserControl
  178. {
  179. protected DynamicSplitPanel _splitPanel;
  180. protected Label _header;
  181. protected DynamicTabControl _tabControl;
  182. protected abstract string MasterCaption { get; }
  183. protected abstract string DetailCaption { get; }
  184. protected MasterDetailPanel()
  185. {
  186. _splitPanel = new DynamicSplitPanel
  187. {
  188. View = DynamicSplitPanelView.Combined,
  189. AnchorWidth = 300,
  190. MasterCaption = MasterCaption,
  191. DetailCaption = DetailCaption
  192. };
  193. _splitPanel.OnChanged += SplitPanel_OnChanged;
  194. _header = new Label
  195. {
  196. HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
  197. VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
  198. };
  199. _splitPanel.Header = new Border
  200. {
  201. BorderBrush = new SolidColorBrush(Colors.Gray),
  202. BorderThickness = new System.Windows.Thickness(0.75),
  203. Background = new SolidColorBrush(Colors.WhiteSmoke),
  204. Height = 25,
  205. Child = _header
  206. };
  207. _tabControl = new DynamicTabControl();
  208. Grid.SetColumn(_tabControl, 2);
  209. Grid.SetRow(_tabControl, 0);
  210. Grid.SetRowSpan(_tabControl, 2);
  211. _tabControl.SelectionChanged += Pages_OnChanged;
  212. _splitPanel.Detail = _tabControl;
  213. Content = _splitPanel;
  214. }
  215. protected abstract void DoSplitPanelChanged();
  216. private void SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  217. {
  218. DoSplitPanelChanged();
  219. }
  220. protected abstract void DoPagesChanged();
  221. private void Pages_OnChanged(object sender, SelectionChangedEventArgs e)
  222. {
  223. if (Equals(e.Source, _tabControl))
  224. DoPagesChanged();
  225. }
  226. }