using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; using PRS.Shared.Events; using PRS.Shared.Grids.EventEditor; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Media; namespace PRS.Shared; public class EventActionContainer : BaseObject where TEvent : IEvent { public IEventAction Action { get; set; } public string ActionType => Action.GetType().GetCaption(); public string Description => Action.Description; } public class EventActionGrid : DynamicItemsListGrid> where TEvent : IEvent where TDataModel : IEventDataModel { public IEnumerable> EventActions => Items.Select(x => x.Action); private readonly IEventDataModelDefinition DataModelDefinition; public EventActionGrid(IEnumerable> items, IEventDataModelDefinition dataModelDefinition) { Items.AddRange(items.Select(x => new EventActionContainer { Action = x })); DataModelDefinition = dataModelDefinition; } protected override void DoReconfigure(DynamicGridOptions options) { base.DoReconfigure(options); options.AddRows = true; options.EditRows = true; options.DeleteRows = true; options.MultiSelect = true; } public override DynamicGridColumns GenerateColumns() { var cols = new DynamicGridColumns>(); cols.Add(x => x.Description); return cols; } protected override void DoAdd(bool openEditorOnDirectEdit = false) { var menu = new ContextMenu(); foreach(var type in EventUtils.GetEventActionTypes(typeof(TEvent))) { menu.AddItem(type.GetCaption(), null, type, MenuAdd_Click); } foreach(var type in EventUtils.GetStandardEventActionTypes()) { menu.AddItem(type.GetCaption(), null, type, MenuAdd_Click); } menu.IsOpen = true; } protected override void DoEdit() { if (SelectedRows.Length != 1) return; var row = SelectedRows.First(); var item = LoadItem(row); if (EventActionEditors.EditAction(item.Action, DataModelDefinition)) { UpdateRow(row, item); DoChanged(); } } private void MenuAdd_Click(Type type) { if (type.IsGenericType) { if(!type.BuildGenericType() .AddInterfaceFrom(typeof(IEntityEvent<>), typeof(TEvent)) .AddInterface(typeof(IStandardEventAction<>), typeof(TEvent)) .TryBuild(out var newType)) { return; } type = newType; } var action = (Activator.CreateInstance(type) as IEventAction)!; if(!EventActionEditors.EditAction(action, DataModelDefinition)) { return; } Items.Add(new() { Action = action }); Refresh(false, true); DoChanged(); } }