DefaultColumns.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 CreateColumn(IProperty prop)
  56. {
  57. var col = new CoreGridColumn(
  58. prop,
  59. prop.Editor.Width,
  60. prop.Editor.Alignment,
  61. prop.Editor.Format,
  62. prop.Editor.CloneEditor(),
  63. prop.Caption);
  64. return col;
  65. }
  66. public static CoreGridColumn CreateColumn<TType, TProperty>(
  67. Expression<Func<TType, TProperty>> member,
  68. int? width = null,
  69. string? caption = null,
  70. string? format = null,
  71. Alignment? alignment = null)
  72. {
  73. var prop = DatabaseSchema.Property(member) ?? throw new Exception($"Could not find property {CoreUtils.GetFullPropertyName(member, ".")}");
  74. var col = new CoreGridColumn(
  75. prop,
  76. width ?? prop.Editor.Width,
  77. alignment ?? prop.Editor.Alignment,
  78. format ?? prop.Editor.Format,
  79. prop.Editor.CloneEditor(),
  80. caption ?? prop.Caption);
  81. return col;
  82. }
  83. public static CoreGridColumn Add<TType>(
  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. var list = _columns.GetValueOrAdd(typeof(TType));
  93. list.Add(col);
  94. return col;
  95. }
  96. }
  97. }