DFLayoutLookupFieldProperties.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace InABox.Core
  6. {
  7. public enum DFLayoutLookupDisplayType
  8. {
  9. Button = 0,
  10. Combo = 1,
  11. }
  12. public class DFLayoutLookupFieldProperties : DFLayoutFieldProperties<string>
  13. {
  14. [DoNotSerialize]
  15. [NullEditor]
  16. public List<string> AdditionalPropertiesList = new List<string>();
  17. [ComboLookupEditor(typeof(PropertyClassLookups))]
  18. [EditorSequence(100)]
  19. public string LookupType { get; set; } = "";
  20. // A JSON-serialized Filter for filtering entries in the lookup editor
  21. [FilterEditor]
  22. [EditorSequence(101)]
  23. public string Filter { get; set; } = "";
  24. /// <summary>
  25. /// A list of comma-separated property names to also pull with the lookup.
  26. /// </summary>
  27. //[TextBoxEditor(ToolTip = "A comma-separated list of property names.")]
  28. [MultiLookupEditor(typeof(AdditionalPropertyLookupGenerator))]
  29. [EditorSequence(102)]
  30. public string AdditionalProperties
  31. {
  32. get => string.Join(',', AdditionalPropertiesList);
  33. set
  34. {
  35. AdditionalPropertiesList = value.Split(',').ToList();
  36. }
  37. }
  38. [EnumLookupEditor(typeof(DFLayoutLookupDisplayType))]
  39. [EditorSequence(103)]
  40. public DFLayoutLookupDisplayType DisplayType { get; set; }
  41. public DFLayoutLookupFieldProperties()
  42. {
  43. DisplayType = DFLayoutLookupDisplayType.Button;
  44. }
  45. public override string FormatValue(object? value)
  46. {
  47. return value?.ToString() ?? "";
  48. }
  49. public override object? ParseValue(object? value)
  50. {
  51. return value?.ToString();
  52. }
  53. public override IEnumerable<CoreColumn> GetAdditionalColumns()
  54. {
  55. if(!CoreUtils.TryGetEntity(LookupType, out var entity))
  56. return Enumerable.Empty<CoreColumn>();
  57. return AdditionalPropertiesList.Select(x =>
  58. {
  59. if (CoreUtils.TryGetProperty(entity, x, out var property))
  60. return new CoreColumn
  61. {
  62. ColumnName = $"{Code}.{x}",
  63. DataType = property.PropertyType
  64. };
  65. return null;
  66. }).Where(x => x != null).Cast<CoreColumn>();
  67. }
  68. protected override void LoadProperties()
  69. {
  70. base.LoadProperties();
  71. LookupType = GetProperty(nameof(LookupType), "");
  72. Filter = GetProperty(nameof(Filter), "");
  73. AdditionalProperties = GetProperty(nameof(AdditionalProperties), "");
  74. DisplayType = GetProperty(nameof(DisplayType), DFLayoutLookupDisplayType.Button);
  75. }
  76. protected override void SaveProperties()
  77. {
  78. base.SaveProperties();
  79. SetProperty(nameof(LookupType), LookupType);
  80. SetProperty(nameof(Filter), Filter);
  81. SetProperty(nameof(AdditionalProperties), AdditionalProperties);
  82. SetProperty(nameof(DisplayType),DisplayType);
  83. }
  84. private class AdditionalPropertyLookupGenerator : LookupGenerator<object>
  85. {
  86. public AdditionalPropertyLookupGenerator(object[] items) : base(items)
  87. {
  88. }
  89. protected override void DoGenerateLookups()
  90. {
  91. if (!(Items?.FirstOrDefault() is DFLayoutLookupFieldProperties properties))
  92. return;
  93. if (!CoreUtils.TryGetEntity(properties.LookupType, out var type))
  94. return;
  95. var props = CoreUtils.PropertyList(type, x => x.GetCustomAttribute<DoNotSerialize>() == null, true).Keys.ToList();
  96. props.Sort();
  97. foreach (var prop in props)
  98. {
  99. AddValue(prop, prop);
  100. }
  101. }
  102. }
  103. }
  104. }