DynamicColumnGrid.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5. using System.Threading;
  6. using InABox.Core;
  7. namespace InABox.DynamicGrid;
  8. public class DynamicColumnGrid : DynamicGrid<DynamicGridColumn>
  9. {
  10. public DynamicColumnGrid(IDynamicGridColumnSchema schema)
  11. {
  12. Columns = new DynamicGridColumns();
  13. Schema = schema;
  14. }
  15. protected override void Init()
  16. {
  17. base.Init();
  18. }
  19. protected override void DoReconfigure(DynamicGridOptions options)
  20. {
  21. base.DoReconfigure(options);
  22. options.RecordCount = true;
  23. options.MultiSelect = true;
  24. options.AddRows = true;
  25. options.EditRows = true;
  26. options.DeleteRows = true;
  27. options.ReorderRows = true;
  28. }
  29. private IDynamicGridColumnSchema _schema;
  30. public IDynamicGridColumnSchema Schema
  31. {
  32. get => _schema;
  33. [MemberNotNull(nameof(_schema))]
  34. set
  35. {
  36. _schema = value;
  37. var column = MasterColumns.FirstOrDefault(x => string.Equals(x.ColumnName, nameof(DynamicGridColumn.ColumnName)));
  38. if(column is not null && column.Editor is DynamicColumnNameEditor edit)
  39. {
  40. edit.Schema = value;
  41. }
  42. }
  43. }
  44. public DynamicGridColumns Columns { get; }
  45. public bool DirectEdit { get; set; }
  46. protected override void MoveRows(CoreRow[] rows, int index, bool isCopy)
  47. {
  48. var targetCol = index < Columns.Count ? Columns[index] : null;
  49. var cols = LoadItems(rows);
  50. if (isCopy)
  51. {
  52. cols = cols.ToArray(x => x.Copy());
  53. }
  54. else
  55. {
  56. foreach(var col in cols)
  57. {
  58. Columns.Remove(col);
  59. }
  60. }
  61. if(targetCol is not null)
  62. {
  63. var idx = Columns.IndexOf(targetCol);
  64. Columns.InsertRange(idx, cols);
  65. }
  66. else
  67. {
  68. Columns.AddRange(cols);
  69. }
  70. Refresh(false, true);
  71. SelectedRows = cols.Select(x => Columns.IndexOf(x)).Select(x => Data.Rows[x]).ToArray();
  72. }
  73. protected override void DoAdd(bool openEditorOnDirectEdit = false)
  74. {
  75. if(DynamicGridColumnNameSelectorGrid.SelectColumnName(Schema, out var column))
  76. {
  77. var item = Schema.GetColumn(column);
  78. SaveItem(item);
  79. DoChanged();
  80. Refresh(false, true);
  81. }
  82. }
  83. protected override void DoValidate(DynamicGridColumn[] items, List<string> errors)
  84. {
  85. base.DoValidate(items, errors);
  86. if (items.Any(x => string.IsNullOrWhiteSpace(x.ColumnName)))
  87. errors.Add("[ColumnName] must not be blank!");
  88. }
  89. protected override Dictionary<string, object?> EditorValueChanged(IDynamicEditorForm editor, DynamicGridColumn[] items, string name, object value)
  90. {
  91. var changes = base.EditorValueChanged(editor, items, name, value);
  92. if(name == nameof(DynamicGridColumn.ColumnName) && value is string columnName)
  93. {
  94. var newCol = Schema.GetColumn(columnName);
  95. foreach(var item in items)
  96. {
  97. CoreUtils.MonitorChanges(item, () =>
  98. {
  99. item.Width = newCol.Width;
  100. item.Alignment = newCol.Alignment;
  101. item.Format = newCol.Format;
  102. item.Editor = newCol.Editor.CloneEditor();
  103. item.Caption = newCol.Caption;
  104. }, changes);
  105. }
  106. }
  107. return changes;
  108. }
  109. #region Save / Load
  110. protected override void Reload(
  111. Filters<DynamicGridColumn> criteria, Columns<DynamicGridColumn> columns, ref SortOrder<DynamicGridColumn>? sort,
  112. CancellationToken token, Action<CoreTable, Exception?> action)
  113. {
  114. var result = new CoreTable();
  115. if (columns == null || columns.Count == 0)
  116. result.LoadColumns(typeof(DynamicGridColumn));
  117. else
  118. result.LoadColumns(columns);
  119. result.LoadRows(Columns);
  120. action.Invoke(result, null);
  121. }
  122. public override DynamicGridColumn LoadItem(CoreRow row)
  123. {
  124. var index = Data.Rows.IndexOf(row);
  125. return Columns[index];
  126. }
  127. public override void SaveItem(DynamicGridColumn item)
  128. {
  129. if (!Columns.Contains(item))
  130. Columns.Add(item);
  131. }
  132. public override void DeleteItems(params CoreRow[] rows)
  133. {
  134. foreach (var row in rows.OrderByDescending(x => x.Index))
  135. Columns.RemoveAt(row.Index);
  136. }
  137. #endregion
  138. }