DFLayoutFieldProperties.cs 9.4 KB

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