DynamicEditorGrid.xaml.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Markup;
  13. using System.Windows.Media;
  14. using System.Windows.Threading;
  15. using System.Xml;
  16. using InABox.Clients;
  17. using InABox.Configuration;
  18. using InABox.Core;
  19. using InABox.WPF;
  20. using NPOI.SS.Formula.Functions;
  21. namespace InABox.DynamicGrid
  22. {
  23. /// <summary>
  24. /// Interaction logic for DynamicEditorGrid.xaml
  25. /// </summary>
  26. public delegate void OnUpdateOtherEditorHandler(string columnname, object value);
  27. public delegate Dictionary<string, object?> EditorValueChangedHandler(object sender, string name, object value);
  28. //public delegate Dictionary<object, object> EditorGetLookupsHandler(object sender, String column);
  29. public partial class DynamicEditorGrid : UserControl
  30. {
  31. public delegate void EditorCreatedHandler(object sender, double height, double width);
  32. public delegate Document? FindDocumentEvent(string FileName);
  33. public delegate Document? GetDocumentEvent(Guid id);
  34. public delegate object? GetPropertyValueHandler(object sender, string name);
  35. public delegate void SaveDocumentEvent(Document document);
  36. public delegate void SetPropertyValueHandler(object sender, string name, object value);
  37. public delegate object?[] GetItemsEvent();
  38. // Column Definitions as defined by calling model
  39. private DynamicGridColumns _columns;
  40. private string _layoutname = "";
  41. private bool _loaded;
  42. private bool bChanging;
  43. // List of Changed Editor Values (used when redesigning screen)
  44. private readonly Dictionary<string, object> changes = new();
  45. private Grid CustomGrid;
  46. private DynamicTabControl Details;
  47. private Grid EditorGrid;
  48. // Active Editor List
  49. private readonly List<IDynamicEditorControl> editors = new();
  50. private readonly Dictionary<IDynamicEditorPage, DynamicTabItem> pagemap = new();
  51. //Dictionary<IDynamicEditorPage, bool> loadedpages = new Dictionary<IDynamicEditorPage, bool>();
  52. public DynamicEditorGrid()
  53. {
  54. InitializeComponent();
  55. Loaded += DynamicEditorGrid_Loaded;
  56. }
  57. //private Dictionary<String, Tuple<FrameworkElement, Object, DynamicGridColumn>> editors = new Dictionary<string, Tuple<FrameworkElement, Object, DynamicGridColumn>>();
  58. public DynamicEditorPages? Pages { get; private set; }
  59. public bool PreloadPages { get; set; }
  60. public Type UnderlyingType { get; set; }
  61. public OnLoadPage? OnLoadPage { get; set; }
  62. public OnSelectPage? OnSelectPage { get; set; }
  63. public OnUnloadPage? OnUnloadPage { get; set; }
  64. //private void UnloadEditorValues()
  65. //{
  66. // foreach (string columnname in editors.Keys)
  67. // {
  68. // bool changed = (bool)CoreUtils.GetPropertyValue(editors[columnname], "Changed");
  69. // if (changed)
  70. // {
  71. // changes[columnname] = CoreUtils.GetPropertyValue(editors[columnname], "Value");
  72. // Dictionary<string, object> othervalues = CoreUtils.GetPropertyValue(editors[columnname], "OtherValues") as Dictionary<string, object>;
  73. // foreach (var field in othervalues.Keys)
  74. // {
  75. // List<String> comps = columnname.Split('.').ToList();
  76. // comps[comps.Count - 1] = field;
  77. // String actualname = String.Join(".", comps);
  78. // changes[actualname] = othervalues[field];
  79. // }
  80. // }
  81. // }
  82. //}
  83. public bool IsCustomLayout { get; private set; }
  84. public bool TryFindEditor(string columnname, [NotNullWhen(true)] out IDynamicEditorControl? editor)
  85. {
  86. editor = editors.FirstOrDefault(x => x.ColumnName.Equals(columnname));
  87. return editor is not null;
  88. }
  89. public IDynamicEditorControl? FindEditor(string columnname)
  90. {
  91. TryFindEditor(columnname, out var editor);
  92. return editor;
  93. }
  94. public object? GetPropertyValue(string columnname)
  95. {
  96. return OnGetPropertyValue?.Invoke(this, columnname);
  97. }
  98. public event EditorCreatedHandler? OnEditorCreated;
  99. public event OnCustomiseColumns? OnCustomiseColumns;
  100. public event OnGetEditor? OnGetEditor;
  101. public event OnGridCustomiseEditor? OnGridCustomiseEditor;
  102. public event OnGetEditorSequence? OnGetSequence;
  103. public event GetPropertyValueHandler? OnGetPropertyValue;
  104. public event SetPropertyValueHandler? OnSetPropertyValue;
  105. public event EditorValueChangedHandler? OnEditorValueChanged;
  106. public event OnAfterEditorValueChanged? OnAfterEditorValueChanged;
  107. public event OnReconfigureEditors? OnReconfigureEditors;
  108. public event OnDefineFilter? OnDefineFilter;
  109. public event OnDefineLookup? OnDefineLookups;
  110. public event OnLookupsDefined? OnLookupsDefined;
  111. public event GetDocumentEvent? OnGetDocument;
  112. public event FindDocumentEvent? OnFindDocument;
  113. public event SaveDocumentEvent? OnSaveDocument;
  114. public event GetItemsEvent? GetItems;
  115. private void DynamicEditorGrid_Loaded(object sender, RoutedEventArgs e)
  116. {
  117. Reload();
  118. }
  119. public void Reload(bool refresh = false)
  120. {
  121. if (_loaded ^ refresh) return;
  122. ConfigureEditors();
  123. LoadEditorValues();
  124. ClearPages();
  125. AddPages();
  126. ReconfigureEditors();
  127. //MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
  128. //MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
  129. //if (Keyboard.PrimaryDevice != null)
  130. //{
  131. // if (Keyboard.PrimaryDevice.ActiveSource != null)
  132. // {
  133. // var e1 = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.Tab) { RoutedEvent = Keyboard.KeyDownEvent };
  134. // InputManager.Current.ProcessInput(e1);
  135. // }
  136. //}
  137. editors.FirstOrDefault()?.SetFocus();
  138. _loaded = true;
  139. }
  140. #region Configure Editors
  141. private void LoadLookupColumns(DynamicGridColumn column, Dictionary<string, string> othercolumns)
  142. {
  143. othercolumns.Clear();
  144. var comps = column.ColumnName.Split('.').ToList();
  145. comps.RemoveAt(comps.Count - 1);
  146. var prefix = string.Format("{0}.", string.Join(".", comps));
  147. var cols = _columns.Where(x => !x.ColumnName.Equals(column.ColumnName) && x.ColumnName.StartsWith(prefix));
  148. foreach (var col in cols)
  149. othercolumns[col.ColumnName.Replace(prefix, "")] = col.ColumnName;
  150. }
  151. private string GetCodeColumn(DynamicGridColumn column)
  152. {
  153. var comps = column.ColumnName.Split('.').ToList();
  154. comps.RemoveAt(comps.Count - 1);
  155. var prefix = string.Format("{0}.", string.Join(".", comps));
  156. var cols = _columns.Where(x => !x.ColumnName.Equals(column.ColumnName) && x.ColumnName.StartsWith(prefix));
  157. foreach (var col in cols)
  158. {
  159. var editor = OnGetEditor?.Invoke(col);
  160. if (editor is CodeEditor || editor is UniqueCodeEditor)
  161. return col.ColumnName.Split('.').Last();
  162. }
  163. return "";
  164. }
  165. private void ConfigureExpressionEditor(ExpressionEditorControl control)
  166. {
  167. control.GetItems += () => GetItems?.Invoke() ?? Array.Empty<object?>();
  168. }
  169. private void ConfigurePopupEditor(PopupEditorControl popup, DynamicGridColumn column, PopupEditor editor)
  170. {
  171. popup.ColumnName = column.ColumnName;
  172. LoadLookupColumns(column, popup.OtherColumns);
  173. if (popup.EditorDefinition is DataLookupEditor dataLookup)
  174. LoadLookupColumns(column, dataLookup.OtherColumns);
  175. popup.OnDefineFilter += (sender, type) => { return OnDefineFilter?.Invoke(sender, type); };
  176. popup.OnUpdateOtherEditor += Lookup_OnUpdateOtherEditor;
  177. }
  178. private void ConfigureCodePopupEditor(CodePopupEditorControl popup, DynamicGridColumn column, CodePopupEditor editor)
  179. {
  180. popup.ColumnName = column.ColumnName;
  181. LoadLookupColumns(column, popup.OtherColumns);
  182. if (popup.EditorDefinition is DataLookupEditor dataLookup)
  183. LoadLookupColumns(column, dataLookup.OtherColumns);
  184. popup.CodeColumn = !string.IsNullOrEmpty(editor.CodeColumn) ? editor.CodeColumn : GetCodeColumn(column);
  185. popup.OnDefineFilter += (sender, type) => { return OnDefineFilter?.Invoke(sender, type); };
  186. popup.OnUpdateOtherEditor += Lookup_OnUpdateOtherEditor;
  187. }
  188. private void ConfigureLookupEditor(LookupEditorControl lookup, DynamicGridColumn column, LookupEditor editor)
  189. {
  190. if (editor.LookupWidth != int.MaxValue)
  191. lookup.Width = editor.LookupWidth;
  192. lookup.ColumnName = column.ColumnName;
  193. LoadLookupColumns(column, lookup.OtherColumns);
  194. if (lookup.EditorDefinition is DataLookupEditor dataLookup)
  195. LoadLookupColumns(column, dataLookup.OtherColumns);
  196. lookup.OnUpdateOtherEditor += Lookup_OnUpdateOtherEditor;
  197. lookup.OnDefineLookups += sender => { OnDefineLookups?.Invoke(sender); };
  198. lookup.OnLookupsDefined += sender => { OnLookupsDefined?.Invoke(sender); };
  199. }
  200. private void ConfigureEnumEditor(LookupEditorControl lookup, DynamicGridColumn column, EnumLookupEditor editor)
  201. {
  202. if (editor.LookupWidth != int.MaxValue)
  203. lookup.Width = editor.LookupWidth;
  204. lookup.ColumnName = column.ColumnName;
  205. lookup.OnDefineLookups += sender => { OnDefineLookups?.Invoke(sender); };
  206. lookup.OnLookupsDefined += sender =>
  207. {
  208. //OnLookupsDefined?.Invoke(sender);
  209. };
  210. }
  211. private void ConfigureComboEditor(LookupEditorControl lookup, DynamicGridColumn column, ComboLookupEditor editor)
  212. {
  213. if (editor.LookupWidth != int.MaxValue)
  214. lookup.Width = editor.LookupWidth;
  215. lookup.ColumnName = column.ColumnName;
  216. lookup.OnDefineLookups += sender => { OnDefineLookups?.Invoke(sender); };
  217. lookup.OnLookupsDefined += sender => { OnLookupsDefined?.Invoke(sender); };
  218. }
  219. private void ConfigureMultiLookupEditor(MultiLookupEditorControl lookup, DynamicGridColumn column, ComboMultiLookupEditor editor)
  220. {
  221. if (editor.LookupWidth != int.MaxValue)
  222. lookup.Width = editor.LookupWidth;
  223. lookup.ColumnName = column.ColumnName;
  224. lookup.OnDefineLookups += sender => { OnDefineLookups?.Invoke(sender); };
  225. lookup.OnLookupsDefined += sender => { OnLookupsDefined?.Invoke(sender); };
  226. }
  227. private void ConfigureCheckListEditor(CheckListBoxEditorControl checks, DynamicGridColumn column, CheckListEditor editor)
  228. {
  229. checks.Width = editor.LookupWidth;
  230. checks.ColumnName = column.ColumnName;
  231. checks.OnDefineLookups += sender => { OnDefineLookups?.Invoke(sender); };
  232. checks.OnLookupsDefined += sender => { OnLookupsDefined?.Invoke(sender); };
  233. }
  234. private void ConfigureDocumentEditor(DocumentEditorControl document, DynamicGridColumn column, BaseDocumentEditor editor)
  235. {
  236. document.ColumnName = column.ColumnName;
  237. LoadLookupColumns(column, document.OtherColumns);
  238. if (document.EditorDefinition is DataLookupEditor dataLookup)
  239. LoadLookupColumns(column, dataLookup.OtherColumns);
  240. document.OnGetDocument += id => { return OnGetDocument?.Invoke(id); };
  241. document.OnSaveDocument += doc => { OnSaveDocument?.Invoke(doc); };
  242. document.OnFindDocument += file => { return OnFindDocument?.Invoke(file); };
  243. document.OnUpdateOtherEditor += Lookup_OnUpdateOtherEditor;
  244. document.Filter = editor.FileMask;
  245. }
  246. private void Lookup_OnUpdateOtherEditor(string columnname, object value)
  247. {
  248. var editor = editors.FirstOrDefault(x => x.ColumnName.Equals(columnname));
  249. if (editor != null)
  250. CoreUtils.SetPropertyValue(editor, "Value", value);
  251. }
  252. private void ConfigurePasswordEditor(PasswordEditorControl passwordEditorControl, DynamicGridColumn column, PasswordEditor passwordEditor)
  253. {
  254. passwordEditorControl.ViewButtonVisible = passwordEditor.ViewButtonVisible;
  255. }
  256. //private IEnumerable<BaseDynamicEditorControl> FindEditors(FrameworkElement element, DynamicGridColumn column)
  257. //{
  258. // if (element == null)
  259. // return new List<BaseDynamicEditorControl>();
  260. // if ((element is Border) && ((element as Border).Child is ScrollViewer))
  261. // return FindEditors(((element as Border).Child as ScrollViewer), column);
  262. // return element.FindVisualChildren<BaseDynamicEditorControl>().Where(x => x.ColumnName != null && x.ColumnName.Equals(column.ColumnName));
  263. //}
  264. private IEnumerable<BaseDynamicEditorControl> FindEditors(DynamicGridColumn column)
  265. {
  266. var results = new List<BaseDynamicEditorControl>();
  267. foreach (DynamicTabItem tab in Details.Items)
  268. {
  269. var border = tab.Content as Border;
  270. if (border != null)
  271. {
  272. var scroll = border.Child as ScrollViewer;
  273. if (scroll != null)
  274. {
  275. var grid = scroll.Content as Grid;
  276. if (grid != null)
  277. results.AddRange(grid.Children.OfType<BaseDynamicEditorControl>()
  278. .Where(x => string.Equals(x.ColumnName, column.ColumnName)));
  279. }
  280. }
  281. }
  282. //results.AddRange(FindEditors(tab.Content as FrameworkElement, column));
  283. return results;
  284. }
  285. private void ConfigureEditors()
  286. {
  287. if (editors.Any() && _columns != null)
  288. foreach (var column in _columns)
  289. {
  290. var editorname = column.ColumnName.Replace(".", "_");
  291. var
  292. Editors = FindEditors(
  293. column); //Details.FindVisualChildren<BaseDynamicEditorControl>().Where(x => x.ColumnName != null && x.ColumnName.Equals(column.ColumnName));
  294. foreach (var Editor in Editors)
  295. {
  296. var editor = Editor.EditorDefinition;
  297. //List<Object> parameters = editor.Parameters != null ? editor.Parameters.ToList() : new List<object>();
  298. if (Editor is LookupEditorControl lookupControl)
  299. {
  300. if (editor is LookupEditor lookupEditor)
  301. ConfigureLookupEditor(lookupControl, column, lookupEditor);
  302. else if (editor is EnumLookupEditor enumEditor)
  303. ConfigureEnumEditor(lookupControl, column, enumEditor);
  304. else if (editor is ComboLookupEditor comboEditor)
  305. ConfigureComboEditor(lookupControl, column, comboEditor);
  306. }
  307. else if(Editor is MultiLookupEditorControl multiLookupEditor && editor is ComboMultiLookupEditor comboMultiLookup)
  308. {
  309. ConfigureMultiLookupEditor(multiLookupEditor, column, comboMultiLookup);
  310. }
  311. else if (Editor is CheckListBoxEditorControl checkBoxControl && editor is CheckListEditor checkListEditor)
  312. {
  313. ConfigureCheckListEditor(checkBoxControl, column, checkListEditor);
  314. }
  315. else if (Editor is PopupEditorControl popupControl && editor is PopupEditor popupEditor)
  316. {
  317. ConfigurePopupEditor(popupControl, column, popupEditor);
  318. }
  319. else if (Editor is CodePopupEditorControl codePopupControl && editor is CodePopupEditor codePopupEditor)
  320. {
  321. ConfigureCodePopupEditor(codePopupControl, column, codePopupEditor);
  322. }
  323. else if (Editor is DocumentEditorControl documentEditorControl && editor is BaseDocumentEditor baseDocumentEditor)
  324. {
  325. ConfigureDocumentEditor(documentEditorControl, column, baseDocumentEditor);
  326. }
  327. else if (Editor is PasswordEditorControl passwordEditorControl && editor is PasswordEditor passwordEditor)
  328. {
  329. ConfigurePasswordEditor(passwordEditorControl, column, passwordEditor);
  330. }
  331. else if(Editor is ExpressionEditorControl expressionEditorControl && editor is ExpressionEditor expressionEditor)
  332. {
  333. ConfigureExpressionEditor(expressionEditorControl);
  334. }
  335. Editor.Configure();
  336. if (!editors.Any(x => x.ColumnName.Equals(Editor.ColumnName)))
  337. editors.Add(Editor);
  338. Editor.Loaded = true;
  339. }
  340. }
  341. }
  342. public virtual void ReconfigureEditors()
  343. {
  344. OnReconfigureEditors?.Invoke(this);
  345. }
  346. #endregion
  347. #region Editor
  348. private void EditorValueChanged(IDynamicEditorControl sender, Dictionary<string, object> values)
  349. {
  350. //Logger.Send(LogType.Information, "", string.Format("DynamicEditorGrid.EditorValueChanged({0})", values.Keys.Count));
  351. var changededitors = new Dictionary<string, object?>();
  352. void ExtractChanged(Dictionary<String, object?>? columns)
  353. {
  354. if (columns != null)
  355. foreach (var (change, value) in columns)
  356. if (editors.Any(x => x.ColumnName.Equals(change)) && !changededitors.ContainsKey(change) && !change.Equals(sender.ColumnName))
  357. changededitors[change] = value;
  358. }
  359. foreach (var key in values.Keys)
  360. {
  361. var changedcolumns = OnEditorValueChanged?.Invoke(this, key, values[key]);
  362. ExtractChanged(changedcolumns);
  363. }
  364. var afterchanged = OnAfterEditorValueChanged?.Invoke(this, sender.ColumnName);
  365. ExtractChanged(afterchanged);
  366. if (changededitors.Any())
  367. LoadEditorValues(changededitors);
  368. ReconfigureEditors();
  369. }
  370. private void LoadEditorValues(Dictionary<string, object?>? changededitors = null)
  371. {
  372. var columnnames = changededitors != null ? changededitors.Keys.ToArray() : editors.Select(x => x.ColumnName).ToArray();
  373. foreach (var columnname in columnnames)
  374. {
  375. var editor = editors.FirstOrDefault(x => x.ColumnName.Equals(columnname));
  376. if (editor == null)
  377. continue;
  378. var bLoaded = editor.Loaded;
  379. editor.Loaded = false;
  380. if (changededitors != null && changededitors.ContainsKey(columnname))
  381. {
  382. CoreUtils.SetPropertyValue(editor, "Value", changededitors[columnname]);
  383. }
  384. else
  385. {
  386. var curvalue = OnGetPropertyValue?.Invoke(this, columnname);
  387. try
  388. {
  389. CoreUtils.SetPropertyValue(editor, "Value", curvalue);
  390. }
  391. catch (Exception e)
  392. {
  393. MessageBox.Show($"Unable to set editor value for {columnname} -> {curvalue}: {CoreUtils.FormatException(e)}");
  394. }
  395. CoreUtils.SetPropertyValue(editor, "Changed", false);
  396. }
  397. editor.Loaded = bLoaded;
  398. editor.OnEditorValueChanged += EditorValueChanged;
  399. }
  400. }
  401. #endregion
  402. #region Loading + Editing Layout
  403. private bool LoadLayout(string xaml)
  404. {
  405. if (!string.IsNullOrWhiteSpace(xaml))
  406. try
  407. {
  408. IsCustomLayout = true;
  409. Content = null;
  410. Details = (XamlReader.Parse(xaml) as DynamicTabControl) ?? throw new Exception("XAML is not a DynamicTabControl");
  411. Content = Details;
  412. Details.ApplyTemplate();
  413. var iHeight = Details.Height > 0 ? Details.Height : 600;
  414. var iWidth = Details.Width > 0 ? Details.Width : 800;
  415. Details.Height = double.NaN;
  416. Details.Width = double.NaN;
  417. OnEditorCreated?.Invoke(this, iHeight, iWidth);
  418. return true;
  419. }
  420. catch (Exception e)
  421. {
  422. MessageBox.Show(string.Format("Unable to Load XAML!\n\n{0}", e.Message));
  423. }
  424. return false;
  425. }
  426. private decimal GetSequence(DynamicGridColumn column)
  427. {
  428. if (OnGetSequence != null)
  429. return OnGetSequence.Invoke(column);
  430. return 999;
  431. }
  432. private void CreateLayout()
  433. {
  434. //Stopwatch sw = new Stopwatch();
  435. //sw.Start();
  436. IsCustomLayout = false;
  437. Content = null;
  438. Details = new DynamicTabControl();
  439. Details.VerticalAlignment = VerticalAlignment.Stretch;
  440. Details.HorizontalAlignment = HorizontalAlignment.Stretch;
  441. Details.Name = "Details";
  442. //Logger.Send(LogType.Information, "DEG.CreateLayout", String.Format("Created Tab Control: {0}", sw.ElapsedMilliseconds));
  443. //sw.Restart();
  444. var EditorGrids = new Dictionary<string, Grid>();
  445. CustomGrid = EnsureGrid(EditorGrids, "Custom Fields");
  446. EditorGrid = EnsureGrid(EditorGrids, "General");
  447. //Logger.Send(LogType.Information, "DEG.CreateLayout", String.Format("Ensured Grids: {0}", sw.ElapsedMilliseconds));
  448. //sw.Restart();
  449. Details.SelectionChanged += Details_SelectionChanged;
  450. Content = Details;
  451. editors.Clear();
  452. double fGeneralHeight = 30; // Allow for Tab Header
  453. //Logger.Send(LogType.Information, "DEG.CreateLayout", String.Format("Sorted Columns: {0}", sw.ElapsedMilliseconds));
  454. //sw.Restart();
  455. foreach (var column in _columns.OrderBy(x => GetSequence(x)))
  456. {
  457. var iProp = DatabaseSchema.Property(UnderlyingType, column.ColumnName);
  458. var editor = OnGetEditor?.Invoke(column);
  459. if (editor != null && iProp?.ShouldShowEditor() != true)
  460. {
  461. editor.Visible = Visible.Hidden;
  462. editor.Editable = Editable.Hidden;
  463. }
  464. if(editor is not null)
  465. {
  466. OnGridCustomiseEditor?.Invoke(this, column, editor);
  467. }
  468. if (editor != null && editor.Editable != Editable.Hidden)
  469. {
  470. var page = string.IsNullOrWhiteSpace(editor.Page) ? iProp is StandardProperty ? "General" : "Custom Fields" : editor.Page;
  471. var grid = EnsureGrid(EditorGrids, page);
  472. //List<Object> parameters = editor.Parameters != null ? editor.Parameters.ToList() : new List<object>();
  473. //bool bParams = true;
  474. BaseDynamicEditorControl? element = null;
  475. element = editor switch
  476. {
  477. TextBoxEditor => new TextBoxEditorControl(),
  478. Core.RichTextEditor => new RichTextEditorControl(),
  479. URLEditor => new URLEditorControl(),
  480. CodeEditor or UniqueCodeEditor => new CodeEditorControl(),
  481. CheckBoxEditor => new CheckBoxEditorControl(),
  482. DateTimeEditor => new DateTimeEditorControl(),
  483. DateEditor dateEditor => new DateEditorControl { TodayVisible = dateEditor.TodayVisible },
  484. TimeOfDayEditor => new TimeOfDayEditorControl { NowButtonVisible = false },
  485. DurationEditor => new DurationEditorControl(),
  486. NotesEditor => new NotesEditorControl(),
  487. PINEditor => new PINEditorControl(),
  488. CheckListEditor => new CheckListBoxEditorControl(),
  489. MemoEditor => new MemoEditorControl(),
  490. JsonEditor => new JsonEditorControl(),
  491. LookupEditor => ClientFactory.IsSupported(((LookupEditor)editor).Type) ? new LookupEditorControl() : null,
  492. PopupEditor => ClientFactory.IsSupported(((PopupEditor)editor).Type) ? new PopupEditorControl() : null,
  493. CodePopupEditor => ClientFactory.IsSupported(((CodePopupEditor)editor).Type) ? new CodePopupEditorControl() : null,
  494. EnumLookupEditor or ComboLookupEditor => new LookupEditorControl(),
  495. ComboMultiLookupEditor => new MultiLookupEditorControl(),
  496. EmbeddedImageEditor imageEditor => new EmbeddedImageEditorControl
  497. {
  498. MaximumHeight = imageEditor.MaximumHeight,
  499. MaximumWidth = imageEditor.MaximumWidth,
  500. MaximumFileSize = imageEditor.MaximumFileSize
  501. },
  502. FileNameEditor fileNameEditor => new FileNameEditorControl
  503. {
  504. Filter = fileNameEditor.FileMask,
  505. AllowView = fileNameEditor.AllowView,
  506. RequireExisting = fileNameEditor.RequireExisting
  507. },
  508. FolderEditor folderEditor => new FolderEditorControl
  509. {
  510. InitialFolder = folderEditor.InitialFolder
  511. },
  512. MiscellaneousDocumentEditor => new DocumentEditorControl(),
  513. ImageDocumentEditor => new DocumentEditorControl(),
  514. VectorDocumentEditor => new DocumentEditorControl(),
  515. PDFDocumentEditor => new DocumentEditorControl(),
  516. PasswordEditor => new PasswordEditorControl(),
  517. CurrencyEditor => new CurrencyEditorControl(),
  518. DoubleEditor => new DoubleEditorControl(),
  519. IntegerEditor => new IntegerEditorControl(),
  520. Core.ScriptEditor scriptEditor => new ScriptEditorControl
  521. {
  522. SyntaxLanguage = scriptEditor.SyntaxLanguage
  523. },
  524. TimestampEditor => new TimestampEditorControl(),
  525. ColorEditor => new ColorEditorControl(),
  526. FilterEditor filter => new FilterEditorControl { FilterType = filter.Type! },
  527. ExpressionEditor expression => new ExpressionEditorControl(expression),
  528. _ => null,
  529. };
  530. if (element != null)
  531. {
  532. element.EditorDefinition = editor; //22
  533. element.IsEnabled = editor.Editable == Editable.Enabled;
  534. if (!string.IsNullOrWhiteSpace(editor.ToolTip))
  535. {
  536. element.ToolTip = new ToolTip() { Content = editor.ToolTip };
  537. }
  538. var label = new Label();
  539. label.Content = CoreUtils.Neatify(editor.Caption); // 2
  540. label.Margin = new Thickness(0F, 0F, 0F, 0F);
  541. label.HorizontalAlignment = HorizontalAlignment.Stretch;
  542. label.VerticalAlignment = VerticalAlignment.Stretch;
  543. label.HorizontalContentAlignment = HorizontalAlignment.Left;
  544. label.VerticalContentAlignment = VerticalAlignment.Center;
  545. label.SetValue(Grid.RowProperty, grid.RowDefinitions.Count);
  546. label.SetValue(Grid.ColumnProperty, 0);
  547. label.Visibility = string.IsNullOrWhiteSpace(editor.Caption) ? Visibility.Collapsed : Visibility.Visible;
  548. grid.Children.Add(label);
  549. element.ColumnName = column.ColumnName;
  550. element.Color = editor is UniqueCodeEditor ? Color.FromArgb(0xFF, 0xF6, 0xC9, 0xE8) : Colors.LightYellow;
  551. editors.Add(element);
  552. element.Margin = new Thickness(5F, 2.5F, 5F, 2.5F);
  553. double iHeight = element.DesiredHeight();
  554. if (iHeight == int.MaxValue)
  555. {
  556. grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
  557. fGeneralHeight += grid == EditorGrid ? element.MinHeight + 5.0F : 0.0F;
  558. }
  559. else
  560. {
  561. grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(iHeight + 5.0F) });
  562. fGeneralHeight += grid == EditorGrid ? iHeight + 5.0F : 0.0F;
  563. }
  564. double iWidth = element.DesiredWidth();
  565. if (iWidth == int.MaxValue)
  566. {
  567. element.HorizontalAlignment = HorizontalAlignment.Stretch;
  568. }
  569. else
  570. {
  571. element.HorizontalAlignment = HorizontalAlignment.Left;
  572. element.Width = iWidth;
  573. }
  574. element.SetValue(Grid.RowProperty, grid.RowDefinitions.Count - 1);
  575. element.SetValue(Grid.ColumnProperty, 1);
  576. grid.Children.Add(element);
  577. }
  578. }
  579. }
  580. //Logger.Send(LogType.Information, "DEG.CreateLayout", String.Format("Created Editors: {0}", sw.ElapsedMilliseconds));
  581. //sw.Restart();
  582. OnEditorCreated?.Invoke(this, fGeneralHeight, 800);
  583. if(Details.Items[^1] is DynamicTabItem custom)
  584. {
  585. custom.Visibility = CustomGrid.Children.Count > 0 ? Visibility.Visible : Visibility.Collapsed;
  586. }
  587. //Logger.Send(LogType.Information, "DEG.CreateLayout", String.Format("Finalised: {0}", sw.ElapsedMilliseconds));
  588. //sw.Stop();
  589. }
  590. private Grid EnsureGrid(Dictionary<string, Grid> grids, string caption)
  591. {
  592. if (grids.ContainsKey(caption))
  593. return grids[caption];
  594. // Create Editor, ScrollViewer and TabItem for Dynamic Editor
  595. var result = new Grid
  596. {
  597. HorizontalAlignment = HorizontalAlignment.Stretch,
  598. VerticalAlignment = VerticalAlignment.Stretch,
  599. //Background = new SolidColorBrush(Colors.Blue),
  600. Margin = new Thickness(0, 2.5, 0, 2.5)
  601. };
  602. result.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
  603. result.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  604. var scroll = new ScrollViewer
  605. {
  606. HorizontalAlignment = HorizontalAlignment.Stretch,
  607. VerticalAlignment = VerticalAlignment.Stretch,
  608. VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
  609. Padding = new Thickness(2)
  610. };
  611. scroll.Content = result;
  612. var border = new Border
  613. {
  614. BorderBrush = new SolidColorBrush(Colors.Gray),
  615. Background = new SolidColorBrush(Colors.White),
  616. BorderThickness = new Thickness(0.75)
  617. };
  618. border.Child = scroll;
  619. var tab = new DynamicTabItem();
  620. tab.Header = caption;
  621. tab.Content = border;
  622. if (Details.Items.Count == 0)
  623. Details.Items.Add(tab);
  624. else
  625. Details.Items.Insert(Details.Items.Count - 1, tab);
  626. grids[caption] = result;
  627. return result;
  628. }
  629. private string FormatXML(string xml)
  630. {
  631. var result = "";
  632. var mStream = new MemoryStream();
  633. var writer = new XmlTextWriter(mStream, Encoding.Unicode);
  634. var document = new XmlDocument();
  635. try
  636. {
  637. // Load the XmlDocument with the XML.
  638. document.LoadXml(xml);
  639. writer.Formatting = Formatting.Indented;
  640. // Write the XML into a formatting XmlTextWriter
  641. document.WriteContentTo(writer);
  642. writer.Flush();
  643. mStream.Flush();
  644. // Have to rewind the MemoryStream in order to read
  645. // its contents.
  646. mStream.Position = 0;
  647. // Read MemoryStream contents into a StreamReader.
  648. var sReader = new StreamReader(mStream);
  649. // Extract the text from the StreamReader.
  650. var formattedXml = sReader.ReadToEnd();
  651. result = formattedXml;
  652. }
  653. catch (XmlException)
  654. {
  655. // Handle the exception
  656. }
  657. mStream.Close();
  658. writer.Close();
  659. return result;
  660. }
  661. public void EditLayout()
  662. {
  663. ClearPages();
  664. //UnloadEditorValues();
  665. var xaml = new GlobalConfiguration<ScreenLayout>(_layoutname).Load().XAML;
  666. if (string.IsNullOrWhiteSpace(xaml))
  667. {
  668. Details.Height = Details.ActualHeight;
  669. Details.Width = Details.ActualWidth;
  670. //xaml = XamlWriter.Save(GetParentWindow(Details));
  671. xaml = XamlWriter.Save(Details);
  672. }
  673. xaml = FormatXML(xaml);
  674. var scripteditor = new ScriptEditor(xaml, SyntaxLanguage.XAML);
  675. if (scripteditor.ShowDialog() == true)
  676. {
  677. var layout = new ScreenLayout { XAML = scripteditor.Script };
  678. new GlobalConfiguration<ScreenLayout>(_layoutname).Save(layout);
  679. Content = null;
  680. if (!LoadLayout(layout.XAML))
  681. if (!LoadLayout(xaml))
  682. CreateLayout();
  683. Details.ApplyTemplate();
  684. Application.Current.Dispatcher.Invoke(() =>
  685. {
  686. ConfigureEditors();
  687. LoadEditorValues();
  688. AddPages();
  689. ReconfigureEditors();
  690. }, DispatcherPriority.Render);
  691. }
  692. }
  693. internal void ResetLayout()
  694. {
  695. new GlobalConfiguration<ScreenLayout>(_layoutname).Delete();
  696. ClearPages();
  697. //UnloadEditorValues();
  698. CreateLayout();
  699. LoadEditorValues();
  700. AddPages();
  701. ReconfigureEditors();
  702. }
  703. #endregion
  704. #region Pages
  705. //List<TabItem> configuredpages = new List<TabItem>();
  706. private void Details_SelectionChanged(object sender, SelectionChangedEventArgs e)
  707. {
  708. if (bChanging || Details?.SelectedItem == null || e.OriginalSource != Details)
  709. return;
  710. bChanging = true;
  711. try
  712. {
  713. var tab = Details.SelectedItem as DynamicTabItem;
  714. if(tab is not null)
  715. {
  716. var page = tab.Content as IDynamicEditorPage;
  717. if (page is not null)
  718. {
  719. if (!page.Ready)
  720. using (new WaitCursor())
  721. {
  722. OnLoadPage?.Invoke(page);
  723. }
  724. }
  725. else
  726. {
  727. if (!_loaded || e.RemovedItems.Count == 0 || e.AddedItems.Count == 0 || e.AddedItems?[0] == e.RemovedItems?[0])
  728. return;
  729. //if (!configuredpages.Contains(tab))
  730. //{
  731. // ConfigureEditors(eds);
  732. // configuredpages.Add(tab);
  733. //}
  734. var selectedGrid = ((tab.Content as Border)?.Child as ScrollViewer)?.Content;
  735. var eds = editors
  736. .Where(x => x is BaseDynamicEditorControl control &&
  737. control.Parent == selectedGrid)
  738. .Select(x => (BaseDynamicEditorControl)x);
  739. foreach (var ed in eds)
  740. {
  741. var editorvalue = ed.GetValue();
  742. var entityvalue = OnGetPropertyValue?.Invoke(this, ed.ColumnName);
  743. if (!Equals(editorvalue, entityvalue))
  744. {
  745. ed.Loaded = false;
  746. ed.SetValue(entityvalue);
  747. ed.Loaded = true;
  748. }
  749. }
  750. }
  751. OnSelectPage?.Invoke(tab, null);
  752. }
  753. }
  754. finally
  755. {
  756. bChanging = false;
  757. }
  758. }
  759. public void UnloadPages(bool saved)
  760. {
  761. if(Pages is not null)
  762. foreach (var page in Pages)
  763. if (page.Ready)
  764. OnUnloadPage?.Invoke(page, saved);
  765. }
  766. private void AddPages()
  767. {
  768. if (Pages != null)
  769. using (new WaitCursor())
  770. {
  771. foreach (var page in Pages.OrderBy(x => x.Order()).ThenBy(x => x.Caption()))
  772. {
  773. var tab = new DynamicTabItem();
  774. tab.Header = page.Caption();
  775. tab.Content = page;
  776. Details.Items.Insert(Details.Items.Count - 1, tab);
  777. pagemap[page] = tab;
  778. if (PreloadPages)
  779. OnLoadPage?.Invoke(page);
  780. page.EditorGrid = this;
  781. }
  782. }
  783. //if (Details.Items.Count <= 2)
  784. // ((TabItem)Details.Items[0]).Visibility = Visibility.Collapsed;
  785. }
  786. private void ClearPages()
  787. {
  788. foreach (var page in pagemap.Keys)
  789. {
  790. var tab = pagemap[page];
  791. tab.Content = null;
  792. Details.Items.Remove(tab);
  793. page.Ready = false;
  794. }
  795. pagemap.Clear();
  796. //if (_pages != null)
  797. //{
  798. // foreach (var page in _pages)
  799. // {
  800. // var tab = Details.Items.OfType<TabItem>().FirstOrDefault(x => x.Content.Equals(page));
  801. // if (tab != null)
  802. // {
  803. // tab.Content = null;
  804. // Details.Items.Remove(tab);
  805. // }
  806. // }
  807. //}
  808. }
  809. public void Load(string layoutname, DynamicEditorPages? pages = null)
  810. {
  811. _layoutname = layoutname;
  812. Pages = pages;
  813. //Stopwatch sw = new Stopwatch();
  814. //sw.Start();
  815. _columns = OnCustomiseColumns?.Invoke(this, null) ?? new DynamicGridColumns();
  816. //Logger.Send(LogType.Information, "DEG.Load", String.Format("Loaded Columns: {0}", sw.ElapsedMilliseconds));
  817. //sw.Restart();
  818. var layout = new GlobalConfiguration<ScreenLayout>(_layoutname).Load();
  819. //Logger.Send(LogType.Information, "DEG.Load", String.Format("Loaded Layout: {0}", sw.ElapsedMilliseconds));
  820. //sw.Restart();
  821. if (!LoadLayout(layout.XAML))
  822. CreateLayout();
  823. //Logger.Send(LogType.Information, "DEG.Load", String.Format("Created Layout: {0}", sw.ElapsedMilliseconds));
  824. //sw.Restart();
  825. }
  826. public bool Unload()
  827. {
  828. ClearPages();
  829. var bChanged = false;
  830. foreach (var columnname in changes.Keys)
  831. {
  832. OnSetPropertyValue?.Invoke(this, columnname, changes[columnname]);
  833. bChanged = true;
  834. }
  835. return bChanged;
  836. }
  837. #endregion
  838. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  839. {
  840. base.OnRenderSizeChanged(sizeInfo);
  841. foreach (var columnname in editors.Select(x => x.ColumnName).ToArray())
  842. {
  843. var editor = editors.FirstOrDefault(x => x.ColumnName.Equals(columnname));
  844. if(editor is not null)
  845. {
  846. editor.Loaded = true;
  847. }
  848. }
  849. }
  850. }
  851. }