EventActionGrid.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using InABox.Core;
  2. using InABox.DynamicGrid;
  3. using InABox.WPF;
  4. using PRS.Shared.Events;
  5. using PRS.Shared.Grids.EventEditor;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Controls;
  12. using System.Windows.Media;
  13. namespace PRS.Shared;
  14. public class EventActionContainer<TEvent, TDataModel> : BaseObject
  15. where TEvent : IEvent<TDataModel>
  16. {
  17. public IEventAction<TEvent> Action { get; set; }
  18. public string ActionType => Action.GetType().GetCaption();
  19. public string Description => Action.Description;
  20. }
  21. public class EventActionGrid<TEvent, TDataModel> : DynamicItemsListGrid<EventActionContainer<TEvent, TDataModel>>
  22. where TEvent : IEvent<TDataModel>
  23. where TDataModel : IEventDataModel
  24. {
  25. public IEnumerable<IEventAction<TEvent>> EventActions => Items.Select(x => x.Action);
  26. private readonly IEventDataModelDefinition DataModelDefinition;
  27. public EventActionGrid(IEnumerable<IEventAction<TEvent>> items, IEventDataModelDefinition dataModelDefinition)
  28. {
  29. Items.AddRange(items.Select(x => new EventActionContainer<TEvent, TDataModel> { Action = x }));
  30. DataModelDefinition = dataModelDefinition;
  31. }
  32. protected override void DoReconfigure(DynamicGridOptions options)
  33. {
  34. base.DoReconfigure(options);
  35. options.AddRows = true;
  36. options.EditRows = true;
  37. options.DeleteRows = true;
  38. options.MultiSelect = true;
  39. }
  40. public override DynamicGridColumns GenerateColumns()
  41. {
  42. var cols = new DynamicGridColumns<EventActionContainer<TEvent, TDataModel>>();
  43. cols.Add(x => x.Description);
  44. return cols;
  45. }
  46. protected override void DoAdd(bool openEditorOnDirectEdit = false)
  47. {
  48. var menu = new ContextMenu();
  49. foreach(var type in EventUtils.GetEventActionTypes(typeof(TEvent)))
  50. {
  51. menu.AddItem(type.GetCaption(), null, type, MenuAdd_Click);
  52. }
  53. foreach(var type in EventUtils.GetStandardEventActionTypes())
  54. {
  55. menu.AddItem(type.GetCaption(), null, type, MenuAdd_Click);
  56. }
  57. menu.IsOpen = true;
  58. }
  59. protected override void DoEdit()
  60. {
  61. if (SelectedRows.Length != 1) return;
  62. var row = SelectedRows.First();
  63. var item = LoadItem(row);
  64. if (EventActionEditors.EditAction<TEvent, TDataModel>(item.Action, DataModelDefinition))
  65. {
  66. UpdateRow(row, item);
  67. DoChanged();
  68. }
  69. }
  70. private void MenuAdd_Click(Type type)
  71. {
  72. if (type.IsGenericType)
  73. {
  74. if(!type.BuildGenericType()
  75. .AddInterfaceFrom(typeof(IEntityEvent<>), typeof(TEvent))
  76. .AddInterface(typeof(IStandardEventAction<>), typeof(TEvent))
  77. .TryBuild(out var newType))
  78. {
  79. return;
  80. }
  81. type = newType;
  82. }
  83. var action = (Activator.CreateInstance(type) as IEventAction<TEvent>)!;
  84. if(!EventActionEditors.EditAction<TEvent, TDataModel>(action, DataModelDefinition))
  85. {
  86. return;
  87. }
  88. Items.Add(new() { Action = action });
  89. Refresh(false, true);
  90. DoChanged();
  91. }
  92. }