DFLayoutFieldProperties.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Linq;
  3. namespace InABox.Core
  4. {
  5. // When creating a new field with properties, make sure to override LoadProperties() and SaveProperties()
  6. public abstract class DFLayoutFieldProperties : DFLayoutObject
  7. {
  8. [EditorSequence(-999)]
  9. [CodeEditor(Editable = Editable.Enabled, ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789")]
  10. public string Code { get; set; }
  11. [EditorSequence(-998)]
  12. [TextBoxEditor]
  13. public string Description { get; set; }
  14. [EditorSequence(-997)]
  15. [ComboLookupEditor(typeof(PropertyLookupGenerator))]
  16. public string Property { get; set; }
  17. [CheckBoxEditor]
  18. [EditorSequence(4)]
  19. public bool Required { get; set; }
  20. [CheckBoxEditor]
  21. [EditorSequence(5)]
  22. public bool Secure { get; set; }
  23. [CheckBoxEditor]
  24. [EditorSequence(6)]
  25. public bool Retain { get; set; }
  26. [TextBoxEditor]
  27. [EditorSequence(7)]
  28. public string Expression { get; set; }
  29. public abstract object? ParseValue(object value);
  30. public abstract string FormatValue(object value);
  31. protected override void LoadProperties()
  32. {
  33. Description = GetProperty("Description", "");
  34. Property = GetProperty("Property", "");
  35. Required = GetProperty("Required", false);
  36. Secure = GetProperty("Secure", false);
  37. Retain = GetProperty("Retain", false);
  38. Expression = GetProperty("Expression", "");
  39. }
  40. protected override void SaveProperties()
  41. {
  42. SetProperty("Description", Description);
  43. SetProperty("Property", Property);
  44. SetProperty("Required", Required);
  45. SetProperty("Secure", Secure);
  46. SetProperty("Retain", Retain);
  47. SetProperty("Expression", Expression);
  48. }
  49. private class PropertyLookupGenerator : LookupGenerator<object>
  50. {
  51. public PropertyLookupGenerator(object[] items) : base(items)
  52. {
  53. }
  54. protected override void DoGenerateLookups()
  55. {
  56. var form = Items?.FirstOrDefault() as DigitalForm;
  57. if (form != null)
  58. {
  59. var type = CoreUtils.TypeList(
  60. AppDomain.CurrentDomain.GetAssemblies(),
  61. x => string.Equals(x.Name, form.AppliesTo)
  62. ).FirstOrDefault();
  63. if (type != null)
  64. {
  65. var props = CoreUtils.PropertyList(type, x => true, true);
  66. foreach (var prop in props.Keys)
  67. {
  68. var info = CoreUtils.GetProperty(type, prop);
  69. var editor = info.GetEditor();
  70. if (editor != null && editor.Editable == Editable.Enabled)
  71. {
  72. var bOK = true;
  73. if (prop.Contains("."))
  74. {
  75. var comps = prop.Split('.').Reverse().Skip(1).Reverse().ToList();
  76. while (comps.Count > 0)
  77. {
  78. var parent = string.Join(".", comps);
  79. comps.Remove(comps.Last());
  80. var parentinfo = CoreUtils.GetProperty(type, parent);
  81. var parenteditor = parentinfo.GetEditor();
  82. if (parenteditor is NullEditor)
  83. bOK = false;
  84. }
  85. }
  86. if (bOK)
  87. AddValue(prop, prop);
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. public abstract class DFLayoutFieldProperties<T> : DFLayoutFieldProperties
  96. {
  97. public DFLayoutFieldProperties()
  98. {
  99. Default = default;
  100. }
  101. [EditorSequence(-995)]
  102. public T Default { get; set; }
  103. protected override void LoadProperties()
  104. {
  105. base.LoadProperties();
  106. Default = GetProperty("Default", default(T));
  107. }
  108. protected override void SaveProperties()
  109. {
  110. base.SaveProperties();
  111. SetProperty("Default", Default);
  112. }
  113. }
  114. }