DynamicVariableGrid.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Controls;
  5. using InABox.Core;
  6. using NPOI.Util.Collections;
  7. namespace InABox.DynamicGrid
  8. {
  9. internal class DynamicVariableGrid : DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>
  10. {
  11. public DynamicVariableGrid()
  12. {
  13. Options.Add(DynamicGridOption.MultiSelect);
  14. }
  15. public bool EditProperties(Type type, DFLayoutFieldProperties item)
  16. {
  17. var editor = new DynamicEditorForm(type);
  18. if (item is DFLayoutLookupFieldProperties)
  19. {
  20. editor.OnFormCustomiseEditor += LookupEditor_OnFormCustomiseEditor;
  21. editor.OnEditorValueChanged += (sender, name, value) =>
  22. {
  23. var result = DynamicGridUtils.UpdateEditorValue(new[] { item }, name, value);
  24. if (name == "LookupType")
  25. {
  26. var grid = (sender as DynamicEditorGrid)!;
  27. var edit = grid.FindEditor("Filter");
  28. if (edit is FilterEditorControl filter)
  29. {
  30. filter.FilterType = value is string str ?
  31. CoreUtils.GetEntity(str) :
  32. null;
  33. }
  34. }
  35. return new();
  36. };
  37. }
  38. editor.Items = new BaseObject[] { item };
  39. editor.OnDefineLookups += o =>
  40. {
  41. var def = (o.EditorDefinition as ILookupEditor)!;
  42. var colname = o.ColumnName;
  43. // Nope, there is nothing dodgy about this at all
  44. // I am not breaking any rules by passing in the QA Form instance, rather than the Field instance
  45. // so that I can get access to the "AppliesTo" property, and thus the list of properties that can be updated
  46. // Nothing to see here, I promise!
  47. CoreTable? values;
  48. if(o.ColumnName == "Property")
  49. {
  50. values = def.Values(colname, new object[] { Item });
  51. }
  52. else
  53. {
  54. values = def.Values(colname, new object[] { item });
  55. }
  56. o.LoadLookups(values);
  57. };
  58. return editor.ShowDialog() == true;
  59. }
  60. private void LookupEditor_OnFormCustomiseEditor(IDynamicEditorForm sender, object[] items, DynamicGridColumn column, BaseEditor editor)
  61. {
  62. if(column.ColumnName == "Filter" && editor is FilterEditor fe)
  63. {
  64. var properties = items[0] as DFLayoutLookupFieldProperties;
  65. var lookupType = properties.LookupType;
  66. var entityType = CoreUtils.GetEntity(lookupType);
  67. fe.Type = entityType;
  68. }
  69. }
  70. private void CreateMenu(ContextMenu parent, string header, Type type)
  71. {
  72. var menu = new MenuItem();
  73. menu.Header = header;
  74. menu.Tag = type;
  75. menu.Click += (o, e) =>
  76. {
  77. var itemtype = (o as MenuItem).Tag as Type;
  78. var fieldBaseType = itemtype.GetSuperclassDefinition(typeof(DFLayoutField<>));
  79. if(fieldBaseType != null)
  80. {
  81. var propertiesType = fieldBaseType.GetGenericArguments()[0];
  82. var properties = Activator.CreateInstance(propertiesType) as DFLayoutFieldProperties;
  83. if (EditProperties(propertiesType, properties))
  84. {
  85. var variable = CreateItem();
  86. variable.SaveProperties(itemtype, properties);
  87. SaveItem(variable);
  88. Refresh(false, true);
  89. }
  90. }
  91. };
  92. parent.Items.Add(menu);
  93. }
  94. protected override void DoAdd()
  95. {
  96. var menu = new ContextMenu();
  97. foreach(var fieldType in DFUtils.GetFieldTypes())
  98. {
  99. var caption = fieldType.GetCaption();
  100. if (string.IsNullOrWhiteSpace(caption))
  101. {
  102. caption = CoreUtils.Neatify(fieldType.Name);
  103. }
  104. CreateMenu(menu, caption, fieldType);
  105. }
  106. menu.IsOpen = true;
  107. }
  108. /*protected override DigitalFormVariable LoadItem(CoreRow row)
  109. {
  110. return Items.FirstOrDefault(r => r.ID.Equals(row.Get<DigitalFormVariable, Guid>(c => c.ID)));
  111. }*/
  112. protected override void DoEdit()
  113. {
  114. if (!SelectedRows.Any())
  115. return;
  116. var variable = LoadItem(SelectedRows.First());
  117. var properties = variable.CreateProperties();
  118. if (EditProperties(properties.GetType(), properties))
  119. {
  120. variable.SaveProperties(properties);
  121. SaveItem(variable);
  122. Refresh(false, true);
  123. }
  124. }
  125. }
  126. }