DeletedEntityGrid.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using FastReport;
  2. using InABox.Core;
  3. using InABox.Database;
  4. using InABox.DynamicGrid;
  5. using InABox.WPF;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace PRSServer.Forms;
  15. class DeletedEntityGrid<T> : DynamicGrid<T>, ISpecificGrid where T : Entity, new()
  16. {
  17. private CoreTable Table { get; set; }
  18. public DeletedEntityGrid(CoreTable table)
  19. {
  20. Table = table;
  21. HiddenColumns.Add(x => x.ID);
  22. ActionColumns.Add(new DynamicImageColumn(Properties.Resources.view.AsBitmapImage(), ViewClick));
  23. }
  24. protected override void Init()
  25. {
  26. }
  27. protected override void DoReconfigure(DynamicGridOptions options)
  28. {
  29. options.Clear();
  30. options.EditRows = true;
  31. }
  32. protected override DynamicGridColumns LoadColumns()
  33. {
  34. var result = base.LoadColumns();
  35. var deletionColumns = DeletionData.DeletionColumns<T>();
  36. var columns = new DynamicGridColumns();
  37. foreach(var column in deletionColumns)
  38. {
  39. var resultColumn = result.FirstOrDefault(x => x.ColumnName == column.Property);
  40. if(resultColumn is not null)
  41. {
  42. columns.Add(resultColumn);
  43. }
  44. }
  45. if(columns.Count == 0)
  46. {
  47. columns.Add<T>(x => x.ID, 0, "ID", "", Alignment.MiddleLeft);
  48. }
  49. return columns;
  50. }
  51. protected override void Reload(
  52. Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sort,
  53. CancellationToken token, Action<CoreTable?, Exception?> action)
  54. {
  55. try
  56. {
  57. action(Table, null);
  58. }
  59. catch(Exception e)
  60. {
  61. action(null, e);
  62. }
  63. }
  64. public override T LoadItem(CoreRow row)
  65. {
  66. return Table.Rows[row.Index].ToObject<T>();
  67. }
  68. private bool ViewClick(CoreRow? row)
  69. {
  70. if(row is null) return false;
  71. ViewItem(LoadItem(row));
  72. return false;
  73. }
  74. private void ViewItem(T item)
  75. {
  76. var editor = new DynamicEditorForm(typeof(T))
  77. {
  78. ReadOnly = true
  79. };
  80. editor.OnFormCustomiseEditor += Editor_OnFormCustomiseEditor;
  81. editor.OnDefineLookups += sender => DefineLookups(sender, Array.Empty<T>());
  82. editor.Items = new BaseObject[] { item };
  83. editor.ShowDialog();
  84. }
  85. private Columns<T>? _deletionColumns;
  86. private void Editor_OnFormCustomiseEditor(IDynamicEditorForm sender, object[] items, DynamicGridColumn column, BaseEditor editor)
  87. {
  88. _deletionColumns ??= DeletionData.DeletionColumns<T>();
  89. if(!_deletionColumns.Any(y => y.Property == column.ColumnName))
  90. {
  91. editor.Editable = Editable.Hidden;
  92. }
  93. }
  94. protected override void DoEdit()
  95. {
  96. if (!SelectedRows.Any())
  97. return;
  98. ViewItem(LoadItem(SelectedRows.First()));
  99. }
  100. public override void SaveItem(T item)
  101. {
  102. // No saving allowed
  103. }
  104. public override void DeleteItems(params CoreRow[] rows)
  105. {
  106. // No deleting allowed
  107. }
  108. }