DFLayoutFieldProperties.cs 4.7 KB

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