DynamicGridCommon.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Windows.Controls;
  5. using InABox.Core;
  6. using InABox.Wpf;
  7. using Syncfusion.Data;
  8. namespace InABox.DynamicGrid;
  9. public abstract class DynamicColumnBase : BaseObject, IDynamicColumnBase
  10. {
  11. public void DoEntityChanged(string columnname, Dictionary<string,object?> changes)
  12. {
  13. EntityChanged?.Invoke(this, new DynamicColumnEntityChangedEventArgs(columnname, changes));
  14. }
  15. public event DynamicColumnEntityChangedEvent? EntityChanged;
  16. [DoNotSerialize]
  17. public object? Tag { get; set; }
  18. }
  19. public class DynamicGridOptions
  20. {
  21. public event Action? OnChanged;
  22. private int _enabled = 0;
  23. private bool _changed = false;
  24. public bool Enabled
  25. {
  26. get => _enabled == 0;
  27. set
  28. {
  29. if (value)
  30. EndUpdate();
  31. else
  32. BeginUpdate();
  33. }
  34. }
  35. public DynamicGridOptions Clear()
  36. {
  37. BeginUpdate();
  38. AddRows = false;
  39. EditRows = false;
  40. DeleteRows = false;
  41. FilterRows = false;
  42. SelectColumns = false;
  43. ExportData = false;
  44. ImportData = false;
  45. MultiSelect = false;
  46. DragSource = false;
  47. DragTarget = false;
  48. ReorderRows = false;
  49. DirectEdit = false;
  50. ShowHelp = false;
  51. Print = false;
  52. RecordCount = false;
  53. HideDatabaseFilters = false;
  54. HideDirectEditButton = false;
  55. PageSize = 0;
  56. NonModalEditorHost = null;
  57. ReadOnly = false;
  58. return EndUpdate();
  59. }
  60. public DynamicGridOptions BeginUpdate()
  61. {
  62. ++_enabled;
  63. return this;
  64. }
  65. private DynamicGridOptions Changed()
  66. {
  67. if (_enabled == 0)
  68. {
  69. _changed = false;
  70. OnChanged?.Invoke();
  71. }
  72. else
  73. {
  74. _changed = true;
  75. }
  76. return this;
  77. }
  78. public DynamicGridOptions EndUpdate()
  79. {
  80. --_enabled;
  81. if(_changed)
  82. {
  83. Changed();
  84. }
  85. return this;
  86. }
  87. private bool _addRows;
  88. public bool AddRows
  89. {
  90. get => _addRows && !ReadOnly;
  91. set
  92. {
  93. if(_addRows != value)
  94. {
  95. _addRows = value;
  96. Changed();
  97. }
  98. }
  99. }
  100. private bool _editRows;
  101. public bool EditRows
  102. {
  103. get => _editRows && !ReadOnly;
  104. set
  105. {
  106. if(_editRows != value)
  107. {
  108. _editRows = value;
  109. Changed();
  110. }
  111. }
  112. }
  113. private bool _deleteRows;
  114. public bool DeleteRows
  115. {
  116. get => _deleteRows && !ReadOnly;
  117. set
  118. {
  119. if(_deleteRows != value)
  120. {
  121. _deleteRows = value;
  122. Changed();
  123. }
  124. }
  125. }
  126. private bool _filterRows;
  127. public bool FilterRows
  128. {
  129. get => _filterRows;
  130. set
  131. {
  132. if(_filterRows != value)
  133. {
  134. _filterRows = value;
  135. Changed();
  136. }
  137. }
  138. }
  139. private bool _selectColumns;
  140. public bool SelectColumns
  141. {
  142. get => _selectColumns;
  143. set
  144. {
  145. if(_selectColumns != value)
  146. {
  147. _selectColumns = value;
  148. Changed();
  149. }
  150. }
  151. }
  152. private bool _exportData;
  153. public bool ExportData
  154. {
  155. get => _exportData;
  156. set
  157. {
  158. if(_exportData != value)
  159. {
  160. _exportData = value;
  161. Changed();
  162. }
  163. }
  164. }
  165. private bool _importData;
  166. public bool ImportData
  167. {
  168. get => _importData;
  169. set
  170. {
  171. if(_importData != value)
  172. {
  173. _importData = value;
  174. Changed();
  175. }
  176. }
  177. }
  178. private bool _multiSelect;
  179. public bool MultiSelect
  180. {
  181. get => _multiSelect;
  182. set
  183. {
  184. if(_multiSelect != value)
  185. {
  186. _multiSelect = value;
  187. Changed();
  188. }
  189. }
  190. }
  191. private bool _dragSource;
  192. public bool DragSource
  193. {
  194. get => _dragSource;
  195. set
  196. {
  197. if(_dragSource != value)
  198. {
  199. _dragSource = value;
  200. Changed();
  201. }
  202. }
  203. }
  204. private bool _dragTarget;
  205. public bool DragTarget
  206. {
  207. get => _dragTarget;
  208. set
  209. {
  210. if(_dragTarget != value)
  211. {
  212. _dragTarget = value;
  213. Changed();
  214. }
  215. }
  216. }
  217. private bool _reorderRows;
  218. /// <summary>
  219. /// Allow re-ordering rows within this grid, including cut+paste functionality, and row dragging.
  220. /// This is never <see langword="true"/> if <see cref="EditRows"/> is not enabled.
  221. /// </summary>
  222. /// <remarks>
  223. /// <see cref="DragSource"/> and <see cref="DragTarget"/> deal with external dragging, whereas this deals with internal dragging.
  224. /// <br/>
  225. /// Note that this requires the <see cref="DynamicGrid{T}.MoveRows(CoreRow[], int)"/> function; the default implementation relies on the items
  226. /// being <see cref="ISequenceable"/>, but this can be overriden.
  227. /// </remarks>
  228. public bool ReorderRows
  229. {
  230. get => _reorderRows && EditRows;
  231. set
  232. {
  233. if(_reorderRows != value)
  234. {
  235. _reorderRows = value;
  236. Changed();
  237. }
  238. }
  239. }
  240. private bool _directEdit;
  241. public bool DirectEdit
  242. {
  243. get => _directEdit && !ReadOnly;
  244. set
  245. {
  246. if(_directEdit != value)
  247. {
  248. _directEdit = value;
  249. Changed();
  250. }
  251. }
  252. }
  253. private bool _showHelp;
  254. public bool ShowHelp
  255. {
  256. get => _showHelp;
  257. set
  258. {
  259. if(_showHelp != value)
  260. {
  261. _showHelp = value;
  262. Changed();
  263. }
  264. }
  265. }
  266. private bool _print;
  267. public bool Print
  268. {
  269. get => _print;
  270. set
  271. {
  272. if(_print != value)
  273. {
  274. _print = value;
  275. Changed();
  276. }
  277. }
  278. }
  279. private bool _recordCount;
  280. public bool RecordCount
  281. {
  282. get => _recordCount;
  283. set
  284. {
  285. if(_recordCount != value)
  286. {
  287. _recordCount = value;
  288. Changed();
  289. }
  290. }
  291. }
  292. private bool _hideDatabaseFilters;
  293. public bool HideDatabaseFilters
  294. {
  295. get => _hideDatabaseFilters;
  296. set
  297. {
  298. if(_hideDatabaseFilters != value)
  299. {
  300. _hideDatabaseFilters = value;
  301. Changed();
  302. }
  303. }
  304. }
  305. private bool _hideDirectEditButton;
  306. public bool HideDirectEditButton
  307. {
  308. get => _hideDirectEditButton;
  309. set
  310. {
  311. if(_hideDirectEditButton != value)
  312. {
  313. _hideDirectEditButton = value;
  314. Changed();
  315. }
  316. }
  317. }
  318. private ISubPanelHost? _nonModalEditorHost;
  319. public ISubPanelHost? NonModalEditorHost
  320. {
  321. get => _nonModalEditorHost;
  322. set
  323. {
  324. if(_nonModalEditorHost != value)
  325. {
  326. _nonModalEditorHost = value;
  327. Changed();
  328. }
  329. }
  330. }
  331. private int _pageSize = 0;
  332. /// <summary>
  333. /// The page size for loading data in pages; set to 0 for no paging functionality.
  334. /// </summary>
  335. public int PageSize
  336. {
  337. get => _pageSize;
  338. set
  339. {
  340. if(_pageSize != value)
  341. {
  342. _pageSize = value;
  343. Changed();
  344. }
  345. }
  346. }
  347. public bool _readOnly = false;
  348. /// <summary>
  349. /// Specifies whether this grid is "read-only"; if this is <see langword="true"/>, then the <see cref="AddRows"/>, <see
  350. /// cref="EditRows"/>, <see cref="DeleteRows"/> and <see cref="DirectEdit"/> will be disabled.
  351. /// </summary>
  352. /// <remarks>
  353. /// Setting this property can improve performance, since it allows the grid to not load <see cref="LookupFactory.RequiredColumns(Type)"/>.
  354. /// </remarks>
  355. public bool ReadOnly
  356. {
  357. get => _readOnly;
  358. set
  359. {
  360. if(_readOnly != value)
  361. {
  362. _readOnly = value;
  363. Changed();
  364. }
  365. }
  366. }
  367. }
  368. public delegate bool OnFilterRecord(CoreRow row);
  369. public delegate void OnCreateItem(object sender, BaseObject item);
  370. public delegate bool OnAfterCreateItem(object sender, BaseObject item);
  371. public delegate T OnCreateItem<T>();
  372. public delegate void OnDefineLookup(ILookupEditorControl editor);
  373. public delegate void OnGridCustomiseEditor(DynamicEditorGrid sender, DynamicGridColumn column, BaseEditor editor);
  374. public delegate void OnFormCustomiseEditor(IDynamicEditorForm sender, object[] items, DynamicGridColumn column, BaseEditor editor);
  375. public delegate void ValidateEvent<T>(DynamicGrid<T> sender, T[] items, List<string> errors)
  376. where T : BaseObject, new();
  377. /// <summary>
  378. ///
  379. /// </summary>
  380. /// <typeparam name="T"></typeparam>
  381. /// <param name="sender"></param>
  382. /// <param name="items">The array of items being edited; <see langword="null"/> is synonymous with an empty array.</param>
  383. /// <param name="column"></param>
  384. /// <param name="editor"></param>
  385. public delegate void OnCustomiseEditor<T>(IDynamicEditorForm sender, T[]? items, DynamicGridColumn column, BaseEditor editor);
  386. public delegate void OnCustomiseEditor(IDynamicEditorForm sender, object[]? items, DynamicGridColumn column, BaseEditor editor);
  387. public delegate void OnLoadEditorButtons<T>(T item, DynamicEditorButtons buttons);
  388. public delegate void OnReconfigureEditors(DynamicEditorGrid sender);
  389. public delegate void OnCreateEditorControl(string column, BaseEditor editor, IDynamicEditorControl control);
  390. public class AfterEditorValueChangedArgs
  391. {
  392. public string ColumnName { get; set; }
  393. public Dictionary<string, object?> ChangedValues { get; set; }
  394. public AfterEditorValueChangedArgs(string columnName, Dictionary<string, object?> changedValues)
  395. {
  396. ColumnName = columnName;
  397. ChangedValues = changedValues;
  398. }
  399. }
  400. public delegate Dictionary<string, object?>? OnAfterEditorValueChanged(DynamicEditorGrid sender, AfterEditorValueChangedArgs args);
  401. //public delegate void OnGridChanged(IDynamicGrid sender);
  402. public delegate void OnLoadPage(IDynamicEditorPage page);
  403. public delegate void OnSelectPage(DynamicEditorGrid sender, BaseObject[]? items);
  404. public delegate void OnUnloadPage(IDynamicEditorPage page, bool saved);
  405. public delegate void OnCustomiseColumns(object sender, DynamicGridColumns columns);
  406. public delegate BaseEditor? OnGetEditor(DynamicGridColumn column);
  407. public delegate decimal OnGetEditorSequence(DynamicGridColumn column);
  408. public delegate IFilter? OnDefineLookupFilter(Type type, string column);
  409. public delegate IFilter? OnDefineFilter(Type type);
  410. public delegate IList<string>? OnValidateData(IDynamicEditorForm sender, BaseObject[] items);
  411. public delegate void OnPrintData(object sender);
  412. public delegate void EntitySaveEvent(IDynamicEditorForm editor, BaseObject[] items);
  413. public delegate bool DynamicGridButtonClickEvent(Button button, CoreRow[] rows);
  414. public delegate void OnContextMenuOpening<TKey>(CoreTreeNode<TKey>? node, ContextMenu menu);
  415. public class DynamicGridSelectionEventArgs : CancelEventArgs
  416. {
  417. public DynamicGridSelectionEventArgs(CoreRow[]? rows)
  418. {
  419. Rows = rows;
  420. }
  421. public CoreRow[]? Rows { get; }
  422. }
  423. public delegate void SelectItemHandler(object sender, DynamicGridSelectionEventArgs e);
  424. public delegate void OnDoubleClick(object sender, HandledEventArgs args);
  425. public class DynamicGridCellClickEventArgs : HandledEventArgs
  426. {
  427. public CoreRow? Row { get; set; }
  428. public DynamicColumnBase? Column { get; set; }
  429. public DynamicGridCellClickEventArgs(CoreRow? row, DynamicColumnBase? column)
  430. {
  431. Row = row;
  432. Column = column;
  433. }
  434. }
  435. public delegate void OnCellDoubleClick(object sender, DynamicGridCellClickEventArgs args);
  436. public class BeforeRefreshEventArgs : CancelEventArgs { }
  437. public delegate void BeforeRefreshEventHandler(object sender, BeforeRefreshEventArgs args);
  438. public class AfterRefreshEventArgs : EventArgs { }
  439. public delegate void AfterRefreshEventHandler(object sender, AfterRefreshEventArgs args);
  440. public class GenerateColumnsEventArgs
  441. {
  442. public DynamicGridColumns Columns { get; private set; }
  443. public GenerateColumnsEventArgs(DynamicGridColumns columns)
  444. {
  445. Columns = columns;
  446. }
  447. }
  448. public delegate void GenerateColumnsEvent(object sender, GenerateColumnsEventArgs args);
  449. public class SaveColumnsEventArgs
  450. {
  451. public DynamicGridColumns Columns { get; private set; }
  452. public SaveColumnsEventArgs(DynamicGridColumns columns)
  453. {
  454. Columns = columns;
  455. }
  456. }
  457. public delegate void SaveColumnsEvent(object sender, SaveColumnsEventArgs args);
  458. public class GetAvailableColumnsEventArgs(List<DynamicGridColumn> columns)
  459. {
  460. public List<DynamicGridColumn> Columns { get; set; } = columns;
  461. }
  462. public delegate void GetAvailableColumnsEvent(GetAvailableColumnsEventArgs args);