DynamicItemsListGrid.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. public override void DeleteItems(params CoreRow[] rows)
  37. {
  38. foreach (var row in rows.OrderByDescending(x => x.Index))
  39. {
  40. Items.RemoveAt(_recordmap[row].Index);
  41. }
  42. }
  43. public override T LoadItem(CoreRow row)
  44. {
  45. return Items[_recordmap[row].Index];
  46. }
  47. protected override void Reload(
  48. Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sort,
  49. CancellationToken token, Action<CoreTable?, Exception?> action)
  50. {
  51. var result = new CoreTable();
  52. result.LoadColumns(columns);
  53. result.LoadRows(Items);
  54. action.Invoke(result, null);
  55. }
  56. public override void SaveItem(T item)
  57. {
  58. if (!Items.Contains(item))
  59. {
  60. Items.Add(item);
  61. }
  62. if (item is ISequenceable)
  63. {
  64. Items.Sort((a, b) => (a as ISequenceable)!.Sequence.CompareTo((b as ISequenceable)!.Sequence));
  65. }
  66. }
  67. protected override bool BeforeCopy(IList<T> items)
  68. {
  69. if (!base.BeforeCopy(items)) return false;
  70. for(int i = 0; i < items.Count; ++i)
  71. {
  72. items[i] = items[i].Clone();
  73. }
  74. return true;
  75. }
  76. }