DynamicGridColumns.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using InABox.Configuration;
  6. using InABox.Core;
  7. namespace InABox.DynamicGrid;
  8. public class DynamicGridColumns : List<DynamicGridColumn>, IGlobalConfigurationSettings, IUserConfigurationSettings
  9. {
  10. public string GridName { get; set; }
  11. private static Dictionary<Type, List<DynamicGridColumn>> _columnscache = new Dictionary<Type, List<DynamicGridColumn>>();
  12. public DynamicGridColumns ExtractColumns(Type type)
  13. {
  14. if (!_columnscache.TryGetValue(type, out List<DynamicGridColumn>? value))
  15. {
  16. value = [];
  17. _columnscache[type] = value;
  18. var defaults = DefaultColumns.GetDefaultColumns(type);
  19. var properties = DatabaseSchema.Properties(type).OrderBy(x => x.Sequence);
  20. foreach (var prop in properties)
  21. {
  22. if ((prop.Editor != null && !(prop.Editor is NullEditor)) || prop.Required)
  23. {
  24. var defaultCol = defaults.FirstOrDefault(x => x.Property == prop);
  25. if(defaultCol is not null)
  26. {
  27. value.Add(DynamicGridColumn.FromCoreGridColumn(defaultCol));
  28. }
  29. else
  30. {
  31. value.Add(new(prop));
  32. }
  33. }
  34. }
  35. }
  36. AddRange(value);
  37. return this;
  38. }
  39. public static DynamicGridColumns ExtractColumns(Type type, IColumns columns)
  40. {
  41. var cols = new DynamicGridColumns();
  42. var master = new DynamicGridColumns().ExtractColumns(type);
  43. foreach (var col in columns)
  44. {
  45. var mc = master.FirstOrDefault(x => x.ColumnName.Equals(col.Property));
  46. if (mc != null && mc.Editor is not NullEditor && mc.Editor.Visible != Visible.Hidden)
  47. cols.Add(mc);
  48. }
  49. return cols;
  50. }
  51. public static DynamicGridColumn CreateColumn<TType>(
  52. Expression<Func<TType, object?>> member,
  53. int? width = null,
  54. string? caption = null,
  55. string? format = null,
  56. Alignment? alignment = null)
  57. {
  58. var prop = DatabaseSchema.Property(member);
  59. var col = new DynamicGridColumn
  60. {
  61. ColumnName = prop.Name,
  62. Width = width ?? prop.Editor.Width,
  63. Alignment = alignment ?? prop.Editor.Alignment,
  64. Format = format ?? prop.Editor.Format,
  65. Editor = prop.Editor.CloneEditor(),
  66. Caption = caption ?? prop.Caption
  67. };
  68. return col;
  69. }
  70. public DynamicGridColumn Add<TType>(
  71. Expression<Func<TType, object?>> member,
  72. int? width = null,
  73. string? caption = null,
  74. string? format = null,
  75. Alignment? alignment = null
  76. )
  77. {
  78. var col = CreateColumn(member, width: width, caption: caption, format: format, alignment: alignment);
  79. Add(col);
  80. return col;
  81. }
  82. public DynamicGridColumns AddFluent<TType>(
  83. Expression<Func<TType, object?>> member,
  84. int? width = null,
  85. string? caption = null,
  86. string? format = null,
  87. Alignment? alignment = null
  88. )
  89. {
  90. Add(member, width: width, caption: caption, format: format, alignment: alignment);
  91. return this;
  92. }
  93. }
  94. public class DynamicGridColumns<T> : DynamicGridColumns
  95. {
  96. public DynamicGridColumn Add(Expression<Func<T, object?>> member, int width, string caption, string format, Alignment alignment)
  97. {
  98. return Add<T>(member, width, caption, format, alignment);
  99. }
  100. public DynamicGridColumn Add(
  101. Expression<Func<T, object?>> member,
  102. int? width = null,
  103. string? caption = null,
  104. string? format = null,
  105. Alignment? alignment = null
  106. )
  107. {
  108. var col = CreateColumn(member, width: width, caption: caption, format: format, alignment: alignment);
  109. Add(col);
  110. return col;
  111. }
  112. public DynamicGridColumns<T> AddFluent(
  113. Expression<Func<T, object?>> member,
  114. int? width = null,
  115. string? caption = null,
  116. string? format = null,
  117. Alignment? alignment = null
  118. )
  119. {
  120. Add(member, width: width, caption: caption, format: format, alignment: alignment);
  121. return this;
  122. }
  123. }
  124. public class DynamicGridColumnGroup(string header, DynamicColumnBase start, DynamicColumnBase end, object? tag = null)
  125. {
  126. public string Header { get; set; } = header;
  127. public DynamicColumnBase StartColumn { get; set; } = start;
  128. public DynamicColumnBase EndColumn { get; set; } = end;
  129. public object? Tag { get; set; } = tag;
  130. }
  131. public class DynamicGridColumnGrouping
  132. {
  133. public List<DynamicGridColumnGroup> Groups = new();
  134. public DynamicGridColumnGrouping AddGroup(string header, DynamicColumnBase start, DynamicColumnBase end, object? tag = null)
  135. {
  136. Groups.Add(new(header, start, end, tag: tag));
  137. return this;
  138. }
  139. }
  140. public class DynamicGridColumnGroupings : List<DynamicGridColumnGrouping>
  141. {
  142. }