CustomModuleGrid.cs 3.3 KB

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