DFLayoutFieldProperties.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. [Comment("Expression for the background colour of editor.")]
  43. [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.")]
  44. [EditorSequence(9)]
  45. public string ColourExpression { get; set; }
  46. public abstract string FormatValue(object? value);
  47. public abstract void Serialize(DFSaveStorageEntry entry, object? value);
  48. public abstract object? Deserialize(DFLoadStorageEntry entry);
  49. public abstract object? GetValue(object? value);
  50. public virtual IEnumerable<CoreColumn> GetAdditionalColumns()
  51. {
  52. return Enumerable.Empty<CoreColumn>();
  53. }
  54. public virtual IEnumerable<KeyValuePair<string, object?>> GetAdditionalValues(object? value)
  55. {
  56. return Enumerable.Empty<KeyValuePair<string, object?>>();
  57. }
  58. public abstract IEnumerable<CoreColumn> GetDisplayColumns();
  59. public abstract IEnumerable<KeyValuePair<string, object?>> GetDisplayValues(object? value);
  60. protected override void LoadProperties()
  61. {
  62. Description = GetProperty(nameof(Description), "");
  63. Property = GetProperty(nameof(Property), "");
  64. ReadOnlyProperty = GetProperty(nameof(ReadOnlyProperty), true);
  65. Required = GetProperty(nameof(Required), false);
  66. Secure = GetProperty(nameof(Secure), false);
  67. Retain = GetProperty(nameof(Retain), false);
  68. Expression = GetProperty(nameof(Expression), "");
  69. ColourExpression = GetProperty(nameof(ColourExpression), "");
  70. }
  71. protected override void SaveProperties()
  72. {
  73. SetProperty(nameof(Description), Description);
  74. SetProperty(nameof(Property), Property);
  75. SetProperty(nameof(ReadOnlyProperty), ReadOnlyProperty);
  76. SetProperty(nameof(Required), Required);
  77. SetProperty(nameof(Secure), Secure);
  78. SetProperty(nameof(Retain), Retain);
  79. SetProperty(nameof(Expression), Expression);
  80. SetProperty(nameof(ColourExpression), ColourExpression);
  81. }
  82. public abstract object? GetDefaultValue();
  83. private class PropertyLookupGenerator : LookupGenerator<object>
  84. {
  85. public PropertyLookupGenerator(object[] items) : base(items)
  86. {
  87. }
  88. protected override void DoGenerateLookups()
  89. {
  90. var form = Items?.FirstOrDefault() as DigitalForm;
  91. if (form != null)
  92. {
  93. var type = DFUtils.FormEntityType(form);
  94. if (type != null)
  95. {
  96. foreach(var prop in DatabaseSchema.Properties(type))
  97. {
  98. if (prop.Editor.Editable.IsEditable())
  99. {
  100. AddValue(prop.Name, prop.Name);
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. public abstract class DFLayoutFieldProperties<TValue, TSerialized> : DFLayoutFieldProperties
  109. {
  110. public DFLayoutFieldProperties()
  111. {
  112. Default = default;
  113. }
  114. [EditorSequence(-995)]
  115. public abstract TValue Default { get; set; }
  116. protected override void LoadProperties()
  117. {
  118. base.LoadProperties();
  119. Default = GetProperty("Default", default(TValue));
  120. }
  121. protected override void SaveProperties()
  122. {
  123. base.SaveProperties();
  124. SetProperty("Default", Default);
  125. }
  126. public sealed override void Serialize(DFSaveStorageEntry entry, object? value)
  127. {
  128. if(value is TSerialized ser)
  129. {
  130. SerializeValue(entry, ser);
  131. }
  132. }
  133. public sealed override object? Deserialize(DFLoadStorageEntry entry)
  134. {
  135. return DeserializeValue(entry);
  136. }
  137. public abstract void SerializeValue(DFSaveStorageEntry entry, TSerialized value);
  138. public abstract TSerialized DeserializeValue(DFLoadStorageEntry entry);
  139. public sealed override string FormatValue(object? value)
  140. {
  141. if (value is TSerialized ser)
  142. {
  143. return FormatValue(ser);
  144. }
  145. else
  146. {
  147. return "";
  148. }
  149. }
  150. public abstract string FormatValue(TSerialized value);
  151. public sealed override object? GetValue(object? value)
  152. {
  153. if (value is TSerialized ser)
  154. {
  155. return GetValue(ser);
  156. }
  157. else
  158. {
  159. return null;
  160. }
  161. }
  162. public abstract TValue GetValue(TSerialized value);
  163. public sealed override IEnumerable<KeyValuePair<string, object?>> GetAdditionalValues(object? value)
  164. {
  165. if(value is TSerialized ser)
  166. {
  167. return GetAdditionalValues(ser);
  168. }
  169. return Enumerable.Empty<KeyValuePair<string, object?>>();
  170. }
  171. public virtual IEnumerable<KeyValuePair<string, object?>> GetAdditionalValues(TSerialized value)
  172. {
  173. return Enumerable.Empty<KeyValuePair<string, object?>>();
  174. }
  175. public override IEnumerable<CoreColumn> GetDisplayColumns()
  176. {
  177. yield return new CoreColumn
  178. {
  179. ColumnName = Code,
  180. DataType = typeof(TValue)
  181. };
  182. foreach(var col in GetAdditionalColumns())
  183. {
  184. yield return col;
  185. }
  186. }
  187. public sealed override IEnumerable<KeyValuePair<string, object?>> GetDisplayValues(object? value)
  188. {
  189. if (value is TSerialized ser)
  190. {
  191. return GetDisplayValues(ser);
  192. }
  193. return Enumerable.Empty<KeyValuePair<string, object?>>();
  194. }
  195. public virtual IEnumerable<KeyValuePair<string, object?>> GetDisplayValues(TSerialized value)
  196. {
  197. yield return new KeyValuePair<string, object?>(Code, value);
  198. foreach (var pair in GetAdditionalValues(value))
  199. {
  200. yield return pair;
  201. }
  202. }
  203. public sealed override object? GetDefaultValue()
  204. {
  205. return Default;
  206. }
  207. }
  208. }