JobScopeGrid.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Media.Imaging;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using InABox.WPF;
  11. using InABox.Wpf;
  12. using System.Threading;
  13. namespace PRSDesktop
  14. {
  15. internal class JobScopeGrid : DynamicDataGrid<JobScope>, IMasterDetailControl<Job,JobScope>
  16. {
  17. private readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
  18. public Job? Master { get; set; }
  19. public Filter<JobScope> MasterDetailFilter => (Master?.ID ?? Guid.Empty) != Guid.Empty
  20. ? new Filter<JobScope>(x => x.Job.ID).IsEqualTo(Master.ID)
  21. : new Filter<JobScope>().None();
  22. public JobScopeGrid()
  23. {
  24. HiddenColumns.Add(x=>x.Job.DefaultScope.ID);
  25. HiddenColumns.Add(x=>x.Job.ID);
  26. ActionColumns.Add(new DynamicImageColumn(IsDefaultImage, SelectDefaultAction) { Position = DynamicActionColumnPosition.End });
  27. }
  28. protected override void DoReconfigure(DynamicGridOptions options)
  29. {
  30. base.DoReconfigure(options);
  31. options.RecordCount = true;
  32. options.SelectColumns = true;
  33. options.FilterRows = true;
  34. }
  35. public override void DeleteItems(params CoreRow[] rows)
  36. {
  37. base.DeleteItems(rows);
  38. var row = rows.FirstOrDefault(x => x.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == x.Get<JobScope, Guid>(x => x.ID));
  39. if (row is not null)
  40. {
  41. var job = Client.Query(
  42. new Filter<Job>(x => x.ID).IsEqualTo(row.Get<JobScope, Guid>(x => x.Job.ID)),
  43. Columns.Required<Job>().Add(x => x.ID).Add(x => x.DefaultScope.ID)
  44. ).Rows.FirstOrDefault()?.ToObject<Job>();
  45. if (job != null)
  46. {
  47. job.DefaultScope.ID = row.Get<JobScope, Guid>(x => x.ID);
  48. Client.Save(job, "Updated Default Scope");
  49. }
  50. }
  51. }
  52. private BitmapImage? IsDefaultImage(CoreRow? row)
  53. {
  54. return row == null
  55. ? tick
  56. : row.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == row.Get<JobScope, Guid>(x => x.ID)
  57. ? tick
  58. : null;
  59. }
  60. private bool SelectDefaultAction(CoreRow? row)
  61. {
  62. if ((row == null) ||row.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == row.Get<JobScope, Guid>(x => x.ID))
  63. return false;
  64. using (new WaitCursor())
  65. {
  66. var job = new Client<Job>().Query(
  67. new Filter<Job>(x => x.ID).IsEqualTo(row.Get<JobScope, Guid>(x => x.Job.ID)),
  68. Columns.Required<Job>().Add(x => x.ID).Add(x => x.DefaultScope.ID)
  69. ).Rows.FirstOrDefault()?.ToObject<Job>();
  70. if (job != null)
  71. {
  72. job.DefaultScope.ID = row.Get<JobScope, Guid>(x => x.ID);
  73. new Client<Job>().Save(job, "Updated Default Scope");
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. protected override bool CanCreateItems()
  80. {
  81. return base.CanCreateItems() && (Master?.ID ?? Guid.Empty) != Guid.Empty;
  82. }
  83. public override JobScope CreateItem()
  84. {
  85. var result = base.CreateItem();
  86. result.Job.ID = Master?.ID ?? Guid.Empty;
  87. result.Job.Synchronise(Master ?? new Job());
  88. result.Type = Data.Rows.Any() ? JobScopeType.Variation : JobScopeType.Contract;
  89. return result;
  90. }
  91. protected override void Reload(
  92. Filters<JobScope> criteria, Columns<JobScope> columns, ref SortOrder<JobScope>? sort,
  93. CancellationToken token, Action<CoreTable?, Exception?> action)
  94. {
  95. criteria.Add(MasterDetailFilter);
  96. base.Reload(criteria, columns, ref sort, token, action);
  97. }
  98. private DynamicGridTreeUIComponent<JobScope, Guid>? _uiComponent;
  99. private DynamicGridTreeUIComponent<JobScope, Guid> UIComponent
  100. {
  101. get
  102. {
  103. if(_uiComponent is null)
  104. {
  105. _uiComponent = new DynamicGridTreeUIComponent<JobScope, Guid>(
  106. x => x.ID,
  107. x => x.Parent.ID,
  108. Guid.Empty)
  109. {
  110. Parent = this,
  111. MaxRowHeight = 30,
  112. };
  113. //_uiComponent.OnContextMenuOpening += JobDocumentSetFolderTree_OnContextMenuOpening;
  114. }
  115. return _uiComponent;
  116. }
  117. }
  118. protected override IDynamicGridUIComponent<JobScope> CreateUIComponent()
  119. {
  120. return UIComponent;
  121. }
  122. }
  123. }