CoreTableGrid.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using InABox.Core;
  2. using NPOI.HSSF.Record;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. namespace InABox.DynamicGrid;
  12. public class CoreTableColumnSchema(CoreTable table) : IDynamicGridColumnSchema
  13. {
  14. private DynamicGridColumn[] _columns = table.Columns.Select(DynamicGridColumn.FromCoreColumn).NotNull().ToArray();
  15. public IEnumerable<string> ColumnNames => _columns.Select(x => x.ColumnName);
  16. public DynamicGridColumn GetColumn(string column)
  17. {
  18. return _columns.First(x => x.ColumnName == column);
  19. }
  20. public string? GetComment(string column)
  21. {
  22. return null;
  23. }
  24. public bool IsVisible(string column)
  25. {
  26. return true;
  27. }
  28. }
  29. public class CoreTableGrid : BaseDynamicGrid
  30. {
  31. public CoreTable Table { get; set; } = new();
  32. public IDynamicGridColumnSchema? ColumnSchema { get; set; }
  33. #region Config
  34. protected override void Init()
  35. {
  36. }
  37. protected override void DoReconfigure(DynamicGridOptions options)
  38. {
  39. // We can add rows by default, but not edit them.
  40. options.EditRows = false;
  41. }
  42. #endregion
  43. #region Row Manipulation
  44. protected override void NewRow()
  45. {
  46. // This makes use of the fact that MasterData will be equivalent to Table (see 'ReloadData()').
  47. var row = Table.NewRow(true);
  48. Table.Rows.Add(row);
  49. var dataRow = Data.NewRow();
  50. dataRow.LoadValues(row.Values);
  51. Data.Rows.Add(dataRow);
  52. _recordmap[dataRow] = row;
  53. InvalidateGrid();
  54. SelectedRows = [dataRow];
  55. DoChanged();
  56. }
  57. protected override bool EditRows(CoreRow[]? rows)
  58. {
  59. if(rows is null)
  60. {
  61. var row = Table.NewRow(true);
  62. Table.Rows.Add(row);
  63. var dataRow = Data.NewRow();
  64. dataRow.LoadValues(row.Values);
  65. Data.Rows.Add(dataRow);
  66. _recordmap[dataRow] = row;
  67. InvalidateGrid();
  68. SelectedRows = [dataRow];
  69. DoChanged();
  70. return true;
  71. }
  72. else
  73. {
  74. // It's not obvious how to edit a row, without this being overriden by a subclass. Hence, we have turned off editing,
  75. // and the case that 'rows' is null is just basically adding a new row.
  76. return false;
  77. }
  78. }
  79. public override void DeleteRows(params CoreRow[] rows)
  80. {
  81. foreach(var row in rows)
  82. {
  83. // This relies on MasterData being equivalent to Table.
  84. if(_recordmap.Remove(row, out var tableRow))
  85. {
  86. Table.Rows.Remove(tableRow);
  87. }
  88. }
  89. }
  90. protected override void MoveRows(CoreRow[] rows, int index, bool isCopy = false)
  91. {
  92. CoreRow? targetRow;
  93. if(index < Data.Rows.Count)
  94. {
  95. var targetDataRow = Data.Rows[index];
  96. targetRow = _recordmap.GetValueOrDefault(targetDataRow);
  97. }
  98. else
  99. {
  100. var lastDataRow = Data.Rows.LastOrDefault();
  101. if(lastDataRow is null)
  102. {
  103. targetRow = null;
  104. }
  105. else if(!_recordmap.TryGetValue(lastDataRow, out var lastTargetRow))
  106. {
  107. targetRow = null;
  108. }
  109. else if(lastTargetRow == Table.Rows.LastOrDefault())
  110. {
  111. targetRow = null;
  112. }
  113. else
  114. {
  115. targetRow = Table.Rows[lastTargetRow.Index + 1];
  116. }
  117. }
  118. if (isCopy)
  119. {
  120. rows = rows.ToArray(x =>
  121. {
  122. var row = Table.NewRow();
  123. row.LoadValues(x.Values);
  124. return row;
  125. });
  126. }
  127. else
  128. {
  129. foreach(var row in rows)
  130. {
  131. if(_recordmap.TryGetValue(row, out var tableRow))
  132. {
  133. Table.Rows.Remove(tableRow);
  134. }
  135. }
  136. }
  137. if(targetRow is not null)
  138. {
  139. var idx = Table.Rows.IndexOf(targetRow);
  140. Table.Rows.InsertRange(idx, rows);
  141. }
  142. else
  143. {
  144. Table.Rows.AddRange(rows);
  145. }
  146. Refresh(false, true);
  147. SelectedRows = rows.Select(row =>
  148. {
  149. return _recordmap.FirstOrDefault(x => x.Value == row).Key;
  150. }).NotNull().ToArray();
  151. }
  152. #endregion
  153. #region Columns
  154. private IDynamicGridColumnSchema GetColumnSchema()
  155. {
  156. return ColumnSchema ?? new CoreTableColumnSchema(Table);
  157. }
  158. protected override bool SelectColumns([NotNullWhen(true)] out DynamicGridColumns? columns)
  159. {
  160. var editor = new DynamicGridColumnsEditor(GetColumnSchema(), null)
  161. {
  162. WindowStartupLocation = WindowStartupLocation.CenterScreen
  163. };
  164. editor.DirectEdit = IsDirectEditMode();
  165. editor.Columns.AddRange(VisibleColumns);
  166. if (editor.ShowDialog().Equals(true))
  167. {
  168. columns = editor.Columns;
  169. return true;
  170. }
  171. else
  172. {
  173. columns = null;
  174. return false;
  175. }
  176. }
  177. private DynamicGridColumns? _columns;
  178. protected override DynamicGridColumns LoadColumns()
  179. {
  180. return _columns ?? base.LoadColumns();
  181. }
  182. public override DynamicGridColumns GenerateColumns()
  183. {
  184. var schema = GetColumnSchema();
  185. var columns = new DynamicGridColumns();
  186. columns.AddRange(schema.ColumnNames.Select(schema.GetColumn));
  187. return columns;
  188. }
  189. protected override void SaveColumns(DynamicGridColumns columns)
  190. {
  191. _columns = columns;
  192. }
  193. #endregion
  194. #region Data
  195. protected override void ReloadData(CancellationToken token, Action<CoreTable?, Exception?> action)
  196. {
  197. // I think this is all we need to do. If we were to do paging or something like that, this would fail,
  198. // since we would be adding the table to itself; but for now this will suffice.
  199. action(Table, null);
  200. }
  201. public override void OnItemSourceChanged(object value)
  202. {
  203. if(value is CoreTable table)
  204. {
  205. Table = table;
  206. Refresh(true, true);
  207. }
  208. }
  209. #endregion
  210. }