DFLayoutFieldProperties.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace InABox.Core
  5. {
  6. // When creating a new field with properties, make sure to override LoadProperties() and SaveProperties()
  7. public abstract class DFLayoutFieldProperties : DFLayoutObject
  8. {
  9. [EditorSequence(-999)]
  10. [CodeEditor(Editable = Editable.Enabled, ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789")]
  11. public string Code { get; set; }
  12. [EditorSequence(-998)]
  13. [TextBoxEditor]
  14. public string Description { get; set; }
  15. [EditorSequence(-997)]
  16. [ComboLookupEditor(typeof(PropertyLookupGenerator))]
  17. public string Property { get; set; }
  18. [CheckBoxEditor]
  19. [EditorSequence(4)]
  20. public bool Required { get; set; }
  21. [CheckBoxEditor]
  22. [EditorSequence(5)]
  23. public bool Secure { get; set; }
  24. [CheckBoxEditor]
  25. [EditorSequence(6)]
  26. public bool Retain { get; set; }
  27. [CheckBoxEditor]
  28. [EditorSequence(7)]
  29. public bool Hidden { get; set; }
  30. [ExpressionEditor(null)]
  31. [EditorSequence(8)]
  32. public string Expression { get; set; }
  33. /// <summary>
  34. /// An expression that sets the field to have a background colour of its result.
  35. /// The result of the expression should be either a hex code like #rrggbb or #aarrggbb, or the string name of a <see cref="System.Drawing.KnownColor"/>,
  36. /// like 'Yellow'.
  37. /// </summary>
  38. [ExpressionEditor(null, ToolTip = "Evalutes to either a hex code (#RRGGBB or #AARRGGBB) or a colour name (like 'Red' or 'Yellow'), which sets the background colour for this variable.")]
  39. [EditorSequence(9)]
  40. public string ColourExpression { get; set; }
  41. public abstract string FormatValue(object? value);
  42. public abstract void Serialize(DFSaveStorageEntry entry, object? value);
  43. public abstract object? Deserialize(DFLoadStorageEntry entry);
  44. public abstract object? GetValue(object? value);
  45. public virtual IEnumerable<CoreColumn> GetAdditionalColumns()
  46. {
  47. return Enumerable.Empty<CoreColumn>();
  48. }
  49. public virtual IEnumerable<KeyValuePair<string, object?>> GetAdditionalValues(object? value)
  50. {
  51. return Enumerable.Empty<KeyValuePair<string, object?>>();
  52. }
  53. public abstract IEnumerable<CoreColumn> GetDisplayColumns();
  54. public abstract IEnumerable<KeyValuePair<string, object?>> GetDisplayValues(object? value);
  55. protected override void LoadProperties()
  56. {
  57. Description = GetProperty(nameof(Description), "");
  58. Property = GetProperty(nameof(Property), "");
  59. Required = GetProperty(nameof(Required), false);
  60. Secure = GetProperty(nameof(Secure), false);
  61. Retain = GetProperty(nameof(Retain), false);
  62. Expression = GetProperty(nameof(Expression), "");
  63. ColourExpression = GetProperty(nameof(ColourExpression), "");
  64. }
  65. protected override void SaveProperties()
  66. {
  67. SetProperty(nameof(Description), Description);
  68. SetProperty(nameof(Property), Property);
  69. SetProperty(nameof(Required), Required);
  70. SetProperty(nameof(Secure), Secure);
  71. SetProperty(nameof(Retain), Retain);
  72. SetProperty(nameof(Expression), Expression);
  73. SetProperty(nameof(ColourExpression), ColourExpression);
  74. }
  75. private class PropertyLookupGenerator : LookupGenerator<object>
  76. {
  77. public PropertyLookupGenerator(object[] items) : base(items)
  78. {
  79. }
  80. protected override void DoGenerateLookups()
  81. {
  82. var form = Items?.FirstOrDefault() as DigitalForm;
  83. if (form != null)
  84. {
  85. var type = CoreUtils.TypeList(
  86. AppDomain.CurrentDomain.GetAssemblies(),
  87. x => string.Equals(x.Name, form.AppliesTo)
  88. ).FirstOrDefault();
  89. if (type != null)
  90. {
  91. var props = CoreUtils.PropertyList(type, x => true, true);
  92. foreach (var prop in props.Keys)
  93. {
  94. var info = CoreUtils.GetProperty(type, prop);
  95. var editor = info.GetEditor();
  96. if (editor != null && editor.Editable.IsEditable())
  97. {
  98. var bOK = true;
  99. if (prop.Contains("."))
  100. {
  101. var comps = prop.Split('.').Reverse().Skip(1).Reverse().ToList();
  102. while (comps.Count > 0)
  103. {
  104. var parent = string.Join(".", comps);
  105. comps.Remove(comps.Last());
  106. var parentinfo = CoreUtils.GetProperty(type, parent);
  107. var parenteditor = parentinfo.GetEditor();
  108. if (parenteditor is NullEditor)
  109. bOK = false;
  110. }
  111. }
  112. if (bOK)
  113. AddValue(prop, prop);
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. public abstract class DFLayoutFieldProperties<TValue, TSerialized> : DFLayoutFieldProperties
  122. {
  123. public DFLayoutFieldProperties()
  124. {
  125. Default = default;
  126. }
  127. [EditorSequence(-995)]
  128. public TValue Default { get; set; }
  129. protected override void LoadProperties()
  130. {
  131. base.LoadProperties();
  132. Default = GetProperty("Default", default(TValue));
  133. }
  134. protected override void SaveProperties()
  135. {
  136. base.SaveProperties();
  137. SetProperty("Default", Default);
  138. }
  139. public sealed override void Serialize(DFSaveStorageEntry entry, object? value)
  140. {
  141. if(value is TSerialized ser)
  142. {
  143. SerializeValue(entry, ser);
  144. }
  145. }
  146. public sealed override object? Deserialize(DFLoadStorageEntry entry)
  147. {
  148. return DeserializeValue(entry);
  149. }
  150. public abstract void SerializeValue(DFSaveStorageEntry entry, TSerialized value);
  151. public abstract TSerialized DeserializeValue(DFLoadStorageEntry entry);
  152. public sealed override string FormatValue(object? value)
  153. {
  154. if (value is TSerialized ser)
  155. {
  156. return FormatValue(ser);
  157. }
  158. else
  159. {
  160. return "";
  161. }
  162. }
  163. public abstract string FormatValue(TSerialized value);
  164. public sealed override object? GetValue(object? value)
  165. {
  166. if (value is TSerialized ser)
  167. {
  168. return GetValue(ser);
  169. }
  170. else
  171. {
  172. return null;
  173. }
  174. }
  175. public abstract TValue GetValue(TSerialized value);
  176. public sealed override IEnumerable<KeyValuePair<string, object?>> GetAdditionalValues(object? value)
  177. {
  178. if(value is TSerialized ser)
  179. {
  180. return GetAdditionalValues(ser);
  181. }
  182. return Enumerable.Empty<KeyValuePair<string, object?>>();
  183. }
  184. public virtual IEnumerable<KeyValuePair<string, object?>> GetAdditionalValues(TSerialized value)
  185. {
  186. return Enumerable.Empty<KeyValuePair<string, object?>>();
  187. }
  188. public override IEnumerable<CoreColumn> GetDisplayColumns()
  189. {
  190. yield return new CoreColumn
  191. {
  192. ColumnName = Code,
  193. DataType = typeof(TValue)
  194. };
  195. foreach(var col in GetAdditionalColumns())
  196. {
  197. yield return col;
  198. }
  199. }
  200. public sealed override IEnumerable<KeyValuePair<string, object?>> GetDisplayValues(object? value)
  201. {
  202. if (value is TSerialized ser)
  203. {
  204. return GetDisplayValues(ser);
  205. }
  206. return Enumerable.Empty<KeyValuePair<string, object?>>();
  207. }
  208. public virtual IEnumerable<KeyValuePair<string, object?>> GetDisplayValues(TSerialized value)
  209. {
  210. yield return new KeyValuePair<string, object?>(Code, value);
  211. foreach (var pair in GetAdditionalValues(value))
  212. {
  213. yield return pair;
  214. }
  215. }
  216. }
  217. }