CustomPropertyGrid.cs 3.9 KB

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