CustomModuleGrid.cs 3.4 KB

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