DFLayoutFieldProperties.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. try
  107. {
  108. var parentinfo = CoreUtils.GetProperty(type, parent);
  109. var parenteditor = parentinfo.GetEditor();
  110. if (parenteditor is NullEditor)
  111. bOK = false;
  112. }
  113. catch (Exception e)
  114. {
  115. bOK = false;
  116. }
  117. }
  118. }
  119. if (bOK)
  120. AddValue(prop, prop);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. public abstract class DFLayoutFieldProperties<TValue, TSerialized> : DFLayoutFieldProperties
  129. {
  130. public DFLayoutFieldProperties()
  131. {
  132. Default = default;
  133. }
  134. [EditorSequence(-995)]
  135. public TValue Default { get; set; }
  136. protected override void LoadProperties()
  137. {
  138. base.LoadProperties();
  139. Default = GetProperty("Default", default(TValue));
  140. }
  141. protected override void SaveProperties()
  142. {
  143. base.SaveProperties();
  144. SetProperty("Default", Default);
  145. }
  146. public sealed override void Serialize(DFSaveStorageEntry entry, object? value)
  147. {
  148. if(value is TSerialized ser)
  149. {
  150. SerializeValue(entry, ser);
  151. }
  152. }
  153. public sealed override object? Deserialize(DFLoadStorageEntry entry)
  154. {
  155. return DeserializeValue(entry);
  156. }
  157. public abstract void SerializeValue(DFSaveStorageEntry entry, TSerialized value);
  158. public abstract TSerialized DeserializeValue(DFLoadStorageEntry entry);
  159. public sealed override string FormatValue(object? value)
  160. {
  161. if (value is TSerialized ser)
  162. {
  163. return FormatValue(ser);
  164. }
  165. else
  166. {
  167. return "";
  168. }
  169. }
  170. public abstract string FormatValue(TSerialized value);
  171. public sealed override object? GetValue(object? value)
  172. {
  173. if (value is TSerialized ser)
  174. {
  175. return GetValue(ser);
  176. }
  177. else
  178. {
  179. return null;
  180. }
  181. }
  182. public abstract TValue GetValue(TSerialized value);
  183. public sealed override IEnumerable<KeyValuePair<string, object?>> GetAdditionalValues(object? value)
  184. {
  185. if(value is TSerialized ser)
  186. {
  187. return GetAdditionalValues(ser);
  188. }
  189. return Enumerable.Empty<KeyValuePair<string, object?>>();
  190. }
  191. public virtual IEnumerable<KeyValuePair<string, object?>> GetAdditionalValues(TSerialized value)
  192. {
  193. return Enumerable.Empty<KeyValuePair<string, object?>>();
  194. }
  195. public override IEnumerable<CoreColumn> GetDisplayColumns()
  196. {
  197. yield return new CoreColumn
  198. {
  199. ColumnName = Code,
  200. DataType = typeof(TValue)
  201. };
  202. foreach(var col in GetAdditionalColumns())
  203. {
  204. yield return col;
  205. }
  206. }
  207. public sealed override IEnumerable<KeyValuePair<string, object?>> GetDisplayValues(object? value)
  208. {
  209. if (value is TSerialized ser)
  210. {
  211. return GetDisplayValues(ser);
  212. }
  213. return Enumerable.Empty<KeyValuePair<string, object?>>();
  214. }
  215. public virtual IEnumerable<KeyValuePair<string, object?>> GetDisplayValues(TSerialized value)
  216. {
  217. yield return new KeyValuePair<string, object?>(Code, value);
  218. foreach (var pair in GetAdditionalValues(value))
  219. {
  220. yield return pair;
  221. }
  222. }
  223. }
  224. }