DFLayoutFieldProperties.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. [ExpressionEditor(null)]
  28. [EditorSequence(7)]
  29. public string Expression { get; set; }
  30. /// <summary>
  31. /// An expression that sets the field to have a background colour of its result.
  32. /// 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"/>,
  33. /// like 'Yellow'.
  34. /// </summary>
  35. [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.")]
  36. [EditorSequence(7)]
  37. public string ColourExpression { get; set; }
  38. public abstract object? ParseValue(object? value);
  39. public abstract string FormatValue(object? value);
  40. public virtual IEnumerable<CoreColumn> GetAdditionalColumns()
  41. {
  42. return Enumerable.Empty<CoreColumn>();
  43. }
  44. protected override void LoadProperties()
  45. {
  46. Description = GetProperty("Description", "");
  47. Property = GetProperty("Property", "");
  48. Required = GetProperty("Required", false);
  49. Secure = GetProperty("Secure", false);
  50. Retain = GetProperty("Retain", false);
  51. Expression = GetProperty("Expression", "");
  52. ColourExpression = GetProperty("ColourExpression", "");
  53. }
  54. protected override void SaveProperties()
  55. {
  56. SetProperty("Description", Description);
  57. SetProperty("Property", Property);
  58. SetProperty("Required", Required);
  59. SetProperty("Secure", Secure);
  60. SetProperty("Retain", Retain);
  61. SetProperty("Expression", Expression);
  62. SetProperty("ColourExpression", ColourExpression);
  63. }
  64. private class PropertyLookupGenerator : LookupGenerator<object>
  65. {
  66. public PropertyLookupGenerator(object[] items) : base(items)
  67. {
  68. }
  69. protected override void DoGenerateLookups()
  70. {
  71. var form = Items?.FirstOrDefault() as DigitalForm;
  72. if (form != null)
  73. {
  74. var type = CoreUtils.TypeList(
  75. AppDomain.CurrentDomain.GetAssemblies(),
  76. x => string.Equals(x.Name, form.AppliesTo)
  77. ).FirstOrDefault();
  78. if (type != null)
  79. {
  80. var props = CoreUtils.PropertyList(type, x => true, true);
  81. foreach (var prop in props.Keys)
  82. {
  83. var info = CoreUtils.GetProperty(type, prop);
  84. var editor = info.GetEditor();
  85. if (editor != null && editor.Editable == Editable.Enabled)
  86. {
  87. var bOK = true;
  88. if (prop.Contains("."))
  89. {
  90. var comps = prop.Split('.').Reverse().Skip(1).Reverse().ToList();
  91. while (comps.Count > 0)
  92. {
  93. var parent = string.Join(".", comps);
  94. comps.Remove(comps.Last());
  95. var parentinfo = CoreUtils.GetProperty(type, parent);
  96. var parenteditor = parentinfo.GetEditor();
  97. if (parenteditor is NullEditor)
  98. bOK = false;
  99. }
  100. }
  101. if (bOK)
  102. AddValue(prop, prop);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. public abstract class DFLayoutFieldProperties<T> : DFLayoutFieldProperties
  111. {
  112. public DFLayoutFieldProperties()
  113. {
  114. Default = default;
  115. }
  116. [EditorSequence(-995)]
  117. public T Default { get; set; }
  118. protected override void LoadProperties()
  119. {
  120. base.LoadProperties();
  121. Default = GetProperty("Default", default(T));
  122. }
  123. protected override void SaveProperties()
  124. {
  125. base.SaveProperties();
  126. SetProperty("Default", Default);
  127. }
  128. }
  129. }