DatabaseScriptGrid.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Windows;
  4. using InABox.Clients;
  5. using InABox.Core;
  6. using InABox.DynamicGrid;
  7. using InABox.WPF;
  8. using ScriptEditor = InABox.DynamicGrid.ScriptEditorWindow;
  9. namespace PRSDesktop
  10. {
  11. public class DatabaseScriptGrid : DynamicDataGrid<Script>
  12. {
  13. protected override void Init()
  14. {
  15. base.Init();
  16. HiddenColumns.Add(x => x.Section);
  17. HiddenColumns.Add(x => x.Code);
  18. ActionColumns.Add(new DynamicImageColumn(PRSDesktop.Resources.script.AsBitmapImage(), EditScript));
  19. OnCustomiseEditor += CustomiseEditor;
  20. }
  21. protected override void DoReconfigure(DynamicGridOptions options)
  22. {
  23. base.DoReconfigure(options);
  24. options.RecordCount = true;
  25. options.SelectColumns = true;
  26. }
  27. protected override void DoValidate(Script[] items, List<string> errors)
  28. {
  29. base.DoValidate(items, errors);
  30. if (items.Any(x => string.IsNullOrWhiteSpace(x.Section)))
  31. errors.Add("[Section] must not be blank");
  32. if (items.Any(x => x.ScriptType == ScriptType.None))
  33. errors.Add("[Script Type] must not be None or empty");
  34. }
  35. private bool EditScript(CoreRow? arg)
  36. {
  37. if (arg == null)
  38. {
  39. MessageBox.Show("Please select a module first");
  40. return false;
  41. }
  42. var script = arg.ToObject<Script>();
  43. var code = script.Code;
  44. var editor = new ScriptEditor(code);
  45. if (editor.ShowDialog() == true)
  46. {
  47. script.Code = editor.Script;
  48. new Client<Script>().Save(script, "Updated by User");
  49. return true;
  50. }
  51. return false;
  52. }
  53. private void CustomiseEditor(IDynamicEditorForm sender, Script[]? items, DynamicGridColumn column, BaseEditor editor)
  54. {
  55. if (string.Equals(column.ColumnName, "Section"))
  56. {
  57. var script = items?.FirstOrDefault();
  58. editor.Editable = !string.IsNullOrWhiteSpace(script?.Section) ? Editable.Disabled : Editable.Enabled;
  59. }
  60. }
  61. }
  62. }