DynamicProblemsColumn.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media.Imaging;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.WPF;
  10. namespace InABox.DynamicGrid;
  11. public class DynamicProblemsColumn<TEntity> : DynamicImageColumn
  12. where TEntity : Entity, IRemotable, IPersistent, IProblems, new()
  13. {
  14. private static readonly BitmapImage _warning = Wpf.Resources.warning.AsBitmapImage();
  15. private static readonly BitmapImage _graywarning = Wpf.Resources.warning.AsGrayScale().AsBitmapImage();
  16. private readonly IDynamicGrid _parent;
  17. public Func<IIssues[], FrameworkElement?>? CustomiseEditor { get; set; }
  18. public Action<IIssues[], FrameworkElement?>? IssuesUpdated { get; set; }
  19. public DynamicProblemsColumn(IDynamicGrid parent)
  20. {
  21. Image = IssuesImage;
  22. Filters = new[] { "Active Issues", "No Issues" };
  23. FilterRecord = DoFilterIssues;
  24. ContextMenu = CreateIssuesMenu;
  25. ToolTip = CreateIssuesToolTip;
  26. _parent = parent;
  27. Position = DynamicActionColumnPosition.Start;
  28. }
  29. private BitmapImage? IssuesImage(CoreRow? row)
  30. {
  31. if (row?.Get<TEntity,string[]>(x=>x.Problem.Notes)?.Any() != true)
  32. return null;
  33. return row?.Get<TEntity,DateTime>(x=>x.Problem.Resolved).IsEmpty() == true
  34. ? _warning
  35. : _graywarning;
  36. }
  37. private FrameworkElement? CreateIssuesToolTip(DynamicActionColumn column, CoreRow? row)
  38. {
  39. var text = row?.Get<TEntity, string[]>(x => x.Problem.Notes) ?? new string[] { };
  40. return TextToolTip(string.Join("\n==================================", text));
  41. }
  42. private bool DoFilterIssues(CoreRow row, string[] filter)
  43. {
  44. var noissues = row?.Get<TEntity, string[]>(x => x.Problem.Notes)?.Any() != true;
  45. if (filter.Contains("No Issues") && noissues)
  46. return true;
  47. if (filter.Contains("Active Issues") && !noissues)
  48. return true;
  49. return false;
  50. }
  51. private MenuItem CreateMenu(string caption, RoutedEventHandler click)
  52. {
  53. var item = new MenuItem();
  54. item.Header = caption;
  55. item.Click += click;
  56. return item;
  57. }
  58. private ContextMenu? CreateIssuesMenu(CoreRow[]? rows)
  59. {
  60. if (!Security.CanManageProblems<TEntity>())
  61. return null;
  62. var issues = rows?.FirstOrDefault()?.Get<TEntity, string[]>(x => x.Problem.Notes)?.Any() == true;
  63. var result = new ContextMenu();
  64. if (issues != true)
  65. {
  66. result.Items.Add(CreateMenu("Create Issue", (o, e) => EditIssues(rows)));
  67. }
  68. else
  69. {
  70. result.Items.Add(CreateMenu("Update Issues", (o, e) => EditIssues(rows)));
  71. if (Security.CanManageProblems<TEntity>())
  72. {
  73. result.Items.Add(new Separator());
  74. result.Items.Add(CreateMenu("Mark as Resolved", (o, e) => ResolveIssues(rows)));
  75. }
  76. }
  77. return result;
  78. }
  79. private void ResolveIssues(CoreRow[]? rows)
  80. {
  81. var row = rows?.FirstOrDefault();
  82. if (row == null)
  83. return;
  84. var entity = row.ToObject<TEntity>();
  85. entity.Problem.Resolved = DateTime.Now;
  86. using (new WaitCursor())
  87. Client.Save(entity, "Resolving Issues", (o, e) => { });
  88. // False here to prevent Refreshing and losing the selected row record
  89. _parent.UpdateRow<TEntity, DateTime>(row, x=>x.Problem.Resolved, entity.Problem.Resolved);
  90. // if (MessageBox.Show("This will clear the flagged issues for these items!\n\nAre you sure you wish to continue?", "Confirm",
  91. // MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  92. // {
  93. // var _updates = LoadIssues(rows).ToArray();
  94. // foreach (var update in _updates)
  95. // {
  96. // update.Issues = "";
  97. // }
  98. // using (new WaitCursor())
  99. // {
  100. // Client.Save(_updates, "Clearing Issues", (o, e) => { });
  101. // }
  102. //
  103. // // False here to prevent Refreshing and losing the selected row record
  104. // foreach (var row in rows)
  105. // _parent.UpdateRow(row, IssuesProperty, "");
  106. // }
  107. }
  108. private void EditIssues(CoreRow[]? rows)
  109. {
  110. var row = rows?.FirstOrDefault();
  111. if (row == null)
  112. return;
  113. var entity = row.ToObject<TEntity>();
  114. var _grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicItemsListGrid<>), entity.Problem.GetType());
  115. if (_grid.EditItems(new object[] { entity.Problem }))
  116. {
  117. using (new WaitCursor())
  118. Client.Save(entity, "Resolving Issues", (o, e) => { });
  119. row.Table.FillRow(row, entity);
  120. _parent.InvalidateRow(row);
  121. }
  122. }
  123. }