ColumnsEditorGrid.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using InABox.Core;
  2. using InABox.DynamicGrid;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace InABox.Wpf.DynamicGrid.Editors.ColumnsEditor;
  9. internal class ColumnsEditorItem : BaseObject
  10. {
  11. public string ColumnName { get; set; } = "";
  12. }
  13. internal class ColumnsEditorGrid : DynamicItemsListGrid<ColumnsEditorItem>
  14. {
  15. private readonly Type Type;
  16. private readonly string[] ColumnNames;
  17. public ColumnsEditorGrid(Type type, IColumns columns)
  18. {
  19. Type = type;
  20. var properties = DatabaseSchema.Properties(Type)
  21. .Where(x => x.IsSerializable)
  22. .Select(x => x.Name)
  23. .ToList();
  24. properties.Sort();
  25. ColumnNames = properties.ToArray();
  26. Items.AddRange(columns.ColumnNames().Select(x => new ColumnsEditorItem { ColumnName = x }));
  27. }
  28. public IColumns GetColumns()
  29. {
  30. return Columns.Create(Type, ColumnTypeFlags.None).Add(Items.Select(x => x.ColumnName));
  31. }
  32. protected override void DoReconfigure(DynamicGridOptions options)
  33. {
  34. base.DoReconfigure(options);
  35. options.Clear();
  36. options.RecordCount = true;
  37. options.MultiSelect = true;
  38. options.AddRows = true;
  39. options.DeleteRows = true;
  40. options.EditRows = true;
  41. options.ReorderRows = true;
  42. }
  43. protected override void MoveRows(CoreRow[] rows, int index, bool isCopy)
  44. {
  45. var targetCol = index < Items.Count ? Items[index] : null;
  46. var cols = LoadItems(rows);
  47. if (isCopy)
  48. {
  49. cols = cols.ToArray(x => new ColumnsEditorItem { ColumnName = x.ColumnName });
  50. }
  51. else
  52. {
  53. foreach(var col in cols)
  54. {
  55. Items.Remove(col);
  56. }
  57. }
  58. if(targetCol is not null)
  59. {
  60. var idx = Items.IndexOf(targetCol);
  61. Items.InsertRange(idx, cols);
  62. }
  63. else
  64. {
  65. Items.AddRange(cols);
  66. }
  67. Refresh(false, true);
  68. SelectedRows = cols.Select(x => Items.IndexOf(x)).Select(x => Data.Rows[x]).ToArray();
  69. }
  70. protected override IDynamicGridUIComponent<ColumnsEditorItem> CreateUIComponent()
  71. {
  72. return new DynamicGridGridUIComponent<ColumnsEditorItem>()
  73. {
  74. Parent = this,
  75. ShowHeader = false
  76. };
  77. }
  78. public override DynamicGridColumns GenerateColumns()
  79. {
  80. var columns = new DynamicGridColumns<ColumnsEditorItem>
  81. {
  82. x => x.ColumnName
  83. };
  84. return columns;
  85. }
  86. protected override void DoAdd(bool openEditorOnDirectEdit = false)
  87. {
  88. if(DynamicGridColumnNameSelectorGrid.SelectColumnName(Type, ColumnNames, out var column, showVisibilityButton: true))
  89. {
  90. CreateItems(() => [
  91. new ColumnsEditorItem { ColumnName = column }
  92. ]);
  93. }
  94. }
  95. protected override void DoEdit()
  96. {
  97. if(DynamicGridColumnNameSelectorGrid.SelectColumnName(Type, ColumnNames, out var column))
  98. {
  99. var items = LoadItems(SelectedRows);
  100. foreach(var item in items)
  101. {
  102. item.ColumnName = column;
  103. }
  104. SaveItems(items);
  105. UpdateRows(SelectedRows, items);
  106. }
  107. }
  108. }