IPanel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using InABox.Configuration;
  8. using InABox.Core;
  9. namespace InABox.Wpf;
  10. public interface IPanelActionItem
  11. {
  12. }
  13. public class PanelAction : DependencyObject, IPanelActionItem
  14. {
  15. public Action<PanelAction>? OnExecute { get; set; }
  16. public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register(
  17. nameof(Caption),
  18. typeof(string),
  19. typeof(PanelAction),
  20. new PropertyMetadata("")
  21. );
  22. public string Caption
  23. {
  24. get => (string)GetValue(CaptionProperty);
  25. set => SetValue(CaptionProperty, value);
  26. }
  27. public static readonly DependencyProperty ImageProperty = DependencyProperty.Register(
  28. nameof(Image),
  29. typeof(Bitmap),
  30. typeof(PanelAction)
  31. );
  32. public Bitmap? Image
  33. {
  34. get => GetValue(ImageProperty) as Bitmap;
  35. set => SetValue(ImageProperty, value);
  36. }
  37. public static readonly DependencyProperty MenuProperty = DependencyProperty.Register(
  38. nameof(Menu),
  39. typeof(ContextMenu),
  40. typeof(PanelAction)
  41. );
  42. public ContextMenu? Menu
  43. {
  44. get => GetValue(MenuProperty) as ContextMenu;
  45. set => SetValue(MenuProperty, value);
  46. }
  47. public PanelAction()
  48. {
  49. }
  50. public PanelAction(string caption, Bitmap image, Action<PanelAction> onExecute, ContextMenu? menu = null)
  51. {
  52. Caption = caption;
  53. Image = image;
  54. Menu = menu;
  55. OnExecute = onExecute;
  56. }
  57. public void Execute()
  58. {
  59. OnExecute?.Invoke(this);
  60. }
  61. }
  62. public class PanelActionSeparator : IPanelActionItem
  63. {
  64. }
  65. public interface ICorePanel
  66. {
  67. void Setup();
  68. /// <summary>
  69. /// Shutdown the panel.
  70. /// </summary>
  71. /// <param name="cancel">If the operation can be cancelled, this is not <see langword="null"/></param>
  72. void Shutdown(CancelEventArgs? cancel);
  73. void Refresh();
  74. }
  75. public interface IBasePanel : ICorePanel, IDataModelSource
  76. {
  77. bool IsReady { get; set; }
  78. void CreateToolbarButtons(IPanelHost host);
  79. Dictionary<string, object[]> Selected();
  80. void Heartbeat(TimeSpan time);
  81. }
  82. public interface IPanel<T> : IBasePanel
  83. {
  84. }
  85. public interface IPropertiesPanel<TProperties>
  86. where TProperties : BaseObject, IGlobalConfigurationSettings, new()
  87. {
  88. public TProperties Properties { get; set; }
  89. }
  90. public interface IPropertiesPanel<TProperties, TSecurity> : IPropertiesPanel<TProperties>
  91. where TProperties : BaseObject, IGlobalConfigurationSettings, new()
  92. where TSecurity : ISecurityDescriptor, new()
  93. {
  94. }
  95. public interface IPanelHost
  96. {
  97. void CreatePanelAction(PanelAction action);
  98. void CreateReport(PanelAction action);
  99. void CreateSetupAction(PanelAction action);
  100. void CreateSetupSeparator();
  101. }
  102. public static class IPanelHostExtensions
  103. {
  104. public static void CreateSetupAction(this IPanelHost host, string caption, Bitmap image, Action<PanelAction> onExecute, ContextMenu? menu = null)
  105. {
  106. host.CreateSetupAction(new PanelAction(caption, image, onExecute, menu));
  107. }
  108. public static void CreateSetupActionIf(this IPanelHost host, string caption, Bitmap image, Action<PanelAction> onExecute, bool canView, ContextMenu? menu = null)
  109. {
  110. if (canView)
  111. {
  112. host.CreateSetupAction(new PanelAction(caption, image, onExecute, menu));
  113. }
  114. }
  115. public static void CreateSetupActionIf<TSecurity>(this IPanelHost host, string caption, Bitmap image, Action<PanelAction> onExecute, ContextMenu? menu = null)
  116. where TSecurity : ISecurityDescriptor, new()
  117. {
  118. host.CreateSetupActionIf(caption, image, onExecute, Security.IsAllowed<TSecurity>(), menu);
  119. }
  120. public static void CreateSetupActionIfCanView<T>(this IPanelHost host, string caption, Bitmap image, Action<PanelAction> onExecute, ContextMenu? menu = null)
  121. where T : Entity, new()
  122. {
  123. host.CreateSetupActionIf(caption, image, onExecute, Security.CanView<T>(), menu);
  124. }
  125. }