EntitySelectionGrid.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using InABox.WPF;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Windows.Media.Imaging;
  10. namespace PRSDesktop
  11. {
  12. public interface IEntitySelectionGrid
  13. {
  14. HashSet<Guid> Entities { get; set; }
  15. }
  16. public class EntitySelectionGrid<T> : DynamicGrid<T>, IEntitySelectionGrid where T : Entity, IRemotable, IPersistent, new()
  17. {
  18. private readonly BitmapImage unticked = PRSDesktop.Resources.disabled.AsBitmapImage();
  19. private readonly BitmapImage ticked = PRSDesktop.Resources.tick.AsBitmapImage();
  20. private HashSet<Guid> _entities;
  21. public HashSet<Guid> Entities
  22. {
  23. get => _entities;
  24. set
  25. {
  26. _entities = value;
  27. }
  28. }
  29. protected override void Init()
  30. {
  31. HiddenColumns.Add(x => x.ID);
  32. ActionColumns.Add(new DynamicImageColumn(TickImage, TickAction));
  33. }
  34. protected override void DoReconfigure(DynamicGridOptions options)
  35. {
  36. options.Clear();
  37. options.FilterRows = true;
  38. }
  39. private BitmapImage TickImage(CoreRow? row)
  40. {
  41. if (row == null)
  42. {
  43. return ticked;
  44. }
  45. return _entities.Contains((Guid)row["ID"]) ? ticked : unticked;
  46. }
  47. private bool TickAction(CoreRow? row)
  48. {
  49. if (row == null)
  50. {
  51. if (_entities.Count < Data.Rows.Count)
  52. {
  53. _entities = Data.Rows.Select(x => (Guid)x["ID"]).ToHashSet();
  54. }
  55. else
  56. {
  57. _entities.Clear();
  58. }
  59. return true;
  60. }
  61. var employeeID = (Guid)row["ID"];
  62. if (_entities.Contains(employeeID))
  63. {
  64. _entities.Remove(employeeID);
  65. }
  66. else
  67. {
  68. _entities.Add(employeeID);
  69. }
  70. return true;
  71. }
  72. public override void DeleteItems(params CoreRow[] rows)
  73. {
  74. }
  75. public override T LoadItem(CoreRow row)
  76. {
  77. var id = row.Get<Employee, Guid>(x => x.ID);
  78. var filter = new Filter<T>(x => x.ID).IsEqualTo(id);
  79. return new Client<T>().Load(filter).FirstOrDefault();
  80. }
  81. protected override void Reload(Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sort, Action<CoreTable?, Exception?> action)
  82. {
  83. new Client<T>().Query(criteria.Combine(), columns, sort, action);
  84. }
  85. public override void SaveItem(T item)
  86. {
  87. }
  88. }
  89. }