DFLayoutFieldProperties.cs 5.5 KB

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