CustomModuleGrid.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Windows;
  3. using Comal.Classes;
  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.Configuration
  10. {
  11. internal class CustomModuleGrid : DynamicDataGrid<CustomModule>, ISpecificGrid
  12. {
  13. public string Section { get; set; }
  14. public DataModel? DataModel { get; set; } = null;
  15. protected override void Init()
  16. {
  17. base.Init();
  18. ActionColumns.Add(new DynamicScheduleEditorColumn<CustomModule> { Position = DynamicActionColumnPosition.Start });
  19. HiddenColumns.Add(x => x.ActiveSchedules);
  20. HiddenColumns.Add(x => x.Section);
  21. HiddenColumns.Add(x => x.Script);
  22. ActionColumns.Add(new DynamicImageColumn(PRSDesktop.Resources.script.AsBitmapImage(), EditScript));
  23. }
  24. protected override void DoReconfigure(DynamicGridOptions options)
  25. {
  26. base.DoReconfigure(options);
  27. options.RecordCount = true;
  28. options.SelectColumns = true;
  29. }
  30. private bool EditScript(CoreRow? arg)
  31. {
  32. if (arg == null)
  33. {
  34. MessageBox.Show("Please select a module first");
  35. return false;
  36. }
  37. var module = arg.ToObject<CustomModule>();
  38. var code = module.Script;
  39. var editor = new ScriptEditor(code);
  40. if (editor.ShowDialog() == true)
  41. {
  42. module.Script = editor.Script;
  43. new Client<CustomModule>().Save(module, "Updated by User");
  44. return true;
  45. }
  46. return false;
  47. }
  48. protected override void Reload(Filters<CustomModule> criteria, Columns<CustomModule> columns, ref SortOrder<CustomModule>? sort,
  49. Action<CoreTable?, Exception?> action)
  50. {
  51. sort = new SortOrder<CustomModule>(x => x.Name);
  52. criteria.Add(new Filter<CustomModule>(x => x.Section).IsEqualTo(Section).And(x => x.DataModel).IsEqualTo(DataModel?.Name ?? ""));
  53. base.Reload(criteria, columns, ref sort, action);
  54. }
  55. public override CustomModule CreateItem()
  56. {
  57. var module = base.CreateItem();
  58. module.Section = Section;
  59. module.DataModel = DataModel?.Name ?? "";
  60. module.Script =
  61. @"using System;
  62. using System.Collections.Generic;
  63. using System.Linq;
  64. using System.Runtime;
  65. using System.Windows;
  66. using System.Windows.Controls;
  67. using InABox.Core;
  68. using InABox.Clients;
  69. using InABox.DynamicGrid;
  70. using InABox.Logging;
  71. using InABox.WPF;
  72. using Comal.Classes;
  73. using PRSDesktop;
  74. public class Module
  75. {
  76. public DataModel Model { get; set; }
  77. public void BeforeLoad(){
  78. // Code to customise Model here
  79. }
  80. public void CheckTables(List<string> tables){
  81. // Customise tables of Model.
  82. }
  83. public bool Execute()
  84. {
  85. Progress.Show("""");
  86. try
  87. {
  88. // Enter your code here
  89. Progress.Close();
  90. }
  91. catch (Exception e)
  92. {
  93. Progress.Close();
  94. MessageBox.Show(e.Message);
  95. }
  96. MessageBox.Show(""All Done!"");
  97. return true;
  98. }
  99. }";
  100. return module;
  101. }
  102. }
  103. }