DynamicEditorGrid.xaml.cs 33 KB

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