EmbeddedDynamicEditorForm.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. using InABox.Core;
  2. using InABox.Wpf;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Diagnostics.CodeAnalysis;
  7. using System.Linq;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. namespace InABox.DynamicGrid
  13. {
  14. /// <summary>
  15. /// Interaction logic for EmbeddedDynamicEditorForm.xaml
  16. /// </summary>
  17. public partial class EmbeddedDynamicEditorForm : UserControl, IDynamicEditorForm
  18. {
  19. public event OnBeforeLoad? OnBeforeLoad;
  20. public void BeforeLoad() => OnBeforeLoad?.Invoke(this);
  21. public event OnAfterLoad? OnAfterLoad;
  22. public void AfterLoad() => OnAfterLoad?.Invoke(this);
  23. public event IDynamicEditorForm.OnReloadEventHandler? OnReload;
  24. public event EventHandler OnChanged;
  25. private bool bChanged = false;
  26. public void DoChanged()
  27. {
  28. bChanged = true;
  29. //OKButton.IsEnabled = true;
  30. //CancelButton.IsEnabled = true;
  31. OnChanged?.Invoke(this, EventArgs.Empty);
  32. UpdateButtonEnabled();
  33. }
  34. public delegate void OKEvent();
  35. public delegate void CancelEvent();
  36. public DynamicEditorPages Pages { get; private set; } = [];
  37. private BaseObject[] _items;
  38. public BaseObject[] Items
  39. {
  40. get => _items;
  41. set
  42. {
  43. _items = value;
  44. DynamicEditorFormModel.Slug = Items?.FirstOrDefault()?.GetType().EntityName().Split('.').Last() ?? "";
  45. Editor.Load(Pages);
  46. bChanged = _items.Any(x => x is not Entity e || e.ID == Guid.Empty);
  47. UpdateButtonEnabled();
  48. foreach (var page in Pages)
  49. page.OnChanged += (sender, args) => DoChanged();
  50. }
  51. }
  52. public bool ReadOnly
  53. {
  54. get => Editor.ReadOnly;
  55. set
  56. {
  57. Editor.ReadOnly = value;
  58. UpdateButtonEnabled();
  59. }
  60. }
  61. private bool _disableIfUnchanged = false;
  62. public bool DisableOKIfUnchanged
  63. {
  64. get => _disableIfUnchanged;
  65. set
  66. {
  67. _disableIfUnchanged = value;
  68. UpdateButtonEnabled();
  69. }
  70. }
  71. private bool _hideButtons = false;
  72. public bool HideButtons
  73. {
  74. get => _hideButtons;
  75. set
  76. {
  77. _hideButtons = value;
  78. OKButton.Visibility = value ? Visibility.Collapsed : Visibility.Visible;
  79. CancelButton.Visibility = value ? Visibility.Collapsed : Visibility.Visible;
  80. }
  81. }
  82. private bool _highlightButtons = false;
  83. public bool HighlightButtons
  84. {
  85. get => _highlightButtons;
  86. set
  87. {
  88. _highlightButtons = value;
  89. UpdateButtonHighlight(OKButton, Colors.DarkGreen, Colors.LimeGreen, Colors.White);
  90. UpdateButtonHighlight(CancelButton, Colors.Firebrick, Colors.Red, Colors.White);
  91. }
  92. }
  93. public double ContentWidth => Editor.TotalWidth;
  94. public double ContentHeight => Editor.TotalHeight;
  95. private void UpdateButtonEnabled()
  96. {
  97. OKButton.IsEnabled = !ReadOnly && (!DisableOKIfUnchanged || bChanged);
  98. CancelButton.IsEnabled = !ReadOnly;
  99. }
  100. private void UpdateButtonHighlight(Button button, Color border, Color background, Color foreground)
  101. {
  102. button.BorderBrush = _highlightButtons
  103. ? new SolidColorBrush(border)
  104. : new SolidColorBrush(Colors.Gray);
  105. button.Background = _highlightButtons
  106. ? new SolidColorBrush(background)
  107. : new SolidColorBrush(Colors.Gainsboro);
  108. button.Foreground = _highlightButtons
  109. ? new SolidColorBrush(foreground)
  110. : new SolidColorBrush(Colors.Black);
  111. button.FontWeight = _highlightButtons
  112. ? FontWeights.Bold
  113. : FontWeights.Normal;
  114. }
  115. public static readonly DependencyProperty ButtonsVisibleProperty =
  116. DependencyProperty.Register(
  117. nameof(ButtonsVisible),
  118. typeof(bool),
  119. typeof(EmbeddedDynamicEditorForm),
  120. new UIPropertyMetadata(true)
  121. );
  122. public bool ButtonsVisible
  123. {
  124. get => (bool)GetValue(ButtonsVisibleProperty);
  125. set
  126. {
  127. SetValue(ButtonsVisibleProperty, value);
  128. UpdateButtonsRowVisibility();
  129. }
  130. }
  131. private void UpdateButtonsRowVisibility()
  132. {
  133. ButtonRow.Height = ButtonsVisible
  134. ? new GridLength(1, GridUnitType.Auto)
  135. : new GridLength(0, GridUnitType.Pixel);
  136. }
  137. public static readonly DependencyProperty TabsVisibleProperty =
  138. DependencyProperty.Register(
  139. nameof(TabsVisible),
  140. typeof(bool),
  141. typeof(EmbeddedDynamicEditorForm),
  142. new UIPropertyMetadata(true)
  143. );
  144. public bool TabsVisible
  145. {
  146. get => (bool)GetValue(TabsVisibleProperty);
  147. set
  148. {
  149. SetValue(TabsVisibleProperty, value);
  150. UpdateTabsVisibility();
  151. }
  152. }
  153. private void UpdateTabsVisibility()
  154. {
  155. Editor.TabStripVisible = TabsVisible;
  156. }
  157. #region Events
  158. public event OnValidateData? OnValidateData;
  159. public OnCustomiseColumns? OnCustomiseColumns { get; set; }
  160. public OnDefineLookupFilter? OnDefineFilter { get; set; }
  161. public OnDefineLookup? OnDefineLookups { get; set; }
  162. public DefineEditorEventHandler? OnDefineEditor { get; set; }
  163. public event OnFormCustomiseEditor? OnFormCustomiseEditor;
  164. public OnReconfigureEditors? OnReconfigureEditors { get; set; }
  165. public event EditorValueChangedHandler? OnEditorValueChanged;
  166. public event OnAfterEditorValueChanged? OnAfterEditorValueChanged;
  167. public event OnSelectPage? OnSelectPage;
  168. public DynamicGridSaveEvent? OnSaveItem { get; set; }
  169. public DynamicEditorGrid.EditorCreatedHandler? OnEditorCreated;
  170. public event OKEvent? OnOK;
  171. public event CancelEvent? OnCancel;
  172. #endregion
  173. public EmbeddedDynamicEditorForm()
  174. {
  175. InitializeComponent();
  176. ReadOnly = false;
  177. Editor.Form = this;
  178. }
  179. public override void OnApplyTemplate()
  180. {
  181. base.OnApplyTemplate();
  182. UpdateButtonsRowVisibility();
  183. UpdateTabsVisibility();
  184. }
  185. public EmbeddedDynamicEditorForm(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  186. Func<Type, CoreTable>? PageDataHandler = null, bool PreloadPages = false): this()
  187. {
  188. Setup(type, pages, buttons, PageDataHandler, PreloadPages);
  189. }
  190. private IFilter? Editor_OnDefineFilter(Type type, string column)
  191. {
  192. return OnDefineFilter?.Invoke(type, column);
  193. }
  194. private void ClearEvents()
  195. {
  196. // These are events that are set by the grid/host.
  197. OnEditorValueChanged = null;
  198. OnFormCustomiseEditor = null;
  199. OnAfterEditorValueChanged = null;
  200. OnSelectPage = null;
  201. OnValidateData = null;
  202. }
  203. public void Setup(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  204. Func<Type, CoreTable?>? PageDataHandler = null, bool PreloadPages = false)
  205. {
  206. ClearEvents();
  207. Editor.UnderlyingType = type;
  208. Editor.OnLoadPage = page => { page.Load(Items.First(), PageDataHandler); };
  209. Editor.PreloadPages = PreloadPages;
  210. Pages = pages ?? new DynamicEditorPages();
  211. Buttons.Children.Clear();
  212. if (buttons != null)
  213. foreach (var button in buttons)
  214. {
  215. var btn = new Button();
  216. UpdateButton(btn, button.Image, button.Name);
  217. btn.Tag = button;
  218. btn.Margin = new Thickness(0, 0, 5, 0);
  219. btn.Padding = new Thickness(5, 0, 5, 0);
  220. btn.Click += Btn_Click;
  221. Buttons.Children.Add(btn);
  222. button.Button = btn;
  223. button.Form = this;
  224. }
  225. }
  226. public void UnloadEditorPages(bool saved)
  227. {
  228. Editor.UnloadPages(saved);
  229. }
  230. protected void UpdateButton(Button button, BitmapImage? image, string text)
  231. {
  232. var stackPnl = new StackPanel();
  233. stackPnl.Orientation = Orientation.Horizontal;
  234. //stackPnl.Margin = new Thickness(2);
  235. if (image != null)
  236. {
  237. var img = new Image();
  238. img.Source = image;
  239. img.Margin = new Thickness(2);
  240. stackPnl.Children.Add(img);
  241. }
  242. if (!string.IsNullOrEmpty(text))
  243. {
  244. var lbl = new Label();
  245. lbl.Content = text;
  246. lbl.VerticalAlignment = VerticalAlignment.Stretch;
  247. lbl.VerticalContentAlignment = VerticalAlignment.Center;
  248. lbl.Margin = new Thickness(2, 0, 5, 0);
  249. stackPnl.Children.Add(lbl);
  250. }
  251. button.Content = stackPnl;
  252. }
  253. private Dictionary<string, object?> EditorValueChanged(IDynamicEditorForm sender, string name, object value)
  254. {
  255. if (OnEditorValueChanged != null)
  256. return OnEditorValueChanged(sender, name, value);
  257. return DynamicGridUtils.UpdateEditorValue(_items, name, value);
  258. }
  259. private void Editor_OnEditorCreated(object sender, double height, double width)
  260. {
  261. OnEditorCreated?.Invoke(sender, height, width);
  262. Editor.VerticalAlignment = VerticalAlignment.Stretch;
  263. Editor.HorizontalAlignment = HorizontalAlignment.Stretch;
  264. }
  265. private void Editor_OnCustomiseColumns(object sender, DynamicGridColumns columns)
  266. {
  267. columns.Clear();
  268. if (_items != null && _items.Any())
  269. columns.ExtractColumns(_items.First().GetType());
  270. OnCustomiseColumns?.Invoke(this, columns);
  271. }
  272. private void Btn_Click(object sender, RoutedEventArgs e)
  273. {
  274. var button = (Button)sender;
  275. var deb = (DynamicEditorButton)button.Tag;
  276. deb.Click();
  277. }
  278. private void OKButton_Click(object sender, RoutedEventArgs e)
  279. {
  280. var errors = OnValidateData?.Invoke(this, Items);
  281. if (errors != null && errors.Any())
  282. {
  283. MessageWindow.ShowMessage(
  284. string.Format("The following errors have been found with your data!\nPlease correct them and try again.\n\n- {0}",
  285. string.Join("\n- ", errors)),
  286. "Validation Error");
  287. return;
  288. }
  289. OnOK?.Invoke();
  290. //OKButton.IsEnabled = false;
  291. //CancelButton.IsEnabled = false;
  292. // Don't Commit the changes here, because we want to refer back to thos changes when we save the item
  293. // to trigger specific processes in the database
  294. //Close();
  295. }
  296. private void CancelButton_Click(object sender, RoutedEventArgs e)
  297. {
  298. // However, if we cancel the edits, then we can safely revert the items back to their original (loaded) state
  299. foreach (var item in _items)
  300. item.CancelChanges();
  301. foreach(var page in Editor.Pages)
  302. {
  303. page.Cancel();
  304. }
  305. OnCancel?.Invoke();
  306. //OKButton.IsEnabled = false;
  307. //CancelButton.IsEnabled = false;
  308. //Close();
  309. }
  310. public void SaveItem(CancelEventArgs e)
  311. {
  312. OnSaveItem?.Invoke(this, e);
  313. }
  314. public bool TryFindEditor(string columnname, [NotNullWhen(true)] out IDynamicEditorControl? editor)
  315. {
  316. return Editor.TryFindEditor(columnname, out editor);
  317. }
  318. public object? GetEditorValue(string columnName) => this.FindEditor(columnName).GetValue(columnName);
  319. public void SetEditorValue(string columnName, object? value) => this.FindEditor(columnName).SetValue(columnName, value);
  320. public void SetLayoutType<T>() where T : DynamicEditorGridLayout => Editor.SetLayoutType<T>();
  321. public void SetLayoutType(Type t) => Editor.SetLayoutType(t);
  322. public void SetLayout(DynamicEditorGridLayout layout) => Editor.SetLayout(layout);
  323. private void Editor_OnSelectPage(DynamicEditorGrid sender, BaseObject[] items)
  324. {
  325. OnSelectPage?.Invoke(sender, items);
  326. }
  327. private void Editor_OnUnloadPage(IDynamicEditorPage page, bool saved)
  328. {
  329. if (!saved)
  330. page.BeforeSave(Items.First());
  331. else
  332. page.AfterSave(Items.First());
  333. }
  334. private Dictionary<string, object?>? Editor_OnAfterEditorValueChanged(DynamicEditorGrid sender, AfterEditorValueChangedArgs args)
  335. {
  336. if(args.ChangedValues.Count > 0)
  337. {
  338. DoChanged();
  339. }
  340. return OnAfterEditorValueChanged?.Invoke(sender, args);
  341. }
  342. private void Editor_OnReconfigureEditors(DynamicEditorGrid sender)
  343. {
  344. OnReconfigureEditors?.Invoke(sender);
  345. }
  346. private void Editor_OnGridCustomiseEditor(DynamicEditorGrid sender, DynamicGridColumn column, BaseEditor editor)
  347. {
  348. OnFormCustomiseEditor?.Invoke(this, Items, column, editor);
  349. }
  350. private decimal Editor_OnGetSequence(DynamicGridColumn column)
  351. {
  352. if (_items.Any())
  353. return CoreUtils.GetPropertySequence(_items.First().GetType(), column.ColumnName);
  354. return 0.0M;
  355. }
  356. private void Editor_OnDefineLookups(ILookupEditorControl editor)
  357. {
  358. OnDefineLookups?.Invoke(editor);
  359. }
  360. private BaseObject[] Editor_GetItems()
  361. {
  362. return _items;
  363. }
  364. private BaseEditor Editor_OnGetEditor(DynamicGridColumn column)
  365. {
  366. if (_items != null && _items.Any())
  367. {
  368. var property = DatabaseSchema.Property(Editor.UnderlyingType, column.ColumnName);
  369. if (property == null) return new NullEditor();
  370. if (property.Editor is NullEditor)
  371. return property.Editor.CloneEditor();
  372. BaseEditor editor;
  373. if (property is CustomProperty)
  374. {
  375. editor = property.Editor.CloneEditor();
  376. }
  377. else
  378. {
  379. editor = OnDefineEditor?.Invoke(_items[0], column) ?? column.Editor.CloneEditor();
  380. var propEditor = property.Editor;
  381. editor.Page = propEditor.Page;
  382. editor.Caption = propEditor.Caption;
  383. }
  384. //if (ReadOnly && editor.Editable.IsEditable())
  385. // editor.Editable = Editable.Disabled;
  386. return editor;
  387. }
  388. return new NullEditor();
  389. }
  390. private object? Editor_OnGetPropertyValue(object sender, string column)
  391. {
  392. if (!_items.Any())
  393. return null;
  394. object? result;
  395. try
  396. {
  397. result = CoreUtils.GetPropertyValue(_items.First(), column);
  398. }
  399. catch
  400. {
  401. result = _items.First().UserProperties.ContainsKey(column) ? _items.First().UserProperties[column] : null;
  402. }
  403. if (result == null)
  404. return null;
  405. foreach (var _item in _items)
  406. {
  407. object? curvalue;
  408. try
  409. {
  410. curvalue = CoreUtils.GetPropertyValue(_item, column);
  411. }
  412. catch
  413. {
  414. curvalue = _item.UserProperties.ContainsKey(column) ? _item.UserProperties[column] : null;
  415. }
  416. if (curvalue == null)
  417. return null;
  418. if (!curvalue.Equals(result))
  419. return null;
  420. }
  421. return result;
  422. }
  423. private void Editor_OnSetPropertyValue(object sender, string column, object value)
  424. {
  425. foreach (var _item in _items)
  426. if (_item.UserProperties.ContainsKey(column))
  427. _item.UserProperties[column] = value;
  428. else
  429. CoreUtils.SetPropertyValue(_item, column, value);
  430. }
  431. //public void EditLayout() => Editor.EditLayout();
  432. //public void ResetLayout() => Editor.ResetLayout();
  433. public void AddButton(Button button)
  434. {
  435. Buttons.Children.Add(button);
  436. }
  437. private Dictionary<string, object?> Editor_OnEditorValueChanged(DynamicEditorGrid sender, string name, object value)
  438. {
  439. return EditorValueChanged(this, name, value);
  440. }
  441. private void Editor_OnReload(DynamicEditorGrid sender)
  442. {
  443. OnReload?.Invoke(this);
  444. }
  445. }
  446. }