IPanel.cs 4.8 KB

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