DynamicIssuesColumn.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media.Imaging;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.WPF;
  11. namespace InABox.DynamicGrid;
  12. public class DynamicIssuesColumn<TIssues> : DynamicImageColumn
  13. where TIssues : Entity, IRemotable, IPersistent, IIssues, new()
  14. {
  15. private static readonly BitmapImage _warning = Wpf.Resources.warning.AsBitmapImage();
  16. private readonly IDynamicGrid _parent;
  17. public string IssuesProperty;
  18. public Func<CoreRow[], TIssues[]> LoadIssues;
  19. public DynamicIssuesColumn(IDynamicGrid parent, string issuesProperty, Func<CoreRow[], TIssues[]> loadIssues)
  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. IssuesProperty = issuesProperty;
  29. LoadIssues = loadIssues;
  30. }
  31. public DynamicIssuesColumn(IDynamicGrid parent):
  32. this(parent, CoreUtils.GetFullPropertyName<TIssues, string>(x => x.Issues, ""), (rows) => rows.ToObjects<TIssues>().ToArray())
  33. {
  34. }
  35. private string? GetIssues(CoreRow? row)
  36. {
  37. return row?.Get<string>(IssuesProperty);
  38. }
  39. private BitmapImage? IssuesImage(CoreRow? row)
  40. {
  41. if (GetIssues(row).IsNullOrWhiteSpace())
  42. return null;
  43. return _warning;
  44. }
  45. private FrameworkElement? CreateIssuesToolTip(DynamicActionColumn column, CoreRow? row)
  46. {
  47. var text = GetIssues(row);
  48. if (text.IsNullOrWhiteSpace())
  49. text = "No Issues Found";
  50. return TextToolTip(text);
  51. }
  52. private bool DoFilterIssues(CoreRow row, string[] filter)
  53. {
  54. var noissues = GetIssues(row).IsNullOrWhiteSpace();
  55. if (filter.Contains("No Issues") && noissues)
  56. return true;
  57. if (filter.Contains("Active Issues") && !noissues)
  58. return true;
  59. return false;
  60. }
  61. private MenuItem CreateMenu(string caption, RoutedEventHandler click)
  62. {
  63. var item = new MenuItem();
  64. item.Header = caption;
  65. item.Click += click;
  66. return item;
  67. }
  68. private ContextMenu? CreateIssuesMenu(CoreRow[]? rows)
  69. {
  70. if (!Security.CanManageIssues<TIssues>())
  71. return null;
  72. var issues = rows?.Select(GetIssues).Where(x => !string.IsNullOrWhiteSpace(x)).Any();
  73. var result = new ContextMenu();
  74. if (issues != true)
  75. {
  76. result.Items.Add(CreateMenu("Create Issue", (o, e) => EditIssues(rows)));
  77. }
  78. else
  79. {
  80. result.Items.Add(CreateMenu("Update Issues", (o, e) => EditIssues(rows)));
  81. result.Items.Add(CreateMenu("Clear Issues", (o, e) => ClearIssues(rows)));
  82. }
  83. return result;
  84. }
  85. private void ClearIssues(CoreRow[]? rows)
  86. {
  87. if (rows is null)
  88. {
  89. return;
  90. }
  91. if (MessageBox.Show("This will clear the flagged issues for these items!\n\nAre you sure you wish to continue?", "Confirm",
  92. MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  93. {
  94. var _updates = LoadIssues(rows).ToArray();
  95. foreach (var update in _updates)
  96. {
  97. update.Issues = "";
  98. }
  99. using (new WaitCursor())
  100. {
  101. Client.Save(_updates, "Clearing Issues", (o, e) => { });
  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 objects = LoadIssues(rows ?? Array.Empty<CoreRow>());
  111. var map = new Dictionary<CoreRow, TIssues>();
  112. if (rows is not null)
  113. {
  114. var i = 0;
  115. foreach (var row in rows)
  116. {
  117. map[row] = objects[i];
  118. ++i;
  119. }
  120. }
  121. if (new DynamicIssuesEditor(map.Values.ToArray()).ShowDialog() == true)
  122. {
  123. using (new WaitCursor())
  124. {
  125. Client.Save(map.Values, "Updating Issues", (o, e) => { });
  126. }
  127. foreach (var row in map.Keys)
  128. _parent.UpdateRow(row, IssuesProperty, map[row].Issues);
  129. }
  130. }
  131. }