DefaultColumns.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Text;
  7. namespace InABox.Core
  8. {
  9. public class CoreGridColumn
  10. {
  11. public IProperty Property { get; set; }
  12. public int Width { get; set; }
  13. public Alignment Alignment { get; set; }
  14. public string Format { get; set; }
  15. public BaseEditor Editor { get; set; }
  16. public string Caption { get; set; }
  17. public CoreGridColumn(IProperty property, int width, Alignment alignment, string format, BaseEditor editor, string caption)
  18. {
  19. Property = property;
  20. Width = width;
  21. Alignment = alignment;
  22. Format = format;
  23. Editor = editor;
  24. Caption = caption;
  25. }
  26. }
  27. /// <summary>
  28. /// This is the new system for managing default columns on grids. (This replaces <see cref="Visible.Default"/> on <see cref="BaseEditor.Visible"/>).
  29. /// </summary>
  30. public static class DefaultColumns
  31. {
  32. private static readonly Dictionary<Type, List<CoreGridColumn>> _columns = new Dictionary<Type, List<CoreGridColumn>>();
  33. public static IEnumerable<CoreGridColumn> GetDefaultColumns(Type T)
  34. {
  35. System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(T.TypeHandle);
  36. if(_columns.TryGetValue(T, out var cols))
  37. {
  38. return cols;
  39. }
  40. else
  41. {
  42. return Enumerable.Empty<CoreGridColumn>();
  43. }
  44. }
  45. public static IEnumerable<CoreGridColumn> GenerateColumns(Type T)
  46. {
  47. foreach(var property in DatabaseSchema.RootProperties(T))
  48. {
  49. if(property.Editor.Visible != Visible.Hidden && !property.IsParent)
  50. {
  51. yield return CreateColumn(property);
  52. }
  53. }
  54. }
  55. public static CoreGridColumn GetColumn(Type type, string column)
  56. {
  57. var prop = DatabaseSchema.PropertyStrict(type, column);
  58. var defaults = GetDefaultColumns(type);
  59. var defaultCol = defaults.FirstOrDefault(x => x.Property == prop);
  60. if(defaultCol != null)
  61. {
  62. return defaultCol;
  63. }
  64. return CreateColumn(prop);
  65. }
  66. public static CoreGridColumn CreateColumn(IProperty prop)
  67. {
  68. var col = new CoreGridColumn(
  69. prop,
  70. prop.Editor.Width,
  71. prop.Editor.Alignment,
  72. prop.Editor.Format,
  73. prop.Editor.CloneEditor(),
  74. prop.Caption);
  75. return col;
  76. }
  77. public static CoreGridColumn CreateColumn<TType, TProperty>(
  78. Expression<Func<TType, TProperty>> member,
  79. int? width = null,
  80. string? caption = null,
  81. string? format = null,
  82. Alignment? alignment = null)
  83. {
  84. var prop = DatabaseSchema.Property(member) ?? throw new Exception($"Could not find property {CoreUtils.GetFullPropertyName(member, ".")}");
  85. var col = new CoreGridColumn(
  86. prop,
  87. width ?? prop.Editor.Width,
  88. alignment ?? prop.Editor.Alignment,
  89. format ?? prop.Editor.Format,
  90. prop.Editor.CloneEditor(),
  91. caption ?? prop.Caption);
  92. return col;
  93. }
  94. public static CoreGridColumn Add<TType>(
  95. Expression<Func<TType, object?>> member,
  96. int? width = null,
  97. string? caption = null,
  98. string? format = null,
  99. Alignment? alignment = null
  100. )
  101. {
  102. var col = CreateColumn(member, width: width, caption: caption, format: format, alignment: alignment);
  103. var list = _columns.GetValueOrAdd(typeof(TType));
  104. list.Add(col);
  105. return col;
  106. }
  107. }
  108. }