DynamicGridColumns.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 DynamicGridColumn Insert<TType>(
  83. int index,
  84. Expression<Func<TType, object?>> member,
  85. int? width = null,
  86. string? caption = null,
  87. string? format = null,
  88. Alignment? alignment = null
  89. )
  90. {
  91. var col = CreateColumn(member, width: width, caption: caption, format: format, alignment: alignment);
  92. Insert(index,col);
  93. return col;
  94. }
  95. public DynamicGridColumns AddFluent<TType>(
  96. Expression<Func<TType, object?>> member,
  97. int? width = null,
  98. string? caption = null,
  99. string? format = null,
  100. Alignment? alignment = null
  101. )
  102. {
  103. Add(member, width: width, caption: caption, format: format, alignment: alignment);
  104. return this;
  105. }
  106. }
  107. public class DynamicGridColumns<T> : DynamicGridColumns
  108. {
  109. public DynamicGridColumn Add(Expression<Func<T, object?>> member, int width, string caption, string format, Alignment alignment)
  110. {
  111. return Add<T>(member, width, caption, format, alignment);
  112. }
  113. public DynamicGridColumn Add(
  114. Expression<Func<T, object?>> member,
  115. int? width = null,
  116. string? caption = null,
  117. string? format = null,
  118. Alignment? alignment = null
  119. )
  120. {
  121. var col = CreateColumn(member, width: width, caption: caption, format: format, alignment: alignment);
  122. Add(col);
  123. return col;
  124. }
  125. public DynamicGridColumns<T> AddFluent(
  126. Expression<Func<T, object?>> member,
  127. int? width = null,
  128. string? caption = null,
  129. string? format = null,
  130. Alignment? alignment = null
  131. )
  132. {
  133. Add(member, width: width, caption: caption, format: format, alignment: alignment);
  134. return this;
  135. }
  136. }
  137. public class DynamicGridColumnGroup(string header, DynamicColumnBase start, DynamicColumnBase end, object? tag = null)
  138. {
  139. public string Header { get; set; } = header;
  140. public DynamicColumnBase StartColumn { get; set; } = start;
  141. public DynamicColumnBase EndColumn { get; set; } = end;
  142. public object? Tag { get; set; } = tag;
  143. }
  144. public class DynamicGridColumnGrouping
  145. {
  146. public List<DynamicGridColumnGroup> Groups = new();
  147. public DynamicGridColumnGrouping AddGroup(string header, DynamicColumnBase start, DynamicColumnBase end, object? tag = null)
  148. {
  149. Groups.Add(new(header, start, end, tag: tag));
  150. return this;
  151. }
  152. }
  153. public class DynamicGridColumnGroupings : List<DynamicGridColumnGrouping>
  154. {
  155. }