CustomPropertyGrid.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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(DynamicGridOptions options)
  20. {
  21. base.DoReconfigure(options);
  22. options.RecordCount = true;
  23. options.SelectColumns = true;
  24. }
  25. protected override void DoValidate(CustomProperty[] items, List<string> errors)
  26. {
  27. base.DoValidate(items, errors);
  28. if (items.Any(x => string.IsNullOrWhiteSpace(x.Class)))
  29. errors.Add("[Class] must not be blank!");
  30. if (items.Any(x => string.IsNullOrWhiteSpace(x.Name)))
  31. errors.Add("[Name] must not be blank!");
  32. if (items.Any(x => string.IsNullOrWhiteSpace(x.Type)))
  33. errors.Add("[Type] must not be blank!");
  34. }
  35. private IEnumerable<Type> FindTypes(Func<Type, bool> predicate)
  36. {
  37. if (predicate == null)
  38. throw new ArgumentNullException(nameof(predicate));
  39. foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
  40. if (!assembly.IsDynamic)
  41. {
  42. Type[] exportedTypes = null;
  43. try
  44. {
  45. exportedTypes = assembly.GetExportedTypes();
  46. }
  47. catch (ReflectionTypeLoadException e)
  48. {
  49. exportedTypes = e.Types;
  50. }
  51. if (exportedTypes != null)
  52. foreach (var type in exportedTypes)
  53. if (predicate(type))
  54. yield return type;
  55. }
  56. }
  57. public override void ConfigureColumns(DynamicGridColumns columns /*, bool dolookups = true */)
  58. {
  59. base.ConfigureColumns(columns /*, dolookups */);
  60. var column = columns.FirstOrDefault(x => x.ColumnName.Equals("Class"));
  61. if (column != null)
  62. {
  63. column.Lookups.Clear();
  64. var classes = CoreUtils.Entities.Where(
  65. myType =>
  66. myType.IsClass
  67. && !myType.IsGenericType
  68. && myType.IsSubclassOf(typeof(Entity))
  69. && myType.GetInterfaces().Contains(typeof(IPersistent))
  70. ).OrderBy(x => x.EntityName()).ToArray();
  71. foreach (var entity in classes)
  72. if (ClientFactory.IsSupported(entity))
  73. column.Lookups[entity.EntityName()] = entity;
  74. }
  75. column = columns.FirstOrDefault(x => x.ColumnName.Equals("Type"));
  76. if (column != null)
  77. {
  78. column.Lookups.Clear();
  79. column.Lookups[typeof(string).EntityName()] = typeof(string).EntityName();
  80. column.Lookups[typeof(int).EntityName()] = typeof(int).EntityName();
  81. column.Lookups[typeof(double).EntityName()] = typeof(double).EntityName();
  82. column.Lookups[typeof(bool).EntityName()] = typeof(bool).EntityName();
  83. column.Lookups[typeof(DateTime).EntityName()] = typeof(DateTime).EntityName();
  84. }
  85. }
  86. protected override void Reload(Filters<CustomProperty> criteria, Columns<CustomProperty> columns, ref SortOrder<CustomProperty>? sort, Action<CoreTable?, Exception?> action)
  87. {
  88. base.Reload(criteria, columns, ref sort, action);
  89. }
  90. }
  91. }