123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- 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<TEvent, TDataModel> : BaseObject
- where TEvent : IEvent<TDataModel>
- {
- public IEventAction<TEvent> Action { get; set; }
- public string ActionType => Action.GetType().GetCaption();
- public string Description => Action.Description;
- }
- public class EventActionGrid<TEvent, TDataModel> : DynamicItemsListGrid<EventActionContainer<TEvent, TDataModel>>
- where TEvent : IEvent<TDataModel>
- where TDataModel : IEventDataModel
- {
- public IEnumerable<IEventAction<TEvent>> EventActions => Items.Select(x => x.Action);
- private readonly IEventDataModelDefinition DataModelDefinition;
- public EventActionGrid(IEnumerable<IEventAction<TEvent>> items, IEventDataModelDefinition dataModelDefinition)
- {
- Items.AddRange(items.Select(x => new EventActionContainer<TEvent, TDataModel> { 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<EventActionContainer<TEvent, TDataModel>>();
- 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<TEvent, TDataModel>(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<TEvent>)!;
- if(!EventActionEditors.EditAction<TEvent, TDataModel>(action, DataModelDefinition))
- {
- return;
- }
- Items.Add(new() { Action = action });
- Refresh(false, true);
- DoChanged();
- }
- }
|