ProductDimensionUnitGrid.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.Core;
  8. using InABox.DynamicGrid;
  9. using InABox.Wpf;
  10. using InABox.WPF;
  11. namespace PRSDesktop.Grids;
  12. public class ProductDimensionUnitGrid : DynamicDataGrid<ProductDimensionUnit>
  13. {
  14. private static readonly BitmapImage CONVERT = PRSDesktop.Resources.specifications.AsBitmapImage();
  15. protected override void Init()
  16. {
  17. base.Init();
  18. HiddenColumns.Add(x => x.Code);
  19. HiddenColumns.Add(x => x.HasQuantity);
  20. HiddenColumns.Add(x => x.HasLength);
  21. HiddenColumns.Add(x => x.HasWeight);
  22. HiddenColumns.Add(x => x.HasWidth);
  23. HiddenColumns.Add(x => x.HasHeight);
  24. HiddenColumns.Add(x =>x.Conversion);
  25. ActionColumns.Add(new DynamicImageColumn(ConvertImage) { Position = DynamicActionColumnPosition.End});
  26. AddButton("Update Expressions", null, (button, rows) =>
  27. {
  28. UpdateExpressions(rows.ToArray<ProductDimensionUnit>());
  29. return false;
  30. });
  31. }
  32. private BitmapImage? ConvertImage(CoreRow? row)
  33. {
  34. return row == null
  35. ? CONVERT
  36. : String.IsNullOrWhiteSpace(row.Get<ProductDimensionUnit, string>(x => x.Conversion))
  37. ? null
  38. : CONVERT;
  39. }
  40. protected override void DoValidate(ProductDimensionUnit[] items, List<string> errors)
  41. {
  42. base.DoValidate(items, errors);
  43. foreach (var item in items)
  44. item.Validate(errors);
  45. }
  46. private void UpdateExpressions(ProductDimensionUnit[] items)
  47. {
  48. Dictionary<Type, int>? results = null;
  49. Exception? exception = null;
  50. Progress.ShowModal("Updating Dimensions", progress =>
  51. {
  52. try
  53. {
  54. results = DimensionUnitUtils.UpdateExpressions<ProductDimensionUnit, ProductDimensionUnitLink>(items, progress);
  55. }
  56. catch(Exception e)
  57. {
  58. exception = e;
  59. }
  60. });
  61. if(results is not null)
  62. {
  63. MessageWindow.ShowMessage($"Update successful:\n{string.Join("\n", results.Select(x => $"- {x.Key.Name}: {x.Value} items updated"))}", "Success");
  64. }
  65. else if(exception is not null)
  66. {
  67. MessageWindow.ShowError("Error while updating dimensions", exception);
  68. }
  69. }
  70. private bool ShouldUpdateExpressions = false;
  71. protected override void DoBeforeSave(IDynamicEditorForm editor, ProductDimensionUnit[] items)
  72. {
  73. base.DoBeforeSave(editor, items);
  74. ShouldUpdateExpressions = false;
  75. if(items.Any(x => x.HasOriginalValue(x => x.Format) || x.HasOriginalValue(x => x.Formula)))
  76. {
  77. if(MessageWindow.ShowYesNo("The format and/or formula has been changed; do you wish to update the UnitSize/Value of every item that uses dimension to match the new expression? (This may take a while)", "Update expressions?"))
  78. {
  79. ShouldUpdateExpressions = true;
  80. }
  81. }
  82. }
  83. protected override void DoAfterSave(IDynamicEditorForm editor, ProductDimensionUnit[] items)
  84. {
  85. base.DoAfterSave(editor, items);
  86. if (ShouldUpdateExpressions)
  87. {
  88. UpdateExpressions(items);
  89. }
  90. }
  91. protected override bool DoMerge(CoreRow[] rows)
  92. {
  93. var columns = new Column<ProductDimensionUnit>[]
  94. {
  95. new(x => x.HasLength),
  96. new(x => x.HasQuantity),
  97. new(x => x.HasHeight),
  98. new(x => x.HasWeight),
  99. new(x => x.HasWidth),
  100. };
  101. var target = rows[^1].ToObject<ProductDimensionUnit>();
  102. if(columns.Any(c => rows.Select(r => r[c.Property]).Distinct().Skip(1).Any()))
  103. {
  104. MessageWindow.ShowMessage("These dimension units are incompatible, and cannot be merged.\n\n(Dimension units can only be merged if they have the same [HasQuantity], [HasLength], [HasWidth], [HasHeight] and [HasWeight] values).", "Incompatible units", image: MessageWindow.WarningImage);
  105. return false;
  106. }
  107. if (base.DoMerge(rows))
  108. {
  109. if(MessageWindow.ShowYesNo(
  110. $"Do you wish to update the UnitSize/Value for every item that uses {target.Code}? (This may take a while)",
  111. "Update Expressions?"))
  112. {
  113. UpdateExpressions([target]);
  114. }
  115. return true;
  116. }
  117. return false;
  118. }
  119. protected override void CustomiseEditor(ProductDimensionUnit[] items, DynamicGridColumn column, BaseEditor editor)
  120. {
  121. base.CustomiseEditor(items, column, editor);
  122. if (column.ColumnName == nameof(ProductDimensionUnit.Conversion) && editor is ScriptEditor scriptEditor)
  123. {
  124. scriptEditor.Type = ScriptEditorType.TemplateEditor;
  125. scriptEditor.OnEditorClicked += () =>
  126. {
  127. var script = items.FirstOrDefault()?.Conversion.NotWhiteSpaceOr()
  128. ?? DimensionUnit.DefaultConvertDimensionsScript();
  129. var editor = new ScriptEditorWindow(script, SyntaxLanguage.CSharp);
  130. if (editor.ShowDialog() == true)
  131. {
  132. foreach (var item in items)
  133. SetEditorValue(item, column.ColumnName, editor.Script);
  134. }
  135. };
  136. }
  137. }
  138. }