DeletedEntityGrid.cs 3.5 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.Tasks;
  12. using System.Windows.Forms;
  13. namespace PRSServer.Forms
  14. {
  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(FluentList<DynamicGridOption> options)
  28. {
  29. options.BeginUpdate().Clear()
  30. .AddRange(DynamicGridOption.EditRows).EndUpdate();
  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.GetColumns())
  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, Guid>(x => x.ID, 0, "ID", "", Alignment.MiddleLeft);
  48. }
  49. return columns;
  50. }
  51. protected override void Reload(Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sort, Action<CoreTable?, Exception?> action)
  52. {
  53. try
  54. {
  55. action(Table, null);
  56. }
  57. catch(Exception e)
  58. {
  59. action(null, e);
  60. }
  61. }
  62. protected override T LoadItem(CoreRow row)
  63. {
  64. return Table.Rows[row.Index].ToObject<T>();
  65. }
  66. private bool ViewClick(CoreRow? row)
  67. {
  68. if(row is null) return false;
  69. ViewItem(LoadItem(row));
  70. return false;
  71. }
  72. private void ViewItem(T item)
  73. {
  74. var editor = new DynamicEditorForm(typeof(T))
  75. {
  76. ReadOnly = true
  77. };
  78. editor.OnCustomiseColumns += Editor_OnCustomiseColumns;
  79. editor.OnDefineLookups += sender => DefineLookups(sender, Array.Empty<T>());
  80. editor.Items = new BaseObject[] { item };
  81. editor.ShowDialog();
  82. }
  83. protected override void DoEdit()
  84. {
  85. if (!SelectedRows.Any())
  86. return;
  87. ViewItem(LoadItem(SelectedRows.First()));
  88. }
  89. private void Editor_OnCustomiseColumns(object sender, DynamicGridColumns columns)
  90. {
  91. columns.Clear();
  92. columns.ExtractColumns(typeof(T));
  93. var deletionColumns = DeletionData.DeletionColumns<T>();
  94. columns.RemoveAll(x => !deletionColumns.GetColumns().Any(y => y.Property == x.ColumnName));
  95. }
  96. public override void SaveItem(T item)
  97. {
  98. // No saving allowed
  99. }
  100. protected override void DeleteItems(params CoreRow[] rows)
  101. {
  102. // No deleting allowed
  103. }
  104. }
  105. }