ModuleConfigurationWindow.xaml.cs 4.6 KB

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