MasterDetailPanel.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 virtual string SectionName => SelectedPage?.DataModelSource()?.SectionName ?? MasterColumnsTag;
  44. public virtual void CreateToolbarButtons(IPanelHost host)
  45. {
  46. SelectedPage?.CreateToolbarButtons(host);
  47. }
  48. protected override void DoSplitPanelChanged()
  49. {
  50. SaveSettings();
  51. var newTag = GetMasterColumnsTag();
  52. if (!String.Equals(_masterGrid.ColumnsTag, newTag))
  53. {
  54. _masterGrid.ColumnsTag = newTag;
  55. _masterGrid.Refresh(true, true);
  56. }
  57. }
  58. private string GetMasterColumnsTag()
  59. {
  60. var newTag = Settings.ViewType == DynamicSplitPanelView.Master
  61. ? $"{MasterColumnsTag}:Master"
  62. : $"{MasterColumnsTag}:Combined";
  63. return newTag;
  64. }
  65. protected MasterDetailPanel()
  66. {
  67. _masterGrid = new TMasterGrid();
  68. _masterGrid.OnSelectItem += MasterGrid_OnSelectItem;
  69. _masterGrid.BeforeRefresh += MasterGrid_BeforeRefresh;
  70. _masterGrid.AfterRefresh += MasterGrid_AfterRefresh;
  71. }
  72. public void Setup()
  73. {
  74. LoadSettings();
  75. _masterGrid.ColumnsTag = GetMasterColumnsTag();
  76. _splitPanel.Master = _masterGrid;
  77. _splitPanel.MasterCaption = MasterCaption;
  78. _splitPanel.DetailCaption = DetailCaption;
  79. _header.Content = MasterCaption;
  80. CreatePages();
  81. timer = new DispatcherTimer();
  82. timer.Tick += Timer_Tick;
  83. timer.Interval = new TimeSpan(0, 0, 0, 0, 100);
  84. }
  85. protected TPage? CreatePage<TPage>(Func<bool> isAllowed, string caption)
  86. where TPage : class, IMasterDetailPage<TMaster>
  87. {
  88. if (isAllowed())
  89. {
  90. var header = new DynamicTabItem() { Header = caption };
  91. _tabControl.Items.Add(header);
  92. if (Activator.CreateInstance(typeof(TPage), header) is TPage page)
  93. {
  94. _pages.Add(page);
  95. page.OnUpdateDataModel += Page_OnUpdateDataModel;
  96. return page;
  97. }
  98. }
  99. return null;
  100. }
  101. private void Page_OnUpdateDataModel(string section, DataModel model)
  102. {
  103. OnUpdateDataModel?.Invoke(section, model);
  104. }
  105. protected TPage? CreatePage<TPage>(string caption)
  106. where TPage : class, IMasterDetailPage<TMaster>
  107. {
  108. return CreatePage<TPage>(() => true, caption);
  109. }
  110. public void Shutdown(CancelEventArgs? cancel)
  111. {
  112. foreach(var page in _pages)
  113. {
  114. page.Shutdown(cancel);
  115. if (cancel?.Cancel == true) return;
  116. }
  117. if (timer != null)
  118. timer.IsEnabled = false;
  119. }
  120. public void Refresh()
  121. {
  122. _masterGrid.Refresh(timer?.IsEnabled == false, true);
  123. _lastselection = DateTime.MinValue;
  124. if (timer != null)
  125. timer.IsEnabled = true;
  126. }
  127. public DataModel DataModel(Selection selection)
  128. {
  129. if (_modelsource == null)
  130. {
  131. var row = _masterGrid.SelectedRows.FirstOrDefault();
  132. var filter = row != null
  133. ? new Filter<TMaster>(x => x.ID).IsEqualTo(row.Get<TMaster, Guid>(x => x.ID))
  134. : new Filter<TMaster>().None();
  135. return new AutoDataModel<TMaster>(filter);
  136. }
  137. return _modelsource.DataModel(selection);
  138. }
  139. public event DataModelUpdateEvent? OnUpdateDataModel;
  140. public bool IsReady { get; set; }
  141. protected IMasterDetailPage<TMaster>? SelectedPage => _pages.FirstOrDefault(x => x.Tab == _tabControl.SelectedTab);
  142. public Dictionary<string, object[]> Selected()
  143. {
  144. return SelectedPage != null
  145. ? SelectedPage.Selected()
  146. : new Dictionary<string, object[]> { { typeof(TMaster).EntityName(), _masterGrid.SelectedRows } };
  147. }
  148. public void Heartbeat(TimeSpan time)
  149. {
  150. SelectedPage?.Heartbeat(time);
  151. }
  152. protected override void DoPagesChanged()
  153. {
  154. _lastselection = DateTime.MinValue;
  155. }
  156. private void Timer_Tick(object? sender, EventArgs e)
  157. {
  158. if (_lastselection < DateTime.Now.AddMilliseconds(-500) && !_masterGridRefreshing)
  159. {
  160. _lastselection = DateTime.MaxValue;
  161. var master = _masterGrid.SelectedRows.FirstOrDefault()?.ToObject<TMaster>() ?? new TMaster();
  162. var dataModelSource = SelectedPage?.Refresh(master);
  163. if (_tabControl.SelectedIndex != _currentPage)
  164. {
  165. if(dataModelSource is not null)
  166. {
  167. _modelsource = dataModelSource;
  168. OnUpdateDataModel?.Invoke(dataModelSource.SectionName, dataModelSource.DataModel(Selection.None));
  169. }
  170. _currentPage = _tabControl.SelectedIndex;
  171. }
  172. }
  173. }
  174. private bool _masterGridRefreshing;
  175. private void MasterGrid_BeforeRefresh(object sender, BeforeRefreshEventArgs args)
  176. {
  177. _masterGridRefreshing = true;
  178. }
  179. private void MasterGrid_AfterRefresh(object sender, AfterRefreshEventArgs args)
  180. {
  181. _masterGrid.SelectedRows = Settings.MasterID == Guid.Empty
  182. ? Array.Empty<CoreRow>()
  183. : _masterGrid.Data.Rows.Where(r => r.Get<TMaster, Guid>(c => c.ID) == Settings.MasterID).ToArray();
  184. _masterGridRefreshing = false;
  185. }
  186. private void MasterGrid_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  187. {
  188. if (!_masterGridRefreshing)
  189. {
  190. _lastselection = DateTime.Now;
  191. SaveSettings();
  192. }
  193. }
  194. }
  195. public abstract class MasterDetailPanel : UserControl
  196. {
  197. protected DynamicSplitPanel _splitPanel;
  198. protected Label _header;
  199. protected DynamicTabControl _tabControl;
  200. protected abstract string MasterCaption { get; }
  201. protected abstract string DetailCaption { get; }
  202. protected MasterDetailPanel()
  203. {
  204. _splitPanel = new DynamicSplitPanel
  205. {
  206. View = DynamicSplitPanelView.Combined,
  207. AnchorWidth = 300,
  208. MasterCaption = MasterCaption,
  209. DetailCaption = DetailCaption
  210. };
  211. _splitPanel.OnChanged += SplitPanel_OnChanged;
  212. _header = new Label
  213. {
  214. HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
  215. VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
  216. };
  217. _splitPanel.Header = new Border
  218. {
  219. BorderBrush = new SolidColorBrush(Colors.Gray),
  220. BorderThickness = new System.Windows.Thickness(0.75),
  221. Background = new SolidColorBrush(Colors.WhiteSmoke),
  222. Height = 25,
  223. Child = _header
  224. };
  225. _tabControl = new DynamicTabControl();
  226. Grid.SetColumn(_tabControl, 2);
  227. Grid.SetRow(_tabControl, 0);
  228. Grid.SetRowSpan(_tabControl, 2);
  229. _tabControl.SelectionChanged += Pages_OnChanged;
  230. _tabControl.SeparatorMargin = 4;
  231. _splitPanel.Detail = _tabControl;
  232. Content = _splitPanel;
  233. }
  234. protected abstract void DoSplitPanelChanged();
  235. private void SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  236. {
  237. DoSplitPanelChanged();
  238. }
  239. protected abstract void DoPagesChanged();
  240. private void Pages_OnChanged(object sender, SelectionChangedEventArgs e)
  241. {
  242. if (Equals(e.Source, _tabControl))
  243. DoPagesChanged();
  244. }
  245. }