DeletedEntityGrid.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 DoReconfigure(DynamicGridOptions options)
  25. {
  26. options.Clear();
  27. options.EditRows = true;
  28. }
  29. protected override DynamicGridColumns LoadColumns()
  30. {
  31. var result = base.LoadColumns();
  32. var deletionColumns = DeletionData.DeletionColumns<T>();
  33. var columns = new DynamicGridColumns();
  34. foreach(var column in deletionColumns)
  35. {
  36. var resultColumn = result.FirstOrDefault(x => x.ColumnName == column.Property);
  37. if(resultColumn is not null)
  38. {
  39. columns.Add(resultColumn);
  40. }
  41. }
  42. if(columns.Count == 0)
  43. {
  44. columns.Add<T>(x => x.ID, 0, "ID", "", Alignment.MiddleLeft);
  45. }
  46. return columns;
  47. }
  48. protected override void Reload(
  49. Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sort,
  50. CancellationToken token, Action<CoreTable?, Exception?> action)
  51. {
  52. try
  53. {
  54. action(Table, null);
  55. }
  56. catch(Exception e)
  57. {
  58. action(null, e);
  59. }
  60. }
  61. public override T LoadItem(CoreRow row)
  62. {
  63. return Table.Rows[row.Index].ToObject<T>();
  64. }
  65. private bool ViewClick(CoreRow? row)
  66. {
  67. if(row is null) return false;
  68. ViewItem(LoadItem(row));
  69. return false;
  70. }
  71. private void ViewItem(T item)
  72. {
  73. var editor = new DynamicEditorForm(typeof(T))
  74. {
  75. ReadOnly = true
  76. };
  77. editor.OnFormCustomiseEditor += Editor_OnFormCustomiseEditor;
  78. editor.OnDefineLookups += sender => DefineLookups(sender, Array.Empty<T>());
  79. editor.Items = new BaseObject[] { item };
  80. editor.ShowDialog();
  81. }
  82. private Columns<T>? _deletionColumns;
  83. private void Editor_OnFormCustomiseEditor(IDynamicEditorForm sender, object[] items, DynamicGridColumn column, BaseEditor editor)
  84. {
  85. _deletionColumns ??= DeletionData.DeletionColumns<T>();
  86. if(!_deletionColumns.Any(y => y.Property == column.ColumnName))
  87. {
  88. editor.Editable = Editable.Hidden;
  89. }
  90. }
  91. protected override void DoEdit()
  92. {
  93. if (!SelectedRows.Any())
  94. return;
  95. ViewItem(LoadItem(SelectedRows.First()));
  96. }
  97. public override void SaveItem(T item)
  98. {
  99. // No saving allowed
  100. }
  101. public override void DeleteItems(params CoreRow[] rows)
  102. {
  103. // No deleting allowed
  104. }
  105. }