ModuleConfigurationWindow.xaml.cs 4.8 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(FluentList<DynamicGridOption> options)
  34. {
  35. options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.FilterRows);
  36. }
  37. public Dictionary<string, bool> Configuration { get; set; }
  38. public List<Type> Modules { get; private set; }
  39. protected override void DeleteItems(params CoreRow[] rows)
  40. {
  41. }
  42. public override void SaveItem(ModuleItem item)
  43. {
  44. }
  45. protected override ModuleItem LoadItem(CoreRow row)
  46. {
  47. return Items[row.Index];
  48. }
  49. protected override void Reload(Filters<ModuleItem> criteria, Columns<ModuleItem> columns, ref SortOrder<ModuleItem>? sort,
  50. Action<CoreTable?, Exception?> action)
  51. {
  52. if (Items == null)
  53. {
  54. Items = new List<ModuleItem>();
  55. var types = CoreUtils.Entities.Where(x => x.GetInterfaces().Contains(typeof(IPersistent))).OrderBy(x => x.EntityName());
  56. foreach (var type in types)
  57. {
  58. var moduleitem = new ModuleItem { Description = type.EntityName() };
  59. if (Configuration.ContainsKey(type.EntityName()))
  60. moduleitem.Checked = Configuration[type.EntityName()];
  61. else
  62. moduleitem.Checked = true;
  63. Items.Add(moduleitem);
  64. }
  65. }
  66. var table = new CoreTable();
  67. foreach (var column in columns.Items)
  68. table.Columns.Add(new CoreColumn { ColumnName = column.ToString() });
  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. }