DatabaseScriptGrid.cs 2.3 KB

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