EventActionGrid.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 types = EventUtils.GetEventActionTypes(typeof(TEvent));
  49. var menu = new ContextMenu();
  50. foreach(var type in types)
  51. {
  52. menu.AddItem(type.GetCaption(), null, type, MenuAdd_Click);
  53. }
  54. menu.IsOpen = true;
  55. }
  56. protected override void DoEdit()
  57. {
  58. if (SelectedRows.Length != 1) return;
  59. var row = SelectedRows.First();
  60. var item = LoadItem(row);
  61. if (EventActionEditors.EditAction<TEvent, TDataModel>(item.Action, DataModelDefinition))
  62. {
  63. UpdateRow(row, item);
  64. DoChanged();
  65. }
  66. }
  67. private void MenuAdd_Click(Type type)
  68. {
  69. if (type.IsGenericType)
  70. {
  71. type = type.MakeGenericType(typeof(TEvent).GenericTypeArguments);
  72. }
  73. var action = (Activator.CreateInstance(type) as IEventAction<TEvent>)!;
  74. if(!EventActionEditors.EditAction<TEvent, TDataModel>(action, DataModelDefinition))
  75. {
  76. return;
  77. }
  78. Items.Add(new() { Action = action });
  79. Refresh(false, true);
  80. DoChanged();
  81. }
  82. }