ModuleConfigurationWindow.xaml.cs 4.6 KB

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