EmbeddedDynamicEditorForm.xaml.cs 15 KB

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