DynamicEditorGrid.xaml.cs 32 KB

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