EmbeddedDynamicEditorForm.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using Document = InABox.Core.Document;
  19. namespace InABox.DynamicGrid
  20. {
  21. /// <summary>
  22. /// Interaction logic for EmbeddedDynamicEditorForm.xaml
  23. /// </summary>
  24. public partial class EmbeddedDynamicEditorForm : UserControl, IDynamicEditorForm
  25. {
  26. public delegate void OKEvent();
  27. public delegate void CancelEvent();
  28. public DynamicEditorPages Pages { get; private set; } = new();
  29. private BaseObject[] _items;
  30. public BaseObject[] Items
  31. {
  32. get => _items;
  33. set
  34. {
  35. _items = value;
  36. DynamicEditorFormModel.Slug = Items != null ? Items.Any() ? Items.First().GetType().EntityName().Split('.').Last() : "" : "";
  37. Editor.Load(Pages);
  38. }
  39. }
  40. public bool ReadOnly { get; set; }
  41. public static readonly DependencyProperty ButtonsVisibleProperty =
  42. DependencyProperty.Register(
  43. nameof(ButtonsVisible),
  44. typeof(bool),
  45. typeof(EmbeddedDynamicEditorForm),
  46. new UIPropertyMetadata(true)
  47. );
  48. public bool ButtonsVisible
  49. {
  50. get => (bool)GetValue(ButtonsVisibleProperty);
  51. set
  52. {
  53. SetValue(ButtonsVisibleProperty, value);
  54. UpdateButtonsRowVisibility();
  55. }
  56. }
  57. private void UpdateButtonsRowVisibility()
  58. {
  59. ButtonRow.Height = ButtonsVisible
  60. ? new GridLength(40, GridUnitType.Pixel)
  61. : new GridLength(0, GridUnitType.Pixel);
  62. }
  63. public static readonly DependencyProperty TabsVisibleProperty =
  64. DependencyProperty.Register(
  65. nameof(TabsVisible),
  66. typeof(bool),
  67. typeof(EmbeddedDynamicEditorForm),
  68. new UIPropertyMetadata(true)
  69. );
  70. public bool TabsVisible
  71. {
  72. get => (bool)GetValue(TabsVisibleProperty);
  73. set
  74. {
  75. SetValue(TabsVisibleProperty, value);
  76. UpdateTabsVisibility();
  77. }
  78. }
  79. private void UpdateTabsVisibility()
  80. {
  81. Editor.TabStripVisible = TabsVisible;
  82. }
  83. #region Events
  84. public event OnValidateData? OnValidateData;
  85. public event OnCustomiseColumns? OnCustomiseColumns;
  86. public event OnDefineFilter? OnDefineFilter;
  87. public event OnDefineLookup? OnDefineLookups;
  88. public event DefineEditorEventHandler? OnDefineEditor;
  89. public event OnFormCustomiseEditor? OnFormCustomiseEditor;
  90. public event OnReconfigureEditors? OnReconfigureEditors;
  91. public event EditorValueChangedHandler? OnEditorValueChanged;
  92. public event OnAfterEditorValueChanged? OnAfterEditorValueChanged;
  93. public event IDynamicEditorForm.GetDocumentEvent? OnGetDocument;
  94. public event IDynamicEditorForm.FindDocumentEvent? OnFindDocument;
  95. public event IDynamicEditorForm.SaveDocumentEvent? OnSaveDocument;
  96. public event OnSelectPage? OnSelectPage;
  97. public event DynamicGridSaveEvent? OnSaveItem;
  98. public event DynamicEditorGrid.EditorCreatedHandler? OnEditorCreated;
  99. public event OKEvent? OnOK;
  100. public event CancelEvent? OnCancel;
  101. #endregion
  102. public EmbeddedDynamicEditorForm()
  103. {
  104. InitializeComponent();
  105. }
  106. public override void OnApplyTemplate()
  107. {
  108. base.OnApplyTemplate();
  109. UpdateButtonsRowVisibility();
  110. UpdateTabsVisibility();
  111. }
  112. public EmbeddedDynamicEditorForm(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  113. Func<Type, CoreTable>? PageDataHandler = null, bool PreloadPages = false): this()
  114. {
  115. Setup(type, pages, buttons, PageDataHandler, PreloadPages);
  116. }
  117. public void Setup(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  118. Func<Type, CoreTable>? PageDataHandler = null, bool PreloadPages = false)
  119. {
  120. ReadOnly = false;
  121. //this.Loaded += new RoutedEventHandler(ConfigureSystemMenu);
  122. Editor.UnderlyingType = type;
  123. Editor.OnCustomiseColumns += Editor_OnCustomiseColumns;
  124. Editor.OnDefineFilter += editor => OnDefineFilter?.Invoke(editor);
  125. Editor.OnEditorCreated += Editor_OnEditorCreated;
  126. Editor.OnLoadPage += page => { page.Load(Items.First(), PageDataHandler); };
  127. Editor.OnSelectPage += (tab, items) => { OnSelectPage?.Invoke(tab, items); };
  128. Editor.PreloadPages = PreloadPages;
  129. Editor.OnUnloadPage += (page, saved) =>
  130. {
  131. if (!saved)
  132. page.BeforeSave(Items.First());
  133. else
  134. page.AfterSave(Items.First());
  135. };
  136. //Editor.OnGetPropertyInfo += (o, c) => { return CoreUtils.GetProperty(_item.GetType(), c); };
  137. Editor.OnAfterEditorValueChanged += (g, n) => { return OnAfterEditorValueChanged?.Invoke(g, n); };
  138. Editor.OnReconfigureEditors += g => { OnReconfigureEditors?.Invoke(g); };
  139. Editor.OnGetEditor += c =>
  140. {
  141. if (_items != null && _items.Any())
  142. {
  143. var property = DatabaseSchema.Property(type, c.ColumnName);
  144. if (property == null) return new NullEditor();
  145. if (property.Editor is NullEditor)
  146. return property.Editor;
  147. BaseEditor editor;
  148. if (property is CustomProperty)
  149. {
  150. editor = property.Editor.CloneEditor();
  151. }
  152. else
  153. {
  154. editor = OnDefineEditor?.Invoke(_items[0], c) ?? c.Editor.CloneEditor();
  155. var propEditor = property.Editor;
  156. editor.Page = propEditor.Page;
  157. editor.Caption = propEditor.Caption;
  158. }
  159. //defaultEditor.EditorSequence
  160. //EditorUtils.GetPropertyEditor(type, property, defaultEditor);
  161. /*BaseEditor editor = new NullEditor();
  162. var caption = "";
  163. var page = "";
  164. try
  165. {
  166. var comps = c.ColumnName.Split('.');
  167. for (var i = 0; i < comps.Length; i++)
  168. {
  169. var column = string.Join(".", comps.Take(i + 1));
  170. var prop = CoreUtils.GetProperty(type, column);
  171. if (column.Equals(c.ColumnName))
  172. {
  173. if (OnDefineEditor != null)
  174. editor = OnDefineEditor(_items[0], c);
  175. else
  176. editor = c.Editor != null ? c.Editor : new NullEditor();
  177. }
  178. else
  179. {
  180. var pedit = prop.GetEditor();
  181. if (pedit is NullEditor)
  182. return pedit;
  183. }
  184. editor = editor == null ? new NullEditor() : editor.Clone() as BaseEditor;
  185. var capattr = prop.GetCustomAttribute<Caption>();
  186. var subcap = capattr != null ? capattr.Text : comps[i];
  187. var path = capattr != null ? capattr.IncludePath : true;
  188. if (!string.IsNullOrWhiteSpace(subcap))
  189. caption = string.IsNullOrWhiteSpace(caption) || path == false ? subcap : string.Format("{0} {1}", caption, subcap);
  190. if (string.IsNullOrWhiteSpace(page))
  191. {
  192. var pageattr = prop.GetCustomAttribute<EditorSequence>();
  193. if (pageattr != null)
  194. page = pageattr.Page;
  195. }
  196. }
  197. editor.Caption = caption;
  198. editor.Page = page;
  199. }
  200. catch (Exception e)
  201. {
  202. var dmprop = DatabaseSchema.Property(_items[0].GetType(), c.ColumnName);
  203. if (dmprop is CustomProperty)
  204. {
  205. editor = dmprop.Editor.Clone() as BaseEditor;
  206. editor.Caption = dmprop.Caption;
  207. editor.Page = string.IsNullOrWhiteSpace(dmprop.Page) ? "Custom Fields" : dmprop.Page;
  208. }
  209. }*/
  210. if (ReadOnly && editor.Editable.Equals(Editable.Enabled))
  211. editor.Editable = Editable.Disabled;
  212. return editor;
  213. }
  214. return new NullEditor();
  215. };
  216. Editor.OnGridCustomiseEditor += (sender, column, editor) => OnFormCustomiseEditor?.Invoke(this, Items, column, editor);
  217. Editor.OnGetSequence += c => CoreUtils.GetPropertySequence(_items.First().GetType(), c.ColumnName);
  218. Editor.OnGetPropertyValue += (o, c) =>
  219. {
  220. if (!_items.Any())
  221. return null;
  222. object? result;
  223. try
  224. {
  225. result = CoreUtils.GetPropertyValue(_items.First(), c);
  226. }
  227. catch
  228. {
  229. result = _items.First().UserProperties.ContainsKey(c) ? _items.First().UserProperties[c] : null;
  230. }
  231. if (result == null)
  232. return null;
  233. foreach (var _item in _items)
  234. {
  235. object? curvalue;
  236. try
  237. {
  238. curvalue = CoreUtils.GetPropertyValue(_item, c);
  239. }
  240. catch
  241. {
  242. curvalue = _item.UserProperties.ContainsKey(c) ? _item.UserProperties[c] : null;
  243. }
  244. if (curvalue == null)
  245. return null;
  246. if (!curvalue.Equals(result))
  247. return null;
  248. }
  249. return result;
  250. };
  251. Editor.OnSetPropertyValue += (o, c, v) =>
  252. {
  253. foreach (var _item in _items)
  254. if (_item.UserProperties.ContainsKey(c))
  255. _item.UserProperties[c] = v;
  256. else
  257. CoreUtils.SetPropertyValue(_item, c, v);
  258. };
  259. Editor.OnEditorValueChanged += EditorValueChanged;
  260. Editor.OnDefineLookups += sender => { OnDefineLookups?.Invoke(sender); };
  261. Editor.OnGetDocument += id => { return OnGetDocument?.Invoke(id); };
  262. Editor.OnSaveDocument += doc => { OnSaveDocument?.Invoke(doc); };
  263. Editor.OnFindDocument += file => { return OnFindDocument?.Invoke(file); };
  264. Editor.GetItems += () => _items;
  265. Pages = pages ?? new DynamicEditorPages();
  266. if (Pages == null || Pages.Count == 0)
  267. Editor.Margin = new Thickness(5, 5, 5, 0);
  268. Buttons.Children.Clear();
  269. if (buttons != null)
  270. foreach (var button in buttons)
  271. {
  272. var btn = new Button();
  273. UpdateButton(btn, button.Image, button.Name);
  274. btn.Tag = button;
  275. btn.Margin = new Thickness(5, 5, 0, 5);
  276. btn.Padding = new Thickness(5, 0, 5, 0);
  277. btn.Click += Btn_Click;
  278. Buttons.Children.Add(btn);
  279. button.Button = btn;
  280. button.Form = this;
  281. }
  282. }
  283. public void UnloadEditorPages(bool saved)
  284. {
  285. Editor.UnloadPages(saved);
  286. }
  287. protected void UpdateButton(Button button, BitmapImage? image, string text)
  288. {
  289. var stackPnl = new StackPanel();
  290. stackPnl.Orientation = Orientation.Horizontal;
  291. //stackPnl.Margin = new Thickness(2);
  292. if (image != null)
  293. {
  294. var img = new Image();
  295. img.Source = image;
  296. img.Margin = new Thickness(2);
  297. stackPnl.Children.Add(img);
  298. }
  299. if (!string.IsNullOrEmpty(text))
  300. {
  301. var lbl = new Label();
  302. lbl.Content = text;
  303. lbl.VerticalAlignment = VerticalAlignment.Stretch;
  304. lbl.VerticalContentAlignment = VerticalAlignment.Center;
  305. lbl.Margin = new Thickness(2, 0, 5, 0);
  306. stackPnl.Children.Add(lbl);
  307. }
  308. button.Content = stackPnl;
  309. }
  310. private Dictionary<string, object?> EditorValueChanged(object sender, string name, object value)
  311. {
  312. if (OnEditorValueChanged != null)
  313. return OnEditorValueChanged(sender, name, value);
  314. return DynamicGridUtils.UpdateEditorValue(_items, name, value);
  315. }
  316. private void Editor_OnEditorCreated(object sender, double height, double width)
  317. {
  318. OnEditorCreated?.Invoke(sender, height, width);
  319. Editor.VerticalAlignment = VerticalAlignment.Stretch;
  320. Editor.HorizontalAlignment = HorizontalAlignment.Stretch;
  321. OKButton.IsEnabled = !ReadOnly;
  322. }
  323. private void Editor_OnCustomiseColumns(object sender, DynamicGridColumns columns)
  324. {
  325. columns.Clear();
  326. if (_items != null && _items.Any())
  327. columns.ExtractColumns(_items.First().GetType());
  328. OnCustomiseColumns?.Invoke(this, columns);
  329. }
  330. private void Btn_Click(object sender, RoutedEventArgs e)
  331. {
  332. var button = (Button)sender;
  333. var deb = (DynamicEditorButton)button.Tag;
  334. deb.Click();
  335. }
  336. private void OKButton_Click(object sender, RoutedEventArgs e)
  337. {
  338. var errors = OnValidateData?.Invoke(this, Items);
  339. if (errors != null && errors.Any())
  340. {
  341. MessageBox.Show(
  342. string.Format("The following errors have been found with your data!\nPlease correct them and try again.\n\n- {0}",
  343. string.Join("\n- ", errors)), "Validation Error");
  344. return;
  345. }
  346. OnOK?.Invoke();
  347. // Don't Commit the changes here, because we want to refer back to thos changes when we save the item
  348. // to trigger specific processes in the database
  349. //Close();
  350. }
  351. private void CancelButton_Click(object sender, RoutedEventArgs e)
  352. {
  353. // However, if we cancel the edits, then we can safely revert the items back to their original (loaded) state
  354. foreach (var item in _items)
  355. item.CancelChanges();
  356. OnCancel?.Invoke();
  357. //Close();
  358. }
  359. public void SaveItem(CancelEventArgs e)
  360. {
  361. OnSaveItem?.Invoke(this, e);
  362. }
  363. public bool TryFindEditor(string columnname, [NotNullWhen(true)] out IDynamicEditorControl? editor)
  364. {
  365. return Editor.TryFindEditor(columnname, out editor);
  366. }
  367. public IDynamicEditorControl FindEditor(string columnname)
  368. {
  369. return Editor.FindEditor(columnname);
  370. }
  371. public object? GetEditorValue(string columnName) => FindEditor(columnName).GetValue(columnName);
  372. public void SetEditorValue(string columnName, object? value) => FindEditor(columnName).SetValue(columnName, value);
  373. public void SetLayoutType<T>() where T : DynamicEditorGridLayout => Editor.SetLayoutType<T>();
  374. //public void EditLayout() => Editor.EditLayout();
  375. //public void ResetLayout() => Editor.ResetLayout();
  376. }
  377. }