ModuleConfigurationWindow.xaml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Windows;
  7. using InABox.Configuration;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using InABox.Wpf;
  11. using InABox.WPF;
  12. namespace PRSDesktop;
  13. public class ModuleItem : BaseObject
  14. {
  15. public string Description { get; set; }
  16. public bool Checked { get; set; }
  17. }
  18. public class ModuleGrid : DynamicGrid<ModuleItem>
  19. {
  20. private List<ModuleItem> Items;
  21. public ModuleGrid()
  22. {
  23. Configuration = new Dictionary<string, bool>();
  24. }
  25. protected override void Init()
  26. {
  27. base.Init();
  28. var tick = PRSDesktop.Resources.tick.AsBitmapImage();
  29. var disabled = PRSDesktop.Resources.disabled.AsBitmapImage();
  30. ActionColumns.Add(new DynamicTickColumn<ModuleItem, bool>(x => x.Checked, tick, tick, disabled, CheckClick) { AllowHeaderClick = true });
  31. HiddenColumns.Add(x => x.Description);
  32. HiddenColumns.Add(x => x.Checked);
  33. }
  34. protected override void DoReconfigure(DynamicGridOptions options)
  35. {
  36. base.DoReconfigure(options);
  37. options.RecordCount = true;
  38. options.FilterRows = true;
  39. }
  40. public Dictionary<string, bool> Configuration { get; set; }
  41. public List<Type> Modules { get; private set; }
  42. public override void DeleteItems(params CoreRow[] rows)
  43. {
  44. }
  45. public override void SaveItem(ModuleItem item)
  46. {
  47. }
  48. public override ModuleItem LoadItem(CoreRow row)
  49. {
  50. return Items[row.Index];
  51. }
  52. protected override void Reload(
  53. Filters<ModuleItem> criteria, Columns<ModuleItem> columns, ref SortOrder<ModuleItem>? sort,
  54. CancellationToken token, Action<CoreTable?, Exception?> action)
  55. {
  56. if (Items == null)
  57. {
  58. Items = new List<ModuleItem>();
  59. var types = CoreUtils.Entities.Where(x => x.GetInterfaces().Contains(typeof(IPersistent))).OrderBy(x => x.EntityName());
  60. foreach (var type in types)
  61. {
  62. var moduleitem = new ModuleItem { Description = type.EntityName() };
  63. if (Configuration.ContainsKey(type.EntityName()))
  64. moduleitem.Checked = Configuration[type.EntityName()];
  65. else
  66. moduleitem.Checked = true;
  67. Items.Add(moduleitem);
  68. }
  69. }
  70. var table = new CoreTable();
  71. table.LoadColumns(columns);
  72. table.LoadRows(Items);
  73. action.Invoke(table, null);
  74. }
  75. private bool CheckClick(CoreRow row)
  76. {
  77. var result = false;
  78. var rows = row == null ? Data.Rows.ToArray() : new[] { row };
  79. foreach (var corrow in rows)
  80. {
  81. var desc = corrow.Get<ModuleItem, string>(x => x.Description);
  82. var item = Items.FirstOrDefault(x => x.Description.Equals(desc));
  83. item.Checked = !item.Checked;
  84. Configuration[desc] = item.Checked;
  85. result = true;
  86. }
  87. return result;
  88. }
  89. protected override DynamicGridColumns LoadColumns()
  90. {
  91. var columns = new DynamicGridColumns<ModuleItem>();
  92. columns.Add(x => x.Description, width: 0, alignment: Alignment.MiddleLeft);
  93. return columns;
  94. }
  95. }
  96. /// <summary>
  97. /// Interaction logic for ModuleConfigurationWindow.xaml
  98. /// </summary>
  99. public partial class ModuleConfigurationWindow : ThemableWindow
  100. {
  101. private readonly ModuleConfiguration _config;
  102. private readonly string _database = "";
  103. public ModuleConfigurationWindow(string database)
  104. {
  105. InitializeComponent();
  106. _database = database.ToLower();
  107. _config = new LocalConfiguration<ModuleConfiguration>(Path.GetDirectoryName(_database), Path.GetFileName(_database)).Load();
  108. Modules.Configuration = _config;
  109. Modules.Refresh(true, true);
  110. }
  111. private void OK_Click(object sender, RoutedEventArgs e)
  112. {
  113. new LocalConfiguration<ModuleConfiguration>(Path.GetDirectoryName(_database), Path.GetFileName(_database)).Save(_config);
  114. DialogResult = true;
  115. Close();
  116. }
  117. private void Cancel_Click(object sender, RoutedEventArgs e)
  118. {
  119. DialogResult = false;
  120. Close();
  121. }
  122. private class ModuleConfiguration : Dictionary<string, bool>, ILocalConfigurationSettings
  123. {
  124. }
  125. }