DynamicEditorGrid.xaml.cs 41 KB

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