BaseEditor.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. namespace InABox.Core
  5. {
  6. public interface IBaseEditor
  7. {
  8. Visible Visible { get; set; }
  9. Editable Editable { get; set; }
  10. string ToolTip { get; set; }
  11. Alignment Alignment { get; set; }
  12. int Width { get; set; }
  13. string Format { get; set; }
  14. int ColumnSequence { get; set; }
  15. int EditorSequence { get; set; }
  16. string Caption { get; set; }
  17. string? Page { get; set; }
  18. Summary Summary { get; set; }
  19. SecurityAttribute[] Security { get; set; }
  20. Type? Information { get; set; }
  21. BaseEditor CloneEditor();
  22. public object Clone();
  23. }
  24. public interface IEditorInformation
  25. {
  26. string Display(BaseObject[] items);
  27. }
  28. public abstract class EditorInformation<T> : IEditorInformation where T : class
  29. {
  30. public abstract string GetInfo(T item);
  31. public string Display(BaseObject[] items)
  32. {
  33. var _results = items.OfType<T>().Select(GetInfo).ToArray();
  34. return _results.Length == 0
  35. ? ""
  36. : _results.Length == 1
  37. ? _results[0]
  38. : "(Multiple Values)";
  39. }
  40. }
  41. public abstract class BaseEditor : Attribute, IEnclosedEntity, ICloneable, IBaseEditor
  42. {
  43. private BaseObject _linkedParent;
  44. private string _linkedPath;
  45. public void SetLinkedParent(BaseObject parent)
  46. {
  47. _linkedParent = parent;
  48. }
  49. public void SetLinkedPath(string path)
  50. {
  51. _linkedPath = path;
  52. }
  53. public BaseObject GetLinkedParent() => _linkedParent;
  54. public string GetLinkedPath() => _linkedPath;
  55. protected BaseEditor()
  56. {
  57. Editable = Editable.Enabled;
  58. Visible = Visible.Optional;
  59. Alignment = Alignment.MiddleLeft;
  60. Width = 0;
  61. Format = "";
  62. ColumnSequence = 0;
  63. EditorSequence = 0;
  64. Caption = "";
  65. Summary = Summary.None;
  66. ToolTip = "";
  67. Security = Array.Empty<SecurityAttribute>();
  68. }
  69. [EnumLookupEditor(typeof(Visible))]
  70. public Visible Visible { get; set; }
  71. [EnumLookupEditor(typeof(Editable))]
  72. public Editable Editable { get; set; }
  73. [NullEditor]
  74. public string ToolTip { get; set; }
  75. [NullEditor]
  76. public Alignment Alignment { get; set; }
  77. [NullEditor]
  78. public int Width { get; set; }
  79. [NullEditor]
  80. public string Format { get; set; }
  81. [NullEditor]
  82. public int ColumnSequence { get; set; }
  83. [NullEditor]
  84. public int EditorSequence { get; set; }
  85. [NullEditor]
  86. public string Caption { get; set; }
  87. [NullEditor]
  88. public string? Page { get; set; }
  89. [NullEditor]
  90. public Summary Summary { get; set; }
  91. [NullEditor]
  92. public SecurityAttribute[] Security { get; set; }
  93. private Type? _information;
  94. public Type? Information
  95. {
  96. get => _information;
  97. set
  98. {
  99. if (value?.GetInterfaces(typeof(IEditorInformation)) != null)
  100. _information = value;
  101. else
  102. {
  103. if (value != null)
  104. throw new Exception($"{value.EntityName()} does not implement IEditorInformation!");
  105. }
  106. }
  107. }
  108. public BaseEditor CloneEditor()
  109. {
  110. var result = DoClone();
  111. result.Visible = Visible;
  112. result.Editable = Editable;
  113. result.Alignment = Alignment;
  114. result.Width = Width;
  115. result.Format = Format;
  116. result.ColumnSequence = ColumnSequence;
  117. result.EditorSequence = EditorSequence;
  118. result.Caption = Caption;
  119. result.Page = Page;
  120. result.Summary = Summary;
  121. result.ToolTip = ToolTip;
  122. result.Security = Security.Select(x => x.Clone()).ToArray();
  123. result.Information = Information;
  124. return result;
  125. }
  126. public object Clone() => CloneEditor();
  127. protected abstract BaseEditor DoClone();
  128. }
  129. }