DatabaseScriptGrid.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. Options.AddRange(
  16. DynamicGridOption.AddRows,
  17. DynamicGridOption.EditRows,
  18. DynamicGridOption.DeleteRows,
  19. DynamicGridOption.RecordCount,
  20. DynamicGridOption.SelectColumns
  21. );
  22. HiddenColumns.Add(x => x.Section);
  23. HiddenColumns.Add(x => x.Code);
  24. ActionColumns.Add(new DynamicImageColumn(Properties.Resources.script.AsBitmapImage(), EditScript));
  25. OnCustomiseEditor += CustomiseEditor;
  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 is 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. }