DynamicEditorGrid.xaml.cs 32 KB

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