DFLayoutLookupFieldProperties.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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<Guid, DFLayoutLookupValue>
  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(DFLayoutLookupValue value)
  46. {
  47. return value.Text;
  48. }
  49. public override Guid GetValue(DFLayoutLookupValue value)
  50. {
  51. return value.ID;
  52. }
  53. public override DFLayoutLookupValue DeserializeValue(DFLoadStorageEntry entry)
  54. {
  55. var value = new DFLayoutLookupValue();
  56. value.Deserialize(entry);
  57. return value;
  58. }
  59. public override void SerializeValue(DFSaveStorageEntry entry, DFLayoutLookupValue value)
  60. {
  61. value.Serialize(entry);
  62. }
  63. public override IEnumerable<CoreColumn> GetAdditionalColumns()
  64. {
  65. if(!CoreUtils.TryGetEntity(LookupType, out var entity))
  66. return Enumerable.Empty<CoreColumn>();
  67. return AdditionalPropertiesList.Select(x =>
  68. {
  69. if (CoreUtils.TryGetProperty(entity, x, out var property))
  70. return new CoreColumn
  71. {
  72. ColumnName = $"{Code}.{x}",
  73. DataType = property.PropertyType
  74. };
  75. return null;
  76. }).Where(x => x != null).Cast<CoreColumn>();
  77. }
  78. public override IEnumerable<KeyValuePair<string, object?>> GetAdditionalValues(DFLayoutLookupValue value)
  79. {
  80. if (!CoreUtils.TryGetEntity(LookupType, out var entity))
  81. return Enumerable.Empty<KeyValuePair<string, object?>>();
  82. return AdditionalPropertiesList
  83. .Where(x => CoreUtils.TryGetProperty(entity, x, out var property))
  84. .Select(x => new KeyValuePair<string, object?>($"{Code}.{x}", value.Values.GetValueOrDefault(x)));
  85. }
  86. public override IEnumerable<CoreColumn> GetDisplayColumns()
  87. {
  88. yield return new CoreColumn { ColumnName = Code, DataType = typeof(string) };
  89. yield return new CoreColumn { ColumnName = $"{Code}.ID", DataType = typeof(Guid) };
  90. foreach(var col in GetAdditionalColumns())
  91. {
  92. yield return col;
  93. }
  94. }
  95. public override IEnumerable<KeyValuePair<string, object?>> GetDisplayValues(DFLayoutLookupValue value)
  96. {
  97. yield return new KeyValuePair<string, object?>(Code, value.Text);
  98. yield return new KeyValuePair<string, object?>($"{Code}.ID", value.ID);
  99. foreach (var pair in GetAdditionalValues(value))
  100. {
  101. yield return pair;
  102. }
  103. }
  104. protected override void LoadProperties()
  105. {
  106. base.LoadProperties();
  107. LookupType = GetProperty(nameof(LookupType), "");
  108. Filter = GetProperty(nameof(Filter), "");
  109. AdditionalProperties = GetProperty(nameof(AdditionalProperties), "");
  110. DisplayType = GetProperty(nameof(DisplayType), DFLayoutLookupDisplayType.Button);
  111. }
  112. protected override void SaveProperties()
  113. {
  114. base.SaveProperties();
  115. SetProperty(nameof(LookupType), LookupType);
  116. SetProperty(nameof(Filter), Filter);
  117. SetProperty(nameof(AdditionalProperties), AdditionalProperties);
  118. SetProperty(nameof(DisplayType),DisplayType);
  119. }
  120. private class AdditionalPropertyLookupGenerator : LookupGenerator<object>
  121. {
  122. public AdditionalPropertyLookupGenerator(object[] items) : base(items)
  123. {
  124. }
  125. protected override void DoGenerateLookups()
  126. {
  127. if (!(Items?.FirstOrDefault() is DFLayoutLookupFieldProperties properties))
  128. return;
  129. if (!CoreUtils.TryGetEntity(properties.LookupType, out var type))
  130. return;
  131. var props = CoreUtils.PropertyList(type, x => x.GetCustomAttribute<DoNotSerialize>() == null, true).Keys.ToList();
  132. props.Sort();
  133. foreach (var prop in props)
  134. {
  135. AddValue(prop, prop);
  136. }
  137. }
  138. }
  139. }
  140. }