CustomPropertyGrid.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.DynamicGrid;
  9. namespace PRSDesktop.Configuration
  10. {
  11. internal class CustomPropertyGrid : DynamicDataGrid<CustomProperty>
  12. {
  13. protected override void Init()
  14. {
  15. base.Init();
  16. HiddenColumns.Add(x => x.Class);
  17. HiddenColumns.Add(x => x.Type);
  18. }
  19. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  20. {
  21. base.DoReconfigure(options);
  22. options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns);
  23. }
  24. protected override void DoValidate(CustomProperty[] items, List<string> errors)
  25. {
  26. base.DoValidate(items, errors);
  27. if (items.Any(x => string.IsNullOrWhiteSpace(x.Class)))
  28. errors.Add("[Class] must not be blank!");
  29. if (items.Any(x => string.IsNullOrWhiteSpace(x.Name)))
  30. errors.Add("[Name] must not be blank!");
  31. if (items.Any(x => string.IsNullOrWhiteSpace(x.Type)))
  32. errors.Add("[Type] must not be blank!");
  33. }
  34. private IEnumerable<Type> FindTypes(Func<Type, bool> predicate)
  35. {
  36. if (predicate == null)
  37. throw new ArgumentNullException(nameof(predicate));
  38. foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
  39. if (!assembly.IsDynamic)
  40. {
  41. Type[] exportedTypes = null;
  42. try
  43. {
  44. exportedTypes = assembly.GetExportedTypes();
  45. }
  46. catch (ReflectionTypeLoadException e)
  47. {
  48. exportedTypes = e.Types;
  49. }
  50. if (exportedTypes != null)
  51. foreach (var type in exportedTypes)
  52. if (predicate(type))
  53. yield return type;
  54. }
  55. }
  56. public override void ConfigureColumns(DynamicGridColumns columns /*, bool dolookups = true */)
  57. {
  58. base.ConfigureColumns(columns /*, dolookups */);
  59. var column = columns.FirstOrDefault(x => x.ColumnName.Equals("Class"));
  60. if (column != null)
  61. {
  62. column.Lookups.Clear();
  63. var classes = CoreUtils.TypeList(
  64. new[]
  65. {
  66. typeof(Setout).Assembly
  67. },
  68. myType =>
  69. myType.IsClass
  70. && !myType.IsAbstract
  71. && !myType.IsGenericType
  72. && myType.IsSubclassOf(typeof(Entity))
  73. && myType.GetInterfaces().Contains(typeof(IPersistent))
  74. ).OrderBy(x => x.EntityName()).ToArray();
  75. foreach (var entity in classes)
  76. if (ClientFactory.IsSupported(entity))
  77. column.Lookups[entity.EntityName()] = entity;
  78. }
  79. column = columns.FirstOrDefault(x => x.ColumnName.Equals("Type"));
  80. if (column != null)
  81. {
  82. column.Lookups.Clear();
  83. column.Lookups[typeof(string).EntityName()] = typeof(string).EntityName();
  84. column.Lookups[typeof(int).EntityName()] = typeof(int).EntityName();
  85. column.Lookups[typeof(double).EntityName()] = typeof(double).EntityName();
  86. column.Lookups[typeof(bool).EntityName()] = typeof(bool).EntityName();
  87. column.Lookups[typeof(DateTime).EntityName()] = typeof(DateTime).EntityName();
  88. }
  89. }
  90. protected override void Reload(Filters<CustomProperty> criteria, Columns<CustomProperty> columns, ref SortOrder<CustomProperty>? sort, Action<CoreTable?, Exception?> action)
  91. {
  92. base.Reload(criteria, columns, ref sort, action);
  93. }
  94. }
  95. }