MasterDetailPanel.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 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 TPage? 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. {
  91. _pages.Add(page);
  92. return page;
  93. }
  94. }
  95. return null;
  96. }
  97. protected TPage? CreatePage<TPage>(string caption)
  98. where TPage : class, IMasterDetailPage<TMaster>
  99. {
  100. return CreatePage<TPage>(() => true, caption);
  101. }
  102. public void Shutdown(CancelEventArgs? cancel)
  103. {
  104. if (timer != null)
  105. timer.IsEnabled = false;
  106. }
  107. public void Refresh()
  108. {
  109. _masterGrid.Refresh(timer?.IsEnabled == false, true);
  110. _lastselection = DateTime.MinValue;
  111. if (timer != null)
  112. timer.IsEnabled = true;
  113. }
  114. public DataModel DataModel(Selection selection)
  115. {
  116. if (_modelsource == null)
  117. {
  118. var row = _masterGrid.SelectedRows.FirstOrDefault();
  119. var filter = row != null
  120. ? new Filter<TMaster>(x => x.ID).IsEqualTo(row.Get<TMaster, Guid>(x => x.ID))
  121. : new Filter<TMaster>().None();
  122. return new AutoDataModel<TMaster>(filter);
  123. }
  124. return _modelsource.DataModel(selection);
  125. }
  126. public event DataModelUpdateEvent? OnUpdateDataModel;
  127. public bool IsReady { get; set; }
  128. protected IMasterDetailPage<TMaster>? SelectedPage => _pages.FirstOrDefault(x => x.Tab == _tabControl.SelectedTab);
  129. public Dictionary<string, object[]> Selected()
  130. {
  131. return SelectedPage != null
  132. ? SelectedPage.Selected()
  133. : new Dictionary<string, object[]> { { typeof(TMaster).EntityName(), _masterGrid.SelectedRows } };
  134. }
  135. public void Heartbeat(TimeSpan time)
  136. {
  137. }
  138. protected override void DoPagesChanged()
  139. {
  140. _lastselection = DateTime.MinValue;
  141. }
  142. private void Timer_Tick(object? sender, EventArgs e)
  143. {
  144. if (_lastselection < DateTime.Now.AddMilliseconds(-500) && !_masterGridRefreshing)
  145. {
  146. _lastselection = DateTime.MaxValue;
  147. var master = _masterGrid.SelectedRows.FirstOrDefault()?.ToObject<TMaster>() ?? new TMaster();
  148. var dataModelSource = SelectedPage?.Refresh(master);
  149. if (_tabControl.SelectedIndex != _currentPage)
  150. {
  151. if(dataModelSource is not null)
  152. {
  153. _modelsource = dataModelSource;
  154. OnUpdateDataModel?.Invoke(dataModelSource.SectionName, dataModelSource.DataModel(Selection.None));
  155. }
  156. _currentPage = _tabControl.SelectedIndex;
  157. }
  158. }
  159. }
  160. private bool _masterGridRefreshing;
  161. private void MasterGrid_BeforeRefresh(object sender, BeforeRefreshEventArgs args)
  162. {
  163. _masterGridRefreshing = true;
  164. }
  165. private void MasterGrid_AfterRefresh(object sender, AfterRefreshEventArgs args)
  166. {
  167. _masterGrid.SelectedRows = Settings.MasterID == Guid.Empty
  168. ? Array.Empty<CoreRow>()
  169. : _masterGrid.Data.Rows.Where(r => r.Get<TMaster, Guid>(c => c.ID) == Settings.MasterID).ToArray();
  170. _masterGridRefreshing = false;
  171. }
  172. private void MasterGrid_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  173. {
  174. if (!_masterGridRefreshing)
  175. {
  176. _lastselection = DateTime.Now;
  177. SaveSettings();
  178. }
  179. }
  180. }
  181. public abstract class MasterDetailPanel : UserControl
  182. {
  183. protected DynamicSplitPanel _splitPanel;
  184. protected Label _header;
  185. protected DynamicTabControl _tabControl;
  186. protected abstract string MasterCaption { get; }
  187. protected abstract string DetailCaption { get; }
  188. protected MasterDetailPanel()
  189. {
  190. _splitPanel = new DynamicSplitPanel
  191. {
  192. View = DynamicSplitPanelView.Combined,
  193. AnchorWidth = 300,
  194. MasterCaption = MasterCaption,
  195. DetailCaption = DetailCaption
  196. };
  197. _splitPanel.OnChanged += SplitPanel_OnChanged;
  198. _header = new Label
  199. {
  200. HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
  201. VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
  202. };
  203. _splitPanel.Header = new Border
  204. {
  205. BorderBrush = new SolidColorBrush(Colors.Gray),
  206. BorderThickness = new System.Windows.Thickness(0.75),
  207. Background = new SolidColorBrush(Colors.WhiteSmoke),
  208. Height = 25,
  209. Child = _header
  210. };
  211. _tabControl = new DynamicTabControl();
  212. Grid.SetColumn(_tabControl, 2);
  213. Grid.SetRow(_tabControl, 0);
  214. Grid.SetRowSpan(_tabControl, 2);
  215. _tabControl.SelectionChanged += Pages_OnChanged;
  216. _tabControl.SeparatorMargin = 4;
  217. _splitPanel.Detail = _tabControl;
  218. Content = _splitPanel;
  219. }
  220. protected abstract void DoSplitPanelChanged();
  221. private void SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  222. {
  223. DoSplitPanelChanged();
  224. }
  225. protected abstract void DoPagesChanged();
  226. private void Pages_OnChanged(object sender, SelectionChangedEventArgs e)
  227. {
  228. if (Equals(e.Source, _tabControl))
  229. DoPagesChanged();
  230. }
  231. }