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