123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- using Microsoft.CodeAnalysis.VisualBasic.Syntax;
- using PRS.Shared.Events;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- namespace PRS.Shared.Grids.EventEditor;
- public interface IEventActionEditor
- {
- bool Edit(IEventAction action, IEventDataModelDefinition dataModelDefinition);
- }
- /// <summary>
- /// Indicates that this class is the editor for <typeparamref name="TAction"/>.
- /// </summary>
- /// <remarks>
- /// It is expected that if <typeparamref name="TAction"/> is generic, then this type should have exactly the same
- /// generic arguments.
- /// </remarks>
- public interface IEventActionEditor<TAction> : IEventActionEditor
- where TAction : IEventAction
- {
- bool Edit(TAction action, IEventDataModelDefinition dataModelDefinition);
- bool IEventActionEditor.Edit(IEventAction action, IEventDataModelDefinition dataModelDefinition) => Edit((TAction)action, dataModelDefinition);
- }
- public static class EventActionEditors
- {
- private static Dictionary<Type, Type>? _actionEditors;
- private static Dictionary<Type, Type> GetActionEditors()
- {
- if(_actionEditors is null)
- {
- _actionEditors = new Dictionary<Type, Type>();
- foreach(var type in CoreUtils.TypeList(x => true))
- {
- if (type.GetInterfaceDefinition(typeof(IEventActionEditor<>)) is Type editorInterface)
- {
- var actionType = editorInterface.GenericTypeArguments[0];
- actionType = actionType.IsGenericType ? actionType.GetGenericTypeDefinition() : actionType;
- _actionEditors[actionType] = type;
- }
- }
- }
- return _actionEditors;
- }
- public static Type? GetActionEditorType(Type actionType)
- {
- var genericActionType = actionType.IsGenericType ? actionType.GetGenericTypeDefinition() : actionType;
- if(GetActionEditors().GetValueOrDefault(genericActionType) is Type editorType)
- {
- return editorType.IsGenericType ? editorType.MakeGenericType(actionType.GenericTypeArguments) : editorType;
- }
- else
- {
- return null;
- }
- }
- public static bool EditAction<TEvent, TDataModel>(IEventAction<TEvent> action, IEventDataModelDefinition dataModelDefinition)
- where TEvent : IEvent<TDataModel>
- where TDataModel : IEventDataModel
- {
- var editorType = GetActionEditorType(action.GetType());
- if (editorType is null) return true;
- var editor = (Activator.CreateInstance(editorType) as IEventActionEditor)!;
- return editor.Edit(action, dataModelDefinition);
- }
- }
|