DynamicItemsListGrid.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using InABox.Core;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Microsoft.CodeAnalysis.CSharp.Syntax;
  9. using Syncfusion.Windows.Tools.Controls;
  10. using System.Threading;
  11. namespace InABox.DynamicGrid;
  12. public interface IDynamicItemsListGrid : IDynamicGrid
  13. {
  14. /// <summary>
  15. /// The items list that forms the source for the rows of this grid
  16. /// </summary>
  17. /// <remarks>
  18. /// <b>Note:</b> This must be a list of type <see cref="List{T}"/>, otherwise the assignment to this property <u>will not</u> work.
  19. /// </remarks>
  20. IList Items { get; set; }
  21. }
  22. public class DynamicItemsListGrid<T> : DynamicGrid<T>, IDynamicItemsListGrid
  23. where T : BaseObject, new()
  24. {
  25. private List<T> _items = [];
  26. public List<T> Items
  27. {
  28. get => _items;
  29. set => _items = value;
  30. }
  31. IList IDynamicItemsListGrid.Items
  32. {
  33. get => _items;
  34. set => _items = value as List<T> ?? new List<T>();
  35. }
  36. protected override void Init()
  37. {
  38. }
  39. protected override void DoReconfigure(DynamicGridOptions options)
  40. {
  41. }
  42. public override void DeleteItems(params CoreRow[] rows)
  43. {
  44. foreach (var row in rows.OrderByDescending(x => x.Index))
  45. {
  46. Items.RemoveAt(_recordmap[row].Index);
  47. }
  48. }
  49. public override T LoadItem(CoreRow row)
  50. {
  51. return Items[_recordmap[row].Index];
  52. }
  53. protected override void Reload(
  54. Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sort,
  55. CancellationToken token, Action<CoreTable?, Exception?> action)
  56. {
  57. var result = new CoreTable();
  58. result.LoadColumns(columns);
  59. result.LoadRows(Items);
  60. action.Invoke(result, null);
  61. }
  62. public override void SaveItem(T item)
  63. {
  64. if (!Items.Contains(item))
  65. {
  66. Items.Add(item);
  67. }
  68. if (item is ISequenceable)
  69. {
  70. Items.Sort((a, b) => (a as ISequenceable)!.Sequence.CompareTo((b as ISequenceable)!.Sequence));
  71. }
  72. }
  73. protected override bool BeforeCopy(IList<T> items)
  74. {
  75. if (!base.BeforeCopy(items)) return false;
  76. for(int i = 0; i < items.Count; ++i)
  77. {
  78. items[i] = items[i].Clone();
  79. }
  80. return true;
  81. }
  82. }