MasterDetailPanel.cs 9.4 KB

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