EntitySelectionGrid.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.Threading;
  10. using System.Windows.Media.Imaging;
  11. namespace PRSDesktop;
  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. base.Init();
  32. HiddenColumns.Add(x => x.ID);
  33. ActionColumns.Add(new DynamicImageColumn(TickImage, TickAction));
  34. }
  35. protected override void DoReconfigure(DynamicGridOptions options)
  36. {
  37. options.Clear();
  38. options.FilterRows = true;
  39. }
  40. private BitmapImage TickImage(CoreRow? row)
  41. {
  42. if (row == null)
  43. {
  44. return ticked;
  45. }
  46. return _entities.Contains((Guid)row["ID"]) ? ticked : unticked;
  47. }
  48. private bool TickAction(CoreRow? row)
  49. {
  50. if (row == null)
  51. {
  52. if (_entities.Count < Data.Rows.Count)
  53. {
  54. _entities = Data.Rows.Select(x => (Guid)x["ID"]).ToHashSet();
  55. }
  56. else
  57. {
  58. _entities.Clear();
  59. }
  60. return true;
  61. }
  62. var employeeID = (Guid)row["ID"];
  63. if (_entities.Contains(employeeID))
  64. {
  65. _entities.Remove(employeeID);
  66. }
  67. else
  68. {
  69. _entities.Add(employeeID);
  70. }
  71. return true;
  72. }
  73. public override void DeleteItems(params CoreRow[] rows)
  74. {
  75. }
  76. public override T LoadItem(CoreRow row)
  77. {
  78. var id = row.Get<Employee, Guid>(x => x.ID);
  79. var filter = new Filter<T>(x => x.ID).IsEqualTo(id);
  80. return new Client<T>().Load(filter).FirstOrDefault();
  81. }
  82. protected override void Reload(
  83. Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sort,
  84. CancellationToken token, Action<CoreTable?, Exception?> action)
  85. {
  86. new Client<T>().Query(criteria.Combine(), columns, sort, CoreRange.All, action);
  87. }
  88. public override void SaveItem(T item)
  89. {
  90. }
  91. }