ActionEditors.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using InABox.Core;
  2. using InABox.DynamicGrid;
  3. using InABox.WPF;
  4. using Microsoft.CodeAnalysis.VisualBasic.Syntax;
  5. using PRS.Shared.Events;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Linq;
  10. using System.Runtime.CompilerServices;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Media;
  16. namespace PRS.Shared.Grids.EventEditor;
  17. public interface IEventActionEditor
  18. {
  19. bool Edit(IEventAction action, IEventDataModelDefinition dataModelDefinition);
  20. }
  21. /// <summary>
  22. /// Indicates that this class is the editor for <typeparamref name="TAction"/>.
  23. /// </summary>
  24. /// <remarks>
  25. /// It is expected that if <typeparamref name="TAction"/> is generic, then this type should have exactly the same
  26. /// generic arguments.
  27. /// </remarks>
  28. public interface IEventActionEditor<TAction> : IEventActionEditor
  29. where TAction : IEventAction
  30. {
  31. bool Edit(TAction action, IEventDataModelDefinition dataModelDefinition);
  32. bool IEventActionEditor.Edit(IEventAction action, IEventDataModelDefinition dataModelDefinition) => Edit((TAction)action, dataModelDefinition);
  33. }
  34. public static class EventActionEditors
  35. {
  36. private static Dictionary<Type, Type>? _actionEditors;
  37. private static Dictionary<Type, Type> GetActionEditors()
  38. {
  39. if(_actionEditors is null)
  40. {
  41. _actionEditors = new Dictionary<Type, Type>();
  42. foreach(var type in CoreUtils.TypeList(x => true))
  43. {
  44. if (type.GetInterfaceDefinition(typeof(IEventActionEditor<>)) is Type editorInterface)
  45. {
  46. var actionType = editorInterface.GenericTypeArguments[0];
  47. actionType = actionType.IsGenericType ? actionType.GetGenericTypeDefinition() : actionType;
  48. _actionEditors[actionType] = type;
  49. }
  50. }
  51. }
  52. return _actionEditors;
  53. }
  54. public static Type? GetActionEditorType(Type actionType)
  55. {
  56. var genericActionType = actionType.IsGenericType ? actionType.GetGenericTypeDefinition() : actionType;
  57. if(GetActionEditors().GetValueOrDefault(genericActionType) is Type editorType)
  58. {
  59. return editorType.IsGenericType ? editorType.MakeGenericType(actionType.GenericTypeArguments) : editorType;
  60. }
  61. else
  62. {
  63. return null;
  64. }
  65. }
  66. public static bool EditAction<TEvent, TDataModel>(IEventAction<TEvent> action, IEventDataModelDefinition dataModelDefinition)
  67. where TEvent : IEvent<TDataModel>
  68. where TDataModel : IEventDataModel
  69. {
  70. var editorType = GetActionEditorType(action.GetType());
  71. if (editorType is null) return true;
  72. var editor = (Activator.CreateInstance(editorType) as IEventActionEditor)!;
  73. return editor.Edit(action, dataModelDefinition);
  74. }
  75. }