IPanel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Reflection;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using InABox.Clients;
  9. using InABox.Configuration;
  10. using InABox.Core;
  11. using InABox.DynamicGrid;
  12. namespace InABox.Wpf;
  13. public interface IPanelActionItem
  14. {
  15. }
  16. public interface IPanelActionEntry
  17. {
  18. object? Data { get; set; }
  19. string? Caption { get; set; }
  20. Bitmap? Image { get; set; }
  21. void Execute();
  22. }
  23. public class PanelActionEntry<T> : DependencyObject, IPanelActionEntry
  24. {
  25. public static readonly DependencyProperty DataProperty = DependencyProperty.Register(
  26. nameof(Data),
  27. typeof(T),
  28. typeof(PanelActionEntry<T>)
  29. );
  30. public T? Data { get; set; }
  31. object? IPanelActionEntry.Data
  32. {
  33. get => this.Data;
  34. set => this.Data = value == null ? default(T) : (T)value;
  35. }
  36. public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register(
  37. nameof(Caption),
  38. typeof(string),
  39. typeof(PanelActionEntry<T>),
  40. new PropertyMetadata("")
  41. );
  42. public string? Caption { get; set; }
  43. public static readonly DependencyProperty ImageProperty = DependencyProperty.Register(
  44. nameof(Image),
  45. typeof(Bitmap),
  46. typeof(PanelActionEntry<T>)
  47. );
  48. public Bitmap? Image
  49. {
  50. get => GetValue(ImageProperty) as Bitmap;
  51. set => SetValue(ImageProperty, value);
  52. }
  53. public Action<PanelActionEntry<T>>? OnExecute { get; set; }
  54. public PanelActionEntry()
  55. {
  56. }
  57. public PanelActionEntry(string? caption, Bitmap? image, T? data, Action<PanelActionEntry<T>>? onExecute)
  58. {
  59. Data = data;
  60. Caption = caption;
  61. Image = image;
  62. OnExecute = onExecute;
  63. }
  64. public void Execute()
  65. {
  66. OnExecute?.Invoke(this);
  67. }
  68. }
  69. public class PanelAction : DependencyObject, IPanelActionItem
  70. {
  71. public Func<PanelAction,IPanelActionEntry[]>? OnPopulate { get; set; }
  72. public Action<PanelAction>? OnExecute { get; set; }
  73. public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register(
  74. nameof(IsEnabled),
  75. typeof(bool),
  76. typeof(PanelAction),
  77. new PropertyMetadata(true)
  78. );
  79. public bool IsEnabled
  80. {
  81. get => (bool)GetValue(IsEnabledProperty);
  82. set => SetValue(IsEnabledProperty, value);
  83. }
  84. public static readonly DependencyProperty VisibilityProperty = DependencyProperty.Register(
  85. nameof(Visibility),
  86. typeof(Visibility),
  87. typeof(PanelAction),
  88. new PropertyMetadata(Visibility.Visible)
  89. );
  90. public Visibility Visibility
  91. {
  92. get => (Visibility)GetValue(VisibilityProperty);
  93. set => SetValue(VisibilityProperty, value);
  94. }
  95. public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register(
  96. nameof(Caption),
  97. typeof(string),
  98. typeof(PanelAction),
  99. new PropertyMetadata("")
  100. );
  101. public string Caption
  102. {
  103. get => (string)GetValue(CaptionProperty);
  104. set => SetValue(CaptionProperty, value);
  105. }
  106. public static readonly DependencyProperty ImageProperty = DependencyProperty.Register(
  107. nameof(Image),
  108. typeof(Bitmap),
  109. typeof(PanelAction)
  110. );
  111. public Bitmap? Image
  112. {
  113. get => GetValue(ImageProperty) as Bitmap;
  114. set => SetValue(ImageProperty, value);
  115. }
  116. public static readonly DependencyProperty MenuProperty = DependencyProperty.Register(
  117. nameof(Menu),
  118. typeof(ContextMenu),
  119. typeof(PanelAction)
  120. );
  121. public ContextMenu? Menu
  122. {
  123. get => GetValue(MenuProperty) as ContextMenu;
  124. set => SetValue(MenuProperty, value);
  125. }
  126. public PanelAction()
  127. {
  128. }
  129. public PanelAction(string caption, Bitmap? image, Action<PanelAction>? onExecute,
  130. Func<PanelAction, IPanelActionEntry[]>? onPopulate = null)
  131. {
  132. Caption = caption;
  133. Image = image;
  134. OnPopulate = onPopulate;
  135. OnExecute = onExecute;
  136. }
  137. public void Execute()
  138. {
  139. OnExecute?.Invoke(this);
  140. }
  141. public IPanelActionEntry[]? Populate()
  142. {
  143. return OnPopulate?.Invoke(this);
  144. }
  145. }
  146. public class PanelActionSeparator : IPanelActionItem
  147. {
  148. }
  149. public interface ICorePanel
  150. {
  151. void Setup();
  152. /// <summary>
  153. /// Shutdown the panel.
  154. /// </summary>
  155. /// <param name="cancel">If the operation can be cancelled, this is not <see langword="null"/>.</param>
  156. void Shutdown(CancelEventArgs? cancel);
  157. void Refresh();
  158. }
  159. public interface IBasePanel : ICorePanel, IDataModelSource
  160. {
  161. bool IsReady { get; set; }
  162. void CreateToolbarButtons(IPanelHost host);
  163. Dictionary<string, object[]> Selected();
  164. void Heartbeat(TimeSpan time);
  165. }
  166. public interface IPanel<T> : IBasePanel
  167. {
  168. }
  169. public interface IPropertiesPanel<TProperties>
  170. where TProperties : BaseObject, IGlobalConfigurationSettings, new()
  171. {
  172. public TProperties Properties { get; set; }
  173. }
  174. public interface IPropertiesPanel<TProperties, TSecurity> : IPropertiesPanel<TProperties>
  175. where TProperties : BaseObject, IGlobalConfigurationSettings, new()
  176. where TSecurity : ISecurityDescriptor, new()
  177. {
  178. }
  179. public static class PanelUtils
  180. {
  181. #region Properties
  182. public static void InitializePanelProperties(IBasePanel panel)
  183. {
  184. var propertiesInterface = panel.GetType().GetInterfaceDefinition(typeof(IPropertiesPanel<>));
  185. if (propertiesInterface is not null)
  186. {
  187. var propertiesType = propertiesInterface.GenericTypeArguments[0];
  188. var method = typeof(PanelUtils)
  189. .GetMethod(nameof(InitializePanelPropertiesGeneric), BindingFlags.Public | BindingFlags.Static)
  190. ?.MakeGenericMethod(panel.GetType(), propertiesType)
  191. .Invoke(null, new object?[] { panel });
  192. }
  193. }
  194. public static void InitializePanelPropertiesGeneric<TPanel, TProperties>(TPanel panel)
  195. where TPanel : IPropertiesPanel<TProperties>
  196. where TProperties : BaseObject, IGlobalConfigurationSettings, new()
  197. {
  198. panel.Properties = LoadPanelProperties<TPanel, TProperties>();
  199. }
  200. public static TProperties LoadPanelProperties<TPanel, TProperties>()
  201. where TPanel : IPropertiesPanel<TProperties>
  202. where TProperties : BaseObject, IGlobalConfigurationSettings, new()
  203. {
  204. var config = new GlobalConfiguration<TProperties>();
  205. return config.Load();
  206. }
  207. public static void SavePanelProperties<TPanel, TProperties>(TProperties properties)
  208. where TPanel : IPropertiesPanel<TProperties>
  209. where TProperties : BaseObject, IGlobalConfigurationSettings, new()
  210. {
  211. var config = new GlobalConfiguration<TProperties>();
  212. config.Save(properties);
  213. }
  214. public static void EditPanelProperties<TPanel, TProperties>()
  215. where TPanel : IPropertiesPanel<TProperties>
  216. where TProperties : BaseObject, IGlobalConfigurationSettings, new()
  217. {
  218. var properties = LoadPanelProperties<TPanel, TProperties>();
  219. bool result;
  220. if (DynamicGridUtils.TryFindDynamicGrid(typeof(DynamicGrid<>), typeof(TProperties), out var gridType))
  221. {
  222. var grid = (Activator.CreateInstance(gridType) as DynamicGrid<TProperties>)!;
  223. result = grid.EditItems(new TProperties[] { properties });
  224. }
  225. else
  226. {
  227. var grid = new DynamicItemsListGrid<TProperties>();
  228. result = grid.EditItems(new TProperties[] { properties });
  229. }
  230. if (result)
  231. {
  232. SavePanelProperties<TPanel, TProperties>(properties);
  233. }
  234. }
  235. public static void ConfigurePanel(IBasePanel panel)
  236. {
  237. var propertiesInterface = panel.GetType().GetInterfaceDefinition(typeof(IPropertiesPanel<>))!;
  238. var propertiesType = propertiesInterface.GenericTypeArguments[0];
  239. var basemethod = typeof(PanelUtils)
  240. .GetMethod(nameof(EditPanelProperties), BindingFlags.Public | BindingFlags.Static);
  241. if (basemethod == null)
  242. return;
  243. var method = basemethod?.MakeGenericMethod(panel.GetType(), propertiesType);
  244. if (method != null)
  245. method.Invoke(null, Array.Empty<object?>());
  246. }
  247. #endregion
  248. public static T LoadPanel<T>() where T : class, IBasePanel, new()
  249. {
  250. var panel = new T();
  251. InitializePanelProperties(panel);
  252. panel.IsReady = false;
  253. panel.Setup();
  254. panel.IsReady = true;
  255. return panel;
  256. }
  257. public static void UnloadPanel(IBasePanel panel, CancelEventArgs? cancel)
  258. {
  259. try
  260. {
  261. if(panel is ISubPanelHost host)
  262. {
  263. host.ShutdownSubPanels(cancel);
  264. }
  265. panel.Shutdown(cancel);
  266. if (cancel?.Cancel == true)
  267. {
  268. return;
  269. }
  270. }
  271. catch (Exception e)
  272. {
  273. Logger.Send(LogType.Error, ClientFactory.UserID, string.Format("Error in UnloadPanel(): {0}\n{1}", e.Message, e.StackTrace));
  274. }
  275. }
  276. }
  277. public interface IPanelHost
  278. {
  279. void CreatePanelAction(PanelAction action);
  280. void CreateReport(PanelAction action);
  281. void CreateSetupAction(PanelAction action);
  282. void CreateSetupSeparator();
  283. }
  284. public static class IPanelHostExtensions
  285. {
  286. public static void CreateSetupAction(this IPanelHost host, string caption, Bitmap? image, Action<PanelAction> onExecute, ContextMenu? menu = null)
  287. {
  288. host.CreateSetupAction(new PanelAction(caption, image, onExecute) { Menu = menu });
  289. }
  290. public static void CreateSetupActionIf(this IPanelHost host, string caption, Bitmap? image, Action<PanelAction> onExecute, bool canView, ContextMenu? menu = null)
  291. {
  292. if (canView)
  293. {
  294. host.CreateSetupAction(new PanelAction(caption, image, onExecute) { Menu = menu });
  295. }
  296. }
  297. public static void CreateSetupActionIf<TSecurity>(this IPanelHost host, string caption, Bitmap? image, Action<PanelAction> onExecute, ContextMenu? menu = null)
  298. where TSecurity : ISecurityDescriptor, new()
  299. {
  300. host.CreateSetupActionIf(caption, image, onExecute, Security.IsAllowed<TSecurity>(), menu);
  301. }
  302. public static void CreateSetupActionIfCanView<T>(this IPanelHost host, string caption, Bitmap? image, Action<PanelAction> onExecute, ContextMenu? menu = null)
  303. where T : Entity, new()
  304. {
  305. host.CreateSetupActionIf(caption, image, onExecute, Security.CanView<T>(), menu);
  306. }
  307. }
  308. /// <summary>
  309. /// Implement this interface to cause a class to act as a host of <see cref="ISubPanel"/>. Then, you can use the extension functions
  310. /// <see cref="ISubPanelHostExtensions.ShutdownSubPanels(ISubPanelHost, CancelEventArgs?)"/> and
  311. /// <see cref="ISubPanelHostExtensions.AddSubPanel(ISubPanelHost, ISubPanel)"/> to interact with it.
  312. /// </summary>
  313. /// <remarks>
  314. /// If you mark an <see cref="IPanel{T}"/> as an <see cref="ISubPanelHost"/>, then the shutdown method is called automatically by <see cref="IPanelHost"/>.
  315. /// </remarks>
  316. public interface ISubPanelHost
  317. {
  318. List<ISubPanel> SubPanels { get; }
  319. static ISubPanelHost Global = new GlobalSubPanelHost();
  320. private class GlobalSubPanelHost : ISubPanelHost
  321. {
  322. public List<ISubPanel> SubPanels { get; set; } = new();
  323. }
  324. }
  325. public static class ISubPanelHostExtensions
  326. {
  327. public static void ShutdownSubPanels(this ISubPanelHost host, CancelEventArgs? cancel)
  328. {
  329. ISubPanel[] panels;
  330. lock (host.SubPanels)
  331. {
  332. panels = host.SubPanels.ToArray();
  333. host.SubPanels.Clear();
  334. var isCancelled = false;
  335. foreach(var panel in panels)
  336. {
  337. if (isCancelled)
  338. {
  339. host.SubPanels.Add(panel);
  340. }
  341. else
  342. {
  343. panel.Shutdown(cancel);
  344. if (cancel?.Cancel == true)
  345. {
  346. isCancelled = true;
  347. host.SubPanels.Add(panel);
  348. }
  349. }
  350. }
  351. }
  352. }
  353. public static void AddSubPanel(this ISubPanelHost host, ISubPanel panel)
  354. {
  355. host.SubPanels.Add(panel);
  356. panel.SubPanelClosed += p =>
  357. {
  358. lock (host.SubPanels)
  359. {
  360. host.SubPanels.Remove(p);
  361. }
  362. };
  363. }
  364. }
  365. /// <summary>
  366. /// An <see cref="ISubPanel"/> is a non-modal window, which is tied to a parent
  367. /// </summary>
  368. public interface ISubPanel
  369. {
  370. public delegate void ClosedEvent(ISubPanel panel);
  371. /// <summary>
  372. /// Event to be called when a sub-panel closes itself; in this case, <see cref="Shutdown(CancelEventArgs?)"/> will not be called. This allows
  373. /// the host to get rid of the sub-panel, instead of keeping it forever.
  374. /// </summary>
  375. /// <remarks>
  376. /// You may call this after <see cref="Shutdown(CancelEventArgs?)"/> has been called.
  377. /// </remarks>
  378. public event ClosedEvent? SubPanelClosed;
  379. /// <summary>
  380. /// Shutdown the panel.
  381. /// </summary>
  382. /// <param name="cancel">If the operation can be cancelled, this is not <see langword="null"/></param>
  383. void Shutdown(CancelEventArgs? cancel);
  384. }