DynamicEditorGrid.xaml.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. },
  168. EmbeddedListEditor listEditor => new EmbeddedListEditorControl()
  169. {
  170. DataType = listEditor.DataType,
  171. Label = listEditor.Label,
  172. DirectEdit = listEditor.DirectEdit
  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. else if (Editor is ButtonEditorControl buttonControl && editor is ButtonEditor buttonEditor)
  436. {
  437. ConfigureButtonControl(buttonControl, buttonEditor);
  438. }
  439. Editor.Configure();
  440. if (!Editors.Any(x => x.ColumnName.Equals(Editor.ColumnName)))
  441. Editors.Add(Editor);
  442. Editor.Loaded = true;
  443. }
  444. }
  445. private void ConfigureButtonControl(ButtonEditorControl buttonControl, ButtonEditor buttonEditor)
  446. {
  447. buttonControl.OnClick += buttonEditor.OnClick;
  448. }
  449. #endregion
  450. private void EditorValueChanged(IDynamicEditorControl sender, Dictionary<string, object> values)
  451. {
  452. //Logger.Send(LogType.Information, "", string.Format("DynamicEditorGrid.EditorValueChanged({0})", values.Keys.Count));
  453. var changededitors = new Dictionary<string, object?>();
  454. void ExtractChanged(Dictionary<String, object?>? columns)
  455. {
  456. if (columns != null)
  457. foreach (var (change, value) in columns)
  458. if (!changededitors.ContainsKey(change) && !change.Equals(sender.ColumnName))
  459. changededitors[change] = value;
  460. }
  461. foreach (var key in values.Keys)
  462. {
  463. var changedcolumns = EditorGrid.OnEditorValueChanged?.Invoke(EditorGrid, key, values[key]);
  464. ExtractChanged(changedcolumns);
  465. }
  466. var afterchanged = EditorGrid.OnAfterEditorValueChanged?.Invoke(EditorGrid, sender.ColumnName);
  467. ExtractChanged(afterchanged);
  468. if (changededitors.Any())
  469. LoadEditorValues(changededitors);
  470. EditorGrid.ReconfigureEditors();
  471. }
  472. private void LoadEditorValues(Dictionary<string, object?>? changededitors = null)
  473. {
  474. var columnnames = changededitors != null ? changededitors.Keys.ToArray() : Editors.Select(x => x.ColumnName).ToArray();
  475. foreach (var columnname in columnnames)
  476. {
  477. if (!TryFindEditor(columnname, out var editor))
  478. continue;
  479. var bLoaded = editor.Loaded;
  480. editor.Loaded = false;
  481. if (changededitors != null && changededitors.ContainsKey(columnname))
  482. {
  483. editor.SetValue(columnname, changededitors[columnname]);
  484. }
  485. else
  486. {
  487. var curvalue = EditorGrid.GetPropertyValue(columnname);
  488. try
  489. {
  490. editor.SetValue(columnname, curvalue);
  491. }
  492. catch (Exception e)
  493. {
  494. MessageBox.Show($"Unable to set editor value for {columnname} -> {curvalue}: {CoreUtils.FormatException(e)}");
  495. }
  496. editor.Changed = false;
  497. }
  498. editor.Loaded = bLoaded;
  499. editor.OnEditorValueChanged += EditorValueChanged;
  500. }
  501. }
  502. public void Load(object item, Func<Type, CoreTable>? PageDataHandler)
  503. {
  504. ConfigureEditors();
  505. LoadEditorValues();
  506. foreach (var editor in Editors)
  507. {
  508. foreach(var (column, editorValue) in editor.GetValues())
  509. {
  510. var entityValue = EditorGrid.GetPropertyValue(column);
  511. if (!Equals(editorValue, entityValue))
  512. {
  513. editor.Loaded = false;
  514. editor.SetValue(column, entityValue);
  515. editor.Loaded = true;
  516. }
  517. }
  518. }
  519. Editors.FirstOrDefault()?.SetFocus();
  520. Ready = true;
  521. }
  522. public Size MinimumSize() => new Size(800, GeneralHeight);
  523. public int Order() => PageOrder;
  524. }
  525. #endregion
  526. #region Loading + Editing Layout
  527. private decimal GetSequence(DynamicGridColumn column)
  528. {
  529. if (OnGetSequence != null)
  530. return OnGetSequence.Invoke(column);
  531. return 999;
  532. }
  533. private DynamicEditPage GetEditPage(string name)
  534. {
  535. var page = Pages.Where(x => x is DynamicEditPage page && page.Header == name).FirstOrDefault() as DynamicEditPage;
  536. if(page is null)
  537. {
  538. page = new DynamicEditPage(name);
  539. if (name == "General")
  540. {
  541. page.PageOrder = -1;
  542. }
  543. else
  544. {
  545. page.PageOrder = 0;
  546. }
  547. Pages.Add(page);
  548. }
  549. return page;
  550. }
  551. public void SetLayoutType<T>() where T : DynamicEditorGridLayout
  552. {
  553. LayoutType = typeof(T);
  554. }
  555. private void InitialiseLayout()
  556. {
  557. Layout = (Activator.CreateInstance(LayoutType ?? typeof(DefaultDynamicEditorGridLayout)) as DynamicEditorGridLayout)!;
  558. Layout.OnSelectPage += Layout_SelectPage;
  559. Content = Layout;
  560. }
  561. private void CreateLayout()
  562. {
  563. if(Layout is null)
  564. {
  565. InitialiseLayout();
  566. }
  567. foreach (var column in _columns.OrderBy(x => GetSequence(x)))
  568. {
  569. var iProp = DatabaseSchema.Property(UnderlyingType, column.ColumnName);
  570. var editor = OnGetEditor?.Invoke(column);
  571. if (editor != null && iProp?.ShouldShowEditor() != true)
  572. {
  573. editor.Visible = Visible.Hidden;
  574. editor.Editable = Editable.Hidden;
  575. }
  576. if(editor is not null)
  577. {
  578. OnGridCustomiseEditor?.Invoke(this, column, editor);
  579. }
  580. if (editor != null && editor.Editable != Editable.Hidden)
  581. {
  582. var page = string.IsNullOrWhiteSpace(editor.Page) ? iProp is StandardProperty ? "General" : "Custom Fields" : editor.Page;
  583. var editPage = GetEditPage(page);
  584. editPage.AddEditor(column.ColumnName, editor);
  585. }
  586. else if (iProp?.HasParentEditor() == true)
  587. {
  588. var parent = iProp.GetParentWithEditor();
  589. if(parent is not null)
  590. {
  591. var parentEditor = parent.Editor;
  592. if(parentEditor is not null)
  593. {
  594. OnGridCustomiseEditor?.Invoke(this, new DynamicGridColumn { ColumnName = parent.Name }, parentEditor);
  595. }
  596. if(parentEditor is not null && parentEditor.Editable != Editable.Hidden)
  597. {
  598. var page = string.IsNullOrWhiteSpace(parentEditor.Page)
  599. ? parent is StandardProperty
  600. ? "General"
  601. : "Custom Fields"
  602. : parentEditor.Page;
  603. var editPage = GetEditPage(page);
  604. if (!editPage.TryFindEditor(parent.Name, out var editorControl))
  605. {
  606. editPage.AddEditor(parent.Name, parentEditor);
  607. }
  608. }
  609. }
  610. }
  611. }
  612. OnEditorCreated?.Invoke(this, 0, 800);
  613. }
  614. #endregion
  615. #region Pages
  616. private void Layout_SelectPage(IDynamicEditorPage page)
  617. {
  618. if (!page.Ready)
  619. using (new WaitCursor())
  620. {
  621. OnLoadPage?.Invoke(page);
  622. }
  623. OnSelectPage?.Invoke(this, null);
  624. }
  625. public void UnloadPages(bool saved)
  626. {
  627. if(Pages is not null)
  628. foreach (var page in Pages)
  629. if (page.Ready)
  630. OnUnloadPage?.Invoke(page, saved);
  631. }
  632. private void LoadPages()
  633. {
  634. if (Pages != null && Layout is not null)
  635. using (new WaitCursor())
  636. {
  637. foreach (var page in Pages)
  638. {
  639. page.Ready = false;
  640. page.EditorGrid = this;
  641. }
  642. Layout.LoadPages(Pages);
  643. if (PreloadPages)
  644. {
  645. foreach(var page in Pages)
  646. {
  647. OnLoadPage?.Invoke(page);
  648. }
  649. }
  650. }
  651. }
  652. public void Load(DynamicEditorPages pages)
  653. {
  654. Pages = pages;
  655. _columns = new DynamicGridColumns();
  656. OnCustomiseColumns?.Invoke(this, _columns);
  657. CreateLayout();
  658. Reload();
  659. }
  660. #endregion
  661. }
  662. }