DynamicEditorGrid.xaml.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.WPF;
  11. namespace InABox.DynamicGrid
  12. {
  13. public delegate void OnUpdateOtherEditorHandler(string columnname, object value);
  14. public delegate Dictionary<string, object?> EditorValueChangedHandler(object sender, string name, object value);
  15. /// <summary>
  16. /// Interaction logic for DynamicEditorGrid.xaml
  17. /// </summary>
  18. public partial class DynamicEditorGrid : UserControl
  19. {
  20. public delegate void EditorCreatedHandler(object sender, double height, double width);
  21. public delegate Document? FindDocumentEvent(string FileName);
  22. public delegate Document? GetDocumentEvent(Guid id);
  23. public delegate object? GetPropertyValueHandler(object sender, string name);
  24. public delegate void SaveDocumentEvent(Document document);
  25. public delegate void SetPropertyValueHandler(object sender, string name, object value);
  26. public delegate object?[] GetItemsEvent();
  27. // Column Definitions as defined by calling model
  28. private DynamicGridColumns _columns = new();
  29. private Type? LayoutType;
  30. private DynamicEditorGridLayout? Layout;
  31. public DynamicEditorGrid()
  32. {
  33. InitializeComponent();
  34. Loaded += DynamicEditorGrid_Loaded;
  35. }
  36. public DynamicEditorPages Pages { get; private set; } = new();
  37. public bool PreloadPages { get; set; }
  38. public Type UnderlyingType { get; set; }
  39. public OnLoadPage? OnLoadPage { get; set; }
  40. public OnSelectPage? OnSelectPage { get; set; }
  41. public OnUnloadPage? OnUnloadPage { get; set; }
  42. public DynamicGridColumns Columns => _columns;
  43. public bool TryFindEditor(string columnname, [NotNullWhen(true)] out IDynamicEditorControl? editor)
  44. {
  45. foreach (var page in Pages)
  46. {
  47. if (page is DynamicEditPage editPage)
  48. {
  49. if (editPage.TryFindEditor(columnname, out editor))
  50. return true;
  51. }
  52. }
  53. editor = null;
  54. return false;
  55. }
  56. public IDynamicEditorControl? FindEditor(string columnname)
  57. {
  58. TryFindEditor(columnname, out var editor);
  59. return editor;
  60. }
  61. public virtual void ReconfigureEditors()
  62. {
  63. OnReconfigureEditors?.Invoke(this);
  64. }
  65. public object? GetPropertyValue(string columnname)
  66. {
  67. return OnGetPropertyValue?.Invoke(this, columnname);
  68. }
  69. public event EditorCreatedHandler? OnEditorCreated;
  70. public event OnCustomiseColumns? OnCustomiseColumns;
  71. public event OnGetEditor? OnGetEditor;
  72. public event OnGridCustomiseEditor? OnGridCustomiseEditor;
  73. public event OnGetEditorSequence? OnGetSequence;
  74. public event GetPropertyValueHandler? OnGetPropertyValue;
  75. public event SetPropertyValueHandler? OnSetPropertyValue;
  76. public event EditorValueChangedHandler? OnEditorValueChanged;
  77. public event OnAfterEditorValueChanged? OnAfterEditorValueChanged;
  78. public event OnReconfigureEditors? OnReconfigureEditors;
  79. public event OnDefineFilter? OnDefineFilter;
  80. public event OnDefineLookup? OnDefineLookups;
  81. public event OnLookupsDefined? OnLookupsDefined;
  82. public event GetDocumentEvent? OnGetDocument;
  83. public event FindDocumentEvent? OnFindDocument;
  84. public event SaveDocumentEvent? OnSaveDocument;
  85. public event GetItemsEvent? GetItems;
  86. private void DynamicEditorGrid_Loaded(object sender, RoutedEventArgs e)
  87. {
  88. //Reload();
  89. }
  90. public void Reload()
  91. {
  92. LoadPages();
  93. ReconfigureEditors();
  94. }
  95. #region Edit Page
  96. public class DynamicEditPage : ContentControl, IDynamicEditorPage
  97. {
  98. private Grid Grid;
  99. public DynamicEditorGrid EditorGrid { get; set; } = null!; // Set by DynamicEditorGrid
  100. public bool Ready { get; set; }
  101. private List<BaseDynamicEditorControl> Editors { get; set; }
  102. public PageType PageType => PageType.Editor;
  103. public int PageOrder { get; set; }
  104. public string Header { get; set; }
  105. private double GeneralHeight = 30;
  106. public DynamicEditPage(string header)
  107. {
  108. Header = header;
  109. Editors = new List<BaseDynamicEditorControl>();
  110. InitialiseContent();
  111. }
  112. public void AddEditor(string columnName, BaseEditor editor)
  113. {
  114. BaseDynamicEditorControl? element = null;
  115. element = editor switch
  116. {
  117. TextBoxEditor => new TextBoxEditorControl(),
  118. Core.RichTextEditor => new RichTextEditorControl(),
  119. URLEditor => new URLEditorControl(),
  120. CodeEditor or UniqueCodeEditor => new CodeEditorControl(),
  121. CheckBoxEditor => new CheckBoxEditorControl(),
  122. DateTimeEditor => new DateTimeEditorControl(),
  123. DateEditor dateEditor => new DateEditorControl { TodayVisible = dateEditor.TodayVisible },
  124. TimeOfDayEditor => new TimeOfDayEditorControl { NowButtonVisible = false },
  125. DurationEditor => new DurationEditorControl(),
  126. NotesEditor => new NotesEditorControl(),
  127. PINEditor => new PINEditorControl(),
  128. CheckListEditor => new CheckListBoxEditorControl(),
  129. MemoEditor => new MemoEditorControl(),
  130. JsonEditor => new JsonEditorControl(),
  131. LookupEditor => ClientFactory.IsSupported(((LookupEditor)editor).Type) ? new LookupEditorControl() : null,
  132. PopupEditor => ClientFactory.IsSupported(((PopupEditor)editor).Type) ? new PopupEditorControl() : null,
  133. CodePopupEditor => ClientFactory.IsSupported(((CodePopupEditor)editor).Type) ? new CodePopupEditorControl() : null,
  134. EnumLookupEditor or ComboLookupEditor => new LookupEditorControl(),
  135. ComboMultiLookupEditor => new MultiLookupEditorControl(),
  136. EmbeddedImageEditor imageEditor => new EmbeddedImageEditorControl
  137. {
  138. MaximumHeight = imageEditor.MaximumHeight,
  139. MaximumWidth = imageEditor.MaximumWidth,
  140. MaximumFileSize = imageEditor.MaximumFileSize
  141. },
  142. FileNameEditor fileNameEditor => new FileNameEditorControl
  143. {
  144. Filter = fileNameEditor.FileMask,
  145. AllowView = fileNameEditor.AllowView,
  146. RequireExisting = fileNameEditor.RequireExisting
  147. },
  148. FolderEditor folderEditor => new FolderEditorControl
  149. {
  150. InitialFolder = folderEditor.InitialFolder
  151. },
  152. MiscellaneousDocumentEditor => new DocumentEditorControl(),
  153. ImageDocumentEditor => new DocumentEditorControl(),
  154. VectorDocumentEditor => new DocumentEditorControl(),
  155. PDFDocumentEditor => new DocumentEditorControl(),
  156. PasswordEditor => new PasswordEditorControl(),
  157. CurrencyEditor => new CurrencyEditorControl(),
  158. DoubleEditor => new DoubleEditorControl(),
  159. IntegerEditor => new IntegerEditorControl(),
  160. Core.ScriptEditor scriptEditor => new ScriptEditorControl
  161. {
  162. SyntaxLanguage = scriptEditor.SyntaxLanguage
  163. },
  164. ButtonEditor buttonEditor => new ButtonEditorControl()
  165. {
  166. Label = buttonEditor.Label,
  167. Command = buttonEditor.CreateCommand()
  168. },
  169. EmbeddedListEditor listEditor => new EmbeddedListEditorControl()
  170. {
  171. DataType = listEditor.DataType,
  172. Label = listEditor.Label
  173. },
  174. TimestampEditor => new TimestampEditorControl(),
  175. ColorEditor => new ColorEditorControl(),
  176. FilterEditor filter => new FilterEditorControl { FilterType = filter.Type! },
  177. ExpressionEditor expression => new ExpressionEditorControl(expression),
  178. _ => null,
  179. };
  180. if (element != null)
  181. {
  182. element.EditorDefinition = editor;
  183. element.IsEnabled = editor.Editable == Editable.Enabled;
  184. if (!string.IsNullOrWhiteSpace(editor.ToolTip))
  185. {
  186. element.ToolTip = new ToolTip() { Content = editor.ToolTip };
  187. }
  188. var label = new Label();
  189. label.Content = CoreUtils.Neatify(editor.Caption); // 2
  190. label.Margin = new Thickness(0F, 0F, 0F, 0F);
  191. label.HorizontalAlignment = HorizontalAlignment.Stretch;
  192. label.VerticalAlignment = VerticalAlignment.Stretch;
  193. label.HorizontalContentAlignment = HorizontalAlignment.Left;
  194. label.VerticalContentAlignment = VerticalAlignment.Center;
  195. label.SetValue(Grid.RowProperty, Grid.RowDefinitions.Count);
  196. label.SetValue(Grid.ColumnProperty, 0);
  197. label.Visibility = string.IsNullOrWhiteSpace(editor.Caption) ? Visibility.Collapsed : Visibility.Visible;
  198. Grid.Children.Add(label);
  199. element.ColumnName = columnName;
  200. element.Color = editor is UniqueCodeEditor ? Color.FromArgb(0xFF, 0xF6, 0xC9, 0xE8) : Colors.LightYellow;
  201. Editors.Add(element);
  202. element.Margin = new Thickness(5F, 2.5F, 5F, 2.5F);
  203. double iHeight = element.DesiredHeight();
  204. if (iHeight == int.MaxValue)
  205. {
  206. Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
  207. GeneralHeight += element.MinHeight + 5.0F;
  208. }
  209. else
  210. {
  211. Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(iHeight + 5.0F) });
  212. GeneralHeight += iHeight + 5.0F;
  213. }
  214. double iWidth = element.DesiredWidth();
  215. if (iWidth == int.MaxValue)
  216. {
  217. element.HorizontalAlignment = HorizontalAlignment.Stretch;
  218. }
  219. else
  220. {
  221. element.HorizontalAlignment = HorizontalAlignment.Left;
  222. element.Width = iWidth;
  223. }
  224. element.SetValue(Grid.RowProperty, Grid.RowDefinitions.Count - 1);
  225. element.SetValue(Grid.ColumnProperty, 1);
  226. Grid.Children.Add(element);
  227. }
  228. }
  229. [MemberNotNull(nameof(Grid))]
  230. private void InitialiseContent()
  231. {
  232. Grid = new Grid
  233. {
  234. HorizontalAlignment = HorizontalAlignment.Stretch,
  235. VerticalAlignment = VerticalAlignment.Stretch,
  236. Margin = new Thickness(0, 2.5, 0, 2.5)
  237. };
  238. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
  239. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  240. var scroll = new ScrollViewer
  241. {
  242. HorizontalAlignment = HorizontalAlignment.Stretch,
  243. VerticalAlignment = VerticalAlignment.Stretch,
  244. VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
  245. Padding = new Thickness(2),
  246. Content = Grid
  247. };
  248. var border = new Border
  249. {
  250. BorderBrush = new SolidColorBrush(Colors.Gray),
  251. Background = new SolidColorBrush(Colors.White),
  252. BorderThickness = new Thickness(0.75),
  253. Child = scroll
  254. };
  255. Content = border;
  256. }
  257. public void AfterSave(object item)
  258. {
  259. }
  260. public void BeforeSave(object item)
  261. {
  262. }
  263. public string Caption() => Header;
  264. public bool TryFindEditor(string columnname, [NotNullWhen(true)] out IDynamicEditorControl? editor)
  265. {
  266. editor = Editors.FirstOrDefault(x => x.ColumnName.Equals(columnname));
  267. return editor is not null;
  268. }
  269. public IEnumerable<BaseDynamicEditorControl> FindEditors(DynamicGridColumn column)
  270. {
  271. return Editors.Where(x => string.Equals(x.ColumnName, column.ColumnName));
  272. }
  273. #region Configure Editors
  274. private void LoadLookupColumns(string column, Dictionary<string, string> othercolumns)
  275. {
  276. othercolumns.Clear();
  277. var comps = column.Split('.').ToList();
  278. comps.RemoveAt(comps.Count - 1);
  279. var prefix = string.Format("{0}.", string.Join(".", comps));
  280. var cols = EditorGrid.Columns.Where(x => !x.ColumnName.Equals(column) && x.ColumnName.StartsWith(prefix));
  281. foreach (var col in cols)
  282. othercolumns[col.ColumnName.Replace(prefix, "")] = col.ColumnName;
  283. }
  284. private string GetCodeColumn(string column)
  285. {
  286. var comps = column.Split('.').ToList();
  287. comps.RemoveAt(comps.Count - 1);
  288. var prefix = string.Format("{0}.", string.Join(".", comps));
  289. var cols = EditorGrid.Columns.Where(x => !x.ColumnName.Equals(column) && x.ColumnName.StartsWith(prefix));
  290. foreach (var col in cols)
  291. {
  292. var editor = EditorGrid.OnGetEditor?.Invoke(col);
  293. if (editor is CodeEditor || editor is UniqueCodeEditor)
  294. return col.ColumnName.Split('.').Last();
  295. }
  296. return "";
  297. }
  298. private void ConfigureExpressionEditor(ExpressionEditorControl control)
  299. {
  300. control.GetItems += () => EditorGrid.GetItems?.Invoke() ?? Array.Empty<object?>();
  301. }
  302. private void ConfigurePopupEditor(PopupEditorControl popup, string column, PopupEditor editor)
  303. {
  304. popup.ColumnName = column;
  305. LoadLookupColumns(column, popup.OtherColumns);
  306. if (popup.EditorDefinition is DataLookupEditor dataLookup)
  307. LoadLookupColumns(column, dataLookup.OtherColumns);
  308. popup.OnDefineFilter += (sender, type) => { return EditorGrid.OnDefineFilter?.Invoke(sender, type); };
  309. popup.OnUpdateOtherEditor += Lookup_OnUpdateOtherEditor;
  310. }
  311. private void ConfigureCodePopupEditor(CodePopupEditorControl popup, string column, CodePopupEditor editor)
  312. {
  313. popup.ColumnName = column;
  314. LoadLookupColumns(column, popup.OtherColumns);
  315. if (popup.EditorDefinition is DataLookupEditor dataLookup)
  316. LoadLookupColumns(column, dataLookup.OtherColumns);
  317. popup.CodeColumn = !string.IsNullOrEmpty(editor.CodeColumn) ? editor.CodeColumn : GetCodeColumn(column);
  318. popup.OnDefineFilter += (sender, type) => { return EditorGrid.OnDefineFilter?.Invoke(sender, type); };
  319. popup.OnUpdateOtherEditor += Lookup_OnUpdateOtherEditor;
  320. }
  321. private void ConfigureLookupEditor(LookupEditorControl lookup, string column, LookupEditor editor)
  322. {
  323. if (editor.LookupWidth != int.MaxValue)
  324. lookup.Width = editor.LookupWidth;
  325. lookup.ColumnName = column;
  326. LoadLookupColumns(column, lookup.OtherColumns);
  327. if (lookup.EditorDefinition is DataLookupEditor dataLookup)
  328. LoadLookupColumns(column, dataLookup.OtherColumns);
  329. lookup.OnUpdateOtherEditor += Lookup_OnUpdateOtherEditor;
  330. lookup.OnDefineLookups += sender => { EditorGrid.OnDefineLookups?.Invoke(sender); };
  331. lookup.OnLookupsDefined += sender => { EditorGrid.OnLookupsDefined?.Invoke(sender); };
  332. }
  333. private void ConfigureEnumEditor(LookupEditorControl lookup, string column, EnumLookupEditor editor)
  334. {
  335. if (editor.LookupWidth != int.MaxValue)
  336. lookup.Width = editor.LookupWidth;
  337. lookup.ColumnName = column;
  338. lookup.OnDefineLookups += sender => { EditorGrid.OnDefineLookups?.Invoke(sender); };
  339. lookup.OnLookupsDefined += sender =>
  340. {
  341. //OnLookupsDefined?.Invoke(sender);
  342. };
  343. }
  344. private void ConfigureComboEditor(LookupEditorControl lookup, string column, ComboLookupEditor editor)
  345. {
  346. if (editor.LookupWidth != int.MaxValue)
  347. lookup.Width = editor.LookupWidth;
  348. lookup.ColumnName = column;
  349. lookup.OnDefineLookups += sender => { EditorGrid.OnDefineLookups?.Invoke(sender); };
  350. lookup.OnLookupsDefined += sender => { EditorGrid.OnLookupsDefined?.Invoke(sender); };
  351. }
  352. private void ConfigureMultiLookupEditor(MultiLookupEditorControl lookup, string column, ComboMultiLookupEditor editor)
  353. {
  354. if (editor.LookupWidth != int.MaxValue)
  355. lookup.Width = editor.LookupWidth;
  356. lookup.ColumnName = column;
  357. lookup.OnDefineLookups += sender => { EditorGrid.OnDefineLookups?.Invoke(sender); };
  358. lookup.OnLookupsDefined += sender => { EditorGrid.OnLookupsDefined?.Invoke(sender); };
  359. }
  360. private void ConfigureCheckListEditor(CheckListBoxEditorControl checks, string column, CheckListEditor editor)
  361. {
  362. checks.Width = editor.LookupWidth;
  363. checks.ColumnName = column;
  364. checks.OnDefineLookups += sender => { EditorGrid.OnDefineLookups?.Invoke(sender); };
  365. checks.OnLookupsDefined += sender => { EditorGrid.OnLookupsDefined?.Invoke(sender); };
  366. }
  367. private void ConfigureDocumentEditor(DocumentEditorControl document, string column, BaseDocumentEditor editor)
  368. {
  369. document.ColumnName = column;
  370. LoadLookupColumns(column, document.OtherColumns);
  371. if (document.EditorDefinition is DataLookupEditor dataLookup)
  372. LoadLookupColumns(column, dataLookup.OtherColumns);
  373. document.OnGetDocument += id => { return EditorGrid.OnGetDocument?.Invoke(id); };
  374. document.OnSaveDocument += doc => { EditorGrid.OnSaveDocument?.Invoke(doc); };
  375. document.OnFindDocument += file => { return EditorGrid.OnFindDocument?.Invoke(file); };
  376. document.OnUpdateOtherEditor += Lookup_OnUpdateOtherEditor;
  377. document.Filter = editor.FileMask;
  378. }
  379. private void Lookup_OnUpdateOtherEditor(string columnname, object value)
  380. {
  381. var editor = Editors.FirstOrDefault(x => x.ColumnName.Equals(columnname));
  382. if (editor != null)
  383. CoreUtils.SetPropertyValue(editor, "Value", value);
  384. }
  385. private void ConfigurePasswordEditor(PasswordEditorControl passwordEditorControl, PasswordEditor passwordEditor)
  386. {
  387. passwordEditorControl.ViewButtonVisible = passwordEditor.ViewButtonVisible;
  388. }
  389. private void ConfigureEditors()
  390. {
  391. foreach (var Editor in Editors)
  392. {
  393. var editor = Editor.EditorDefinition;
  394. var column = Editor.ColumnName;
  395. if (Editor is LookupEditorControl lookupControl)
  396. {
  397. if (editor is LookupEditor lookupEditor)
  398. ConfigureLookupEditor(lookupControl, column, lookupEditor);
  399. else if (editor is EnumLookupEditor enumEditor)
  400. ConfigureEnumEditor(lookupControl, column, enumEditor);
  401. else if (editor is ComboLookupEditor comboEditor)
  402. ConfigureComboEditor(lookupControl, column, comboEditor);
  403. }
  404. else if (Editor is MultiLookupEditorControl multiLookupEditor && editor is ComboMultiLookupEditor comboMultiLookup)
  405. {
  406. ConfigureMultiLookupEditor(multiLookupEditor, column, comboMultiLookup);
  407. }
  408. else if (Editor is CheckListBoxEditorControl checkBoxControl && editor is CheckListEditor checkListEditor)
  409. {
  410. ConfigureCheckListEditor(checkBoxControl, column, checkListEditor);
  411. }
  412. else if (Editor is PopupEditorControl popupControl && editor is PopupEditor popupEditor)
  413. {
  414. ConfigurePopupEditor(popupControl, column, popupEditor);
  415. }
  416. else if (Editor is CodePopupEditorControl codePopupControl && editor is CodePopupEditor codePopupEditor)
  417. {
  418. ConfigureCodePopupEditor(codePopupControl, column, codePopupEditor);
  419. }
  420. else if (Editor is DocumentEditorControl documentEditorControl && editor is BaseDocumentEditor baseDocumentEditor)
  421. {
  422. ConfigureDocumentEditor(documentEditorControl, column, baseDocumentEditor);
  423. }
  424. else if (Editor is PasswordEditorControl passwordEditorControl && editor is PasswordEditor passwordEditor)
  425. {
  426. ConfigurePasswordEditor(passwordEditorControl, passwordEditor);
  427. }
  428. else if (Editor is ExpressionEditorControl expressionEditorControl && editor is ExpressionEditor expressionEditor)
  429. {
  430. ConfigureExpressionEditor(expressionEditorControl);
  431. }
  432. Editor.Configure();
  433. if (!Editors.Any(x => x.ColumnName.Equals(Editor.ColumnName)))
  434. Editors.Add(Editor);
  435. Editor.Loaded = true;
  436. }
  437. }
  438. #endregion
  439. private void EditorValueChanged(IDynamicEditorControl sender, Dictionary<string, object> values)
  440. {
  441. //Logger.Send(LogType.Information, "", string.Format("DynamicEditorGrid.EditorValueChanged({0})", values.Keys.Count));
  442. var changededitors = new Dictionary<string, object?>();
  443. void ExtractChanged(Dictionary<String, object?>? columns)
  444. {
  445. if (columns != null)
  446. foreach (var (change, value) in columns)
  447. if (Editors.Any(x => x.ColumnName.Equals(change)) && !changededitors.ContainsKey(change) && !change.Equals(sender.ColumnName))
  448. changededitors[change] = value;
  449. }
  450. foreach (var key in values.Keys)
  451. {
  452. var changedcolumns = EditorGrid.OnEditorValueChanged?.Invoke(EditorGrid, key, values[key]);
  453. ExtractChanged(changedcolumns);
  454. }
  455. var afterchanged = EditorGrid.OnAfterEditorValueChanged?.Invoke(EditorGrid, sender.ColumnName);
  456. ExtractChanged(afterchanged);
  457. if (changededitors.Any())
  458. LoadEditorValues(changededitors);
  459. EditorGrid.ReconfigureEditors();
  460. }
  461. private void LoadEditorValues(Dictionary<string, object?>? changededitors = null)
  462. {
  463. var columnnames = changededitors != null ? changededitors.Keys.ToArray() : Editors.Select(x => x.ColumnName).ToArray();
  464. foreach (var columnname in columnnames)
  465. {
  466. var editor = Editors.FirstOrDefault(x => x.ColumnName.Equals(columnname));
  467. if (editor == null)
  468. continue;
  469. var bLoaded = editor.Loaded;
  470. editor.Loaded = false;
  471. if (changededitors != null && changededitors.ContainsKey(columnname))
  472. {
  473. CoreUtils.SetPropertyValue(editor, "Value", changededitors[columnname]);
  474. }
  475. else
  476. {
  477. var curvalue = EditorGrid.GetPropertyValue(columnname);
  478. try
  479. {
  480. CoreUtils.SetPropertyValue(editor, "Value", curvalue);
  481. }
  482. catch (Exception e)
  483. {
  484. MessageBox.Show($"Unable to set editor value for {columnname} -> {curvalue}: {CoreUtils.FormatException(e)}");
  485. }
  486. CoreUtils.SetPropertyValue(editor, "Changed", false);
  487. }
  488. editor.Loaded = bLoaded;
  489. editor.OnEditorValueChanged += EditorValueChanged;
  490. }
  491. }
  492. public void Load(object item, Func<Type, CoreTable>? PageDataHandler)
  493. {
  494. ConfigureEditors();
  495. LoadEditorValues();
  496. foreach (var editor in Editors)
  497. {
  498. var editorValue = editor.GetValue();
  499. var entityValue = EditorGrid.GetPropertyValue(editor.ColumnName);
  500. if (!Equals(editorValue, entityValue))
  501. {
  502. editor.Loaded = false;
  503. editor.SetValue(entityValue);
  504. editor.Loaded = true;
  505. }
  506. }
  507. Editors.FirstOrDefault()?.SetFocus();
  508. Ready = true;
  509. }
  510. public Size MinimumSize() => new Size(800, GeneralHeight);
  511. public int Order() => PageOrder;
  512. }
  513. #endregion
  514. #region Loading + Editing Layout
  515. private decimal GetSequence(DynamicGridColumn column)
  516. {
  517. if (OnGetSequence != null)
  518. return OnGetSequence.Invoke(column);
  519. return 999;
  520. }
  521. private DynamicEditPage GetEditPage(string name)
  522. {
  523. var page = Pages.Where(x => x is DynamicEditPage page && page.Header == name).FirstOrDefault() as DynamicEditPage;
  524. if(page is null)
  525. {
  526. page = new DynamicEditPage(name);
  527. if (name == "General")
  528. {
  529. page.PageOrder = -1;
  530. }
  531. else
  532. {
  533. page.PageOrder = 0;
  534. }
  535. Pages.Add(page);
  536. }
  537. return page;
  538. }
  539. public void SetLayoutType<T>() where T : DynamicEditorGridLayout
  540. {
  541. LayoutType = typeof(T);
  542. }
  543. private void InitialiseLayout()
  544. {
  545. Layout = (Activator.CreateInstance(LayoutType ?? typeof(DefaultDynamicEditorGridLayout)) as DynamicEditorGridLayout)!;
  546. Layout.OnSelectPage += Layout_SelectPage;
  547. Content = Layout;
  548. }
  549. private void CreateLayout()
  550. {
  551. if(Layout is null)
  552. {
  553. InitialiseLayout();
  554. }
  555. foreach (var column in _columns.OrderBy(x => GetSequence(x)))
  556. {
  557. var iProp = DatabaseSchema.Property(UnderlyingType, column.ColumnName);
  558. var editor = OnGetEditor?.Invoke(column);
  559. if (editor != null && iProp?.ShouldShowEditor() != true)
  560. {
  561. editor.Visible = Visible.Hidden;
  562. editor.Editable = Editable.Hidden;
  563. }
  564. if(editor is not null)
  565. {
  566. OnGridCustomiseEditor?.Invoke(this, column, editor);
  567. }
  568. if (editor != null && editor.Editable != Editable.Hidden)
  569. {
  570. var page = string.IsNullOrWhiteSpace(editor.Page) ? iProp is StandardProperty ? "General" : "Custom Fields" : editor.Page;
  571. var editPage = GetEditPage(page);
  572. editPage.AddEditor(column.ColumnName, editor);
  573. }
  574. }
  575. OnEditorCreated?.Invoke(this, 0, 800);
  576. }
  577. #endregion
  578. #region Pages
  579. private void Layout_SelectPage(IDynamicEditorPage page)
  580. {
  581. if (!page.Ready)
  582. using (new WaitCursor())
  583. {
  584. OnLoadPage?.Invoke(page);
  585. }
  586. OnSelectPage?.Invoke(this, null);
  587. }
  588. public void UnloadPages(bool saved)
  589. {
  590. if(Pages is not null)
  591. foreach (var page in Pages)
  592. if (page.Ready)
  593. OnUnloadPage?.Invoke(page, saved);
  594. }
  595. private void LoadPages()
  596. {
  597. if (Pages != null && Layout is not null)
  598. using (new WaitCursor())
  599. {
  600. foreach (var page in Pages)
  601. {
  602. page.Ready = false;
  603. page.EditorGrid = this;
  604. }
  605. Layout.LoadPages(Pages);
  606. if (PreloadPages)
  607. {
  608. foreach(var page in Pages)
  609. {
  610. OnLoadPage?.Invoke(page);
  611. }
  612. }
  613. }
  614. }
  615. public void Load(DynamicEditorPages pages)
  616. {
  617. Pages = pages;
  618. _columns = OnCustomiseColumns?.Invoke(this, null) ?? new DynamicGridColumns();
  619. CreateLayout();
  620. Reload();
  621. }
  622. #endregion
  623. }
  624. }