DFLayoutLookupFieldProperties.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. [EditorSequence(-995)]
  15. [NullEditor]
  16. public override Guid Default { get; set; }
  17. [DoNotSerialize]
  18. [NullEditor]
  19. public List<string> AdditionalPropertiesList = new List<string>();
  20. [ComboLookupEditor(typeof(PropertyClassLookups))]
  21. [EditorSequence(100)]
  22. public string LookupType { get; set; } = "";
  23. // A JSON-serialized Filter for filtering entries in the lookup editor
  24. [FilterEditor]
  25. [EditorSequence(101)]
  26. public string Filter { get; set; } = "";
  27. /// <summary>
  28. /// A list of comma-separated property names to also pull with the lookup.
  29. /// </summary>
  30. //[TextBoxEditor(ToolTip = "A comma-separated list of property names.")]
  31. [MultiLookupEditor(typeof(AdditionalPropertyLookupGenerator))]
  32. [EditorSequence(102)]
  33. public string AdditionalProperties
  34. {
  35. get => string.Join(',', AdditionalPropertiesList);
  36. set
  37. {
  38. AdditionalPropertiesList = value.Split(',').ToList();
  39. }
  40. }
  41. [EnumLookupEditor(typeof(DFLayoutLookupDisplayType))]
  42. [EditorSequence(103)]
  43. public DFLayoutLookupDisplayType DisplayType { get; set; }
  44. public DFLayoutLookupFieldProperties()
  45. {
  46. DisplayType = DFLayoutLookupDisplayType.Button;
  47. }
  48. public override string FormatValue(DFLayoutLookupValue value)
  49. {
  50. return value.Text;
  51. }
  52. public override Guid GetValue(DFLayoutLookupValue value)
  53. {
  54. return value.ID;
  55. }
  56. public override DFLayoutLookupValue DeserializeValue(DFLoadStorageEntry entry)
  57. {
  58. var value = new DFLayoutLookupValue();
  59. value.Deserialize(entry);
  60. return value;
  61. }
  62. public override void SerializeValue(DFSaveStorageEntry entry, DFLayoutLookupValue value)
  63. {
  64. value.Serialize(entry);
  65. }
  66. public override IEnumerable<CoreColumn> GetAdditionalColumns()
  67. {
  68. if(!CoreUtils.TryGetEntity(LookupType, out var entity))
  69. return Enumerable.Empty<CoreColumn>();
  70. return AdditionalPropertiesList.Select(x =>
  71. {
  72. if (CoreUtils.TryGetProperty(entity, x, out var property))
  73. return new CoreColumn
  74. {
  75. ColumnName = $"{Code}.{x}",
  76. DataType = property.PropertyType
  77. };
  78. return null;
  79. }).Where(x => x != null).Cast<CoreColumn>();
  80. }
  81. public override IEnumerable<KeyValuePair<string, object?>> GetAdditionalValues(DFLayoutLookupValue value)
  82. {
  83. if (!CoreUtils.TryGetEntity(LookupType, out var entity))
  84. return Enumerable.Empty<KeyValuePair<string, object?>>();
  85. return AdditionalPropertiesList
  86. .Where(x => CoreUtils.TryGetProperty(entity, x, out var property))
  87. .Select(x => new KeyValuePair<string, object?>($"{Code}.{x}", value.Values.GetValueOrDefault(x)));
  88. }
  89. public override IEnumerable<CoreColumn> GetDisplayColumns()
  90. {
  91. yield return new CoreColumn { ColumnName = Code, DataType = typeof(string) };
  92. yield return new CoreColumn { ColumnName = $"{Code}.ID", DataType = typeof(Guid) };
  93. foreach(var col in GetAdditionalColumns())
  94. {
  95. yield return col;
  96. }
  97. }
  98. public override IEnumerable<KeyValuePair<string, object?>> GetDisplayValues(DFLayoutLookupValue value)
  99. {
  100. yield return new KeyValuePair<string, object?>(Code, value.Text);
  101. yield return new KeyValuePair<string, object?>($"{Code}.ID", value.ID);
  102. foreach (var pair in GetAdditionalValues(value))
  103. {
  104. yield return pair;
  105. }
  106. }
  107. protected override void LoadProperties()
  108. {
  109. base.LoadProperties();
  110. LookupType = GetProperty(nameof(LookupType), "");
  111. Filter = GetProperty(nameof(Filter), "");
  112. AdditionalProperties = GetProperty(nameof(AdditionalProperties), "");
  113. DisplayType = GetProperty(nameof(DisplayType), DFLayoutLookupDisplayType.Button);
  114. }
  115. protected override void SaveProperties()
  116. {
  117. base.SaveProperties();
  118. SetProperty(nameof(LookupType), LookupType);
  119. SetProperty(nameof(Filter), Filter);
  120. SetProperty(nameof(AdditionalProperties), AdditionalProperties);
  121. SetProperty(nameof(DisplayType),DisplayType);
  122. }
  123. private class AdditionalPropertyLookupGenerator : LookupGenerator<object>
  124. {
  125. public AdditionalPropertyLookupGenerator(object[] items) : base(items)
  126. {
  127. }
  128. protected override void DoGenerateLookups()
  129. {
  130. if (!(Items?.FirstOrDefault() is DFLayoutLookupFieldProperties properties))
  131. return;
  132. if (!CoreUtils.TryGetEntity(properties.LookupType, out var type))
  133. return;
  134. var props = CoreUtils.PropertyList(type, x => x.GetCustomAttribute<DoNotSerialize>() == null, true).Keys.ToList();
  135. props.Sort();
  136. foreach (var prop in props)
  137. {
  138. AddValue(prop, prop);
  139. }
  140. }
  141. }
  142. }
  143. }