|
@@ -2,10 +2,13 @@
|
|
|
using System.Collections.Generic;
|
|
|
using System.ComponentModel;
|
|
|
using System.Drawing;
|
|
|
+using System.Reflection;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Controls;
|
|
|
+using InABox.Clients;
|
|
|
using InABox.Configuration;
|
|
|
using InABox.Core;
|
|
|
+using InABox.DynamicGrid;
|
|
|
|
|
|
namespace InABox.Wpf;
|
|
|
|
|
@@ -218,6 +221,115 @@ public interface IPropertiesPanel<TProperties, TSecurity> : IPropertiesPanel<TPr
|
|
|
{
|
|
|
}
|
|
|
|
|
|
+public static class PanelUtils
|
|
|
+{
|
|
|
+ #region Properties
|
|
|
+
|
|
|
+ public static void InitializePanelProperties(IBasePanel panel)
|
|
|
+ {
|
|
|
+ var propertiesInterface = panel.GetType().GetInterfaceDefinition(typeof(IPropertiesPanel<>));
|
|
|
+ if (propertiesInterface is not null)
|
|
|
+ {
|
|
|
+ var propertiesType = propertiesInterface.GenericTypeArguments[0];
|
|
|
+ var method = typeof(PanelUtils)
|
|
|
+ .GetMethod(nameof(InitializePanelPropertiesGeneric), BindingFlags.Public | BindingFlags.Static)
|
|
|
+ ?.MakeGenericMethod(panel.GetType(), propertiesType)
|
|
|
+ .Invoke(null, new object?[] { panel });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public static void InitializePanelPropertiesGeneric<TPanel, TProperties>(TPanel panel)
|
|
|
+ where TPanel : IPropertiesPanel<TProperties>
|
|
|
+ where TProperties : BaseObject, IGlobalConfigurationSettings, new()
|
|
|
+ {
|
|
|
+ panel.Properties = LoadPanelProperties<TPanel, TProperties>();
|
|
|
+ }
|
|
|
+ public static TProperties LoadPanelProperties<TPanel, TProperties>()
|
|
|
+ where TPanel : IPropertiesPanel<TProperties>
|
|
|
+ where TProperties : BaseObject, IGlobalConfigurationSettings, new()
|
|
|
+ {
|
|
|
+ var config = new GlobalConfiguration<TProperties>();
|
|
|
+ return config.Load();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void SavePanelProperties<TPanel, TProperties>(TProperties properties)
|
|
|
+ where TPanel : IPropertiesPanel<TProperties>
|
|
|
+ where TProperties : BaseObject, IGlobalConfigurationSettings, new()
|
|
|
+ {
|
|
|
+ var config = new GlobalConfiguration<TProperties>();
|
|
|
+ config.Save(properties);
|
|
|
+ }
|
|
|
+ public static void EditPanelProperties<TPanel, TProperties>()
|
|
|
+ where TPanel : IPropertiesPanel<TProperties>
|
|
|
+ where TProperties : BaseObject, IGlobalConfigurationSettings, new()
|
|
|
+ {
|
|
|
+ var properties = LoadPanelProperties<TPanel, TProperties>();
|
|
|
+
|
|
|
+ bool result;
|
|
|
+ if (DynamicGridUtils.TryFindDynamicGrid(typeof(DynamicGrid<>), typeof(TProperties), out var gridType))
|
|
|
+ {
|
|
|
+ var grid = (Activator.CreateInstance(gridType) as DynamicGrid<TProperties>)!;
|
|
|
+ result = grid.EditItems(new TProperties[] { properties });
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var grid = new DynamicItemsListGrid<TProperties>();
|
|
|
+ result = grid.EditItems(new TProperties[] { properties });
|
|
|
+ }
|
|
|
+
|
|
|
+ if (result)
|
|
|
+ {
|
|
|
+ SavePanelProperties<TPanel, TProperties>(properties);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void ConfigurePanel(IBasePanel panel)
|
|
|
+ {
|
|
|
+ var propertiesInterface = panel.GetType().GetInterfaceDefinition(typeof(IPropertiesPanel<>))!;
|
|
|
+ var propertiesType = propertiesInterface.GenericTypeArguments[0];
|
|
|
+ var basemethod = typeof(PanelUtils)
|
|
|
+ .GetMethod(nameof(EditPanelProperties), BindingFlags.Public | BindingFlags.Static);
|
|
|
+ if (basemethod == null)
|
|
|
+ return;
|
|
|
+ var method = basemethod?.MakeGenericMethod(panel.GetType(), propertiesType);
|
|
|
+ if (method != null)
|
|
|
+ method.Invoke(null, Array.Empty<object?>());
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ public static T LoadPanel<T>() where T : class, IBasePanel, new()
|
|
|
+ {
|
|
|
+ var panel = new T();
|
|
|
+ InitializePanelProperties(panel);
|
|
|
+
|
|
|
+ panel.IsReady = false;
|
|
|
+ panel.Setup();
|
|
|
+ panel.IsReady = true;
|
|
|
+
|
|
|
+ return panel;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void UnloadPanel(IBasePanel panel, CancelEventArgs? cancel)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if(panel is ISubPanelHost host)
|
|
|
+ {
|
|
|
+ host.ShutdownSubPanels(cancel);
|
|
|
+ }
|
|
|
+ panel.Shutdown(cancel);
|
|
|
+ if (cancel?.Cancel == true)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ Logger.Send(LogType.Error, ClientFactory.UserID, string.Format("Error in UnloadPanel(): {0}\n{1}", e.Message, e.StackTrace));
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
public interface IPanelHost
|
|
|
{
|
|
|
void CreatePanelAction(PanelAction action);
|