DatabaseScriptGrid.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 PRSServer.Formd.DatabaseScripts
  10. {
  11. public class DatabaseScriptGrid : DynamicDataGrid<Script>
  12. {
  13. public DatabaseScriptGrid()
  14. {
  15. HiddenColumns.Add(x => x.Section);
  16. HiddenColumns.Add(x => x.Code);
  17. ActionColumns.Add(new DynamicImageColumn(Properties.Resources.script.AsBitmapImage(), EditScript));
  18. OnCustomiseEditor += CustomiseEditor;
  19. }
  20. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  21. {
  22. base.DoReconfigure(options);
  23. options.AddRange(
  24. DynamicGridOption.AddRows,
  25. DynamicGridOption.EditRows,
  26. DynamicGridOption.DeleteRows,
  27. DynamicGridOption.RecordCount,
  28. DynamicGridOption.SelectColumns
  29. );
  30. }
  31. protected override void DoValidate(Script[] items, List<string> errors)
  32. {
  33. base.DoValidate(items, errors);
  34. if (items.Any(x => string.IsNullOrWhiteSpace(x.Section)))
  35. errors.Add("[Section] must not be blank");
  36. if (items.Any(x => x.ScriptType == ScriptType.None))
  37. errors.Add("[Script Type] must not be None or empty");
  38. }
  39. private bool EditScript(CoreRow? arg)
  40. {
  41. if (arg is null)
  42. {
  43. MessageBox.Show("Please select a module first");
  44. return false;
  45. }
  46. var script = arg.ToObject<Script>();
  47. var code = script.Code;
  48. var editor = new ScriptEditor(code);
  49. if (editor.ShowDialog() == true)
  50. {
  51. script.Code = editor.Script;
  52. new Client<Script>().Save(script, "Updated by User");
  53. return true;
  54. }
  55. return false;
  56. }
  57. private void CustomiseEditor(IDynamicEditorForm sender, Script[]? items, DynamicGridColumn column, BaseEditor editor)
  58. {
  59. if (string.Equals(column.ColumnName, "Section"))
  60. {
  61. var script = items?.FirstOrDefault();
  62. editor.Editable = !string.IsNullOrWhiteSpace(script?.Section) ? Editable.Disabled : Editable.Enabled;
  63. }
  64. }
  65. }
  66. }