DynamicIssuesColumn.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media.Imaging;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.DynamicGrid.Properties;
  9. using InABox.WPF;
  10. namespace InABox.DynamicGrid
  11. {
  12. public class DynamicIssuesColumn<T> : DynamicActionColumn
  13. where T : Entity, IRemotable, IPersistent, IIssues, new()
  14. {
  15. private static readonly BitmapImage _warning = Resources.warning.AsBitmapImage();
  16. private readonly IDynamicGrid _parent;
  17. public DynamicIssuesColumn(IDynamicGrid parent) : base(IssuesImage)
  18. {
  19. Filters = new[] { "Active Issues", "No Issues" };
  20. FilterRecord = DoFilterIssues;
  21. ContextMenu = CreateIssuesMenu;
  22. ToolTip = CreateIssuesToolTip;
  23. _parent = parent;
  24. Position = DynamicActionColumnPosition.Start;
  25. }
  26. private static BitmapImage IssuesImage(CoreRow row)
  27. {
  28. if (row == null || string.IsNullOrWhiteSpace(row.Get<T, string>(x => x.Issues)))
  29. return null;
  30. return _warning;
  31. }
  32. private FrameworkElement CreateIssuesToolTip(DynamicActionColumn column, CoreRow row)
  33. {
  34. var text = row?.Get<T, string>(x => x.Issues);
  35. if (string.IsNullOrWhiteSpace(text))
  36. text = "No Issues Found";
  37. return TextToolTip(text);
  38. }
  39. private bool DoFilterIssues(CoreRow row, string[] filter)
  40. {
  41. var noissues = string.IsNullOrWhiteSpace(row?.Get<T, string>(x => x.Issues));
  42. if (filter.Contains("No Issues") && noissues)
  43. return true;
  44. if (filter.Contains("Active Issues") && !noissues)
  45. return true;
  46. return false;
  47. }
  48. private MenuItem CreateMenu(string caption, RoutedEventHandler click)
  49. {
  50. var item = new MenuItem();
  51. item.Header = caption;
  52. item.Click += click;
  53. return item;
  54. }
  55. private ContextMenu CreateIssuesMenu(CoreRow[] rows)
  56. {
  57. if (!Security.CanManageIssues<T>())
  58. return null;
  59. var issues = rows?.Select(r => r.Get<T, string>(x => x.Issues)).Where(x => !string.IsNullOrWhiteSpace(x)).Any();
  60. var result = new ContextMenu();
  61. if (issues != true)
  62. {
  63. result.Items.Add(CreateMenu("Create Issue", (o, e) => EditIssues(rows)));
  64. }
  65. else
  66. {
  67. result.Items.Add(CreateMenu("Update Issues", (o, e) => EditIssues(rows)));
  68. result.Items.Add(CreateMenu("Clear Issues", (o, e) => ClearIssues(rows)));
  69. }
  70. return result;
  71. }
  72. private void ClearIssues(CoreRow[] rows)
  73. {
  74. if (MessageBox.Show("This will clear the flagged issues for these items!\n\nAre you sure you wish to continue?", "Confirm",
  75. MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  76. {
  77. var _updates = rows.Select(x => x.ToObject<T>()).ToArray();
  78. foreach (var update in _updates)
  79. update.Issues = "";
  80. using (new WaitCursor())
  81. {
  82. new Client<T>().Save(_updates, "Clearing Issues", (o, e) => { });
  83. }
  84. // False here to prevent Refreshing and losing the selected row record
  85. foreach (var row in rows)
  86. _parent.UpdateRow<T, string>(row, x => x.Issues, "");
  87. }
  88. }
  89. private void EditIssues(CoreRow[] rows)
  90. {
  91. var map = new Dictionary<CoreRow, T>();
  92. foreach (var row in rows)
  93. map[row] = row.ToObject<T>();
  94. if (new DynamicIssuesEditor(map.Values.ToArray()).ShowDialog() == true)
  95. {
  96. using (new WaitCursor())
  97. {
  98. new Client<T>().Save(map.Values, "Updating Issues", (o, e) => { });
  99. }
  100. foreach (var row in map.Keys)
  101. _parent.UpdateRow<T, string>(row, x => x.Issues, map[row].Issues);
  102. }
  103. }
  104. }
  105. }