CustomModuleGrid.cs 3.2 KB

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