EmbeddedDynamicEditorForm.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 OnCustomiseColumns? OnCustomiseColumns { get; set; }
  86. public OnDefineFilter? OnDefineFilter { get; set; }
  87. public OnDefineLookup? OnDefineLookups { get; set; }
  88. public DefineEditorEventHandler? OnDefineEditor { get; set; }
  89. public OnFormCustomiseEditor? OnFormCustomiseEditor { get; set; }
  90. public OnReconfigureEditors? OnReconfigureEditors { get; set; }
  91. public event EditorValueChangedHandler? OnEditorValueChanged;
  92. public event OnAfterEditorValueChanged? OnAfterEditorValueChanged;
  93. public IDynamicEditorForm.GetDocumentEvent? OnGetDocument { get; set; }
  94. public IDynamicEditorForm.FindDocumentEvent? OnFindDocument { get; set; }
  95. public IDynamicEditorForm.SaveDocumentEvent? OnSaveDocument { get; set; }
  96. public event OnSelectPage? OnSelectPage;
  97. public DynamicGridSaveEvent? OnSaveItem { get; set; }
  98. public 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. private IFilter? Editor_OnDefineFilter(Type type)
  118. {
  119. return OnDefineFilter?.Invoke(type);
  120. }
  121. private void ClearEvents()
  122. {
  123. OnEditorValueChanged = null;
  124. OnAfterEditorValueChanged = null;
  125. OnSelectPage = null;
  126. OnValidateData = null;
  127. }
  128. public void Setup(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  129. Func<Type, CoreTable>? PageDataHandler = null, bool PreloadPages = false)
  130. {
  131. ClearEvents();
  132. ReadOnly = false;
  133. Editor.UnderlyingType = type;
  134. Editor.OnLoadPage = page => { page.Load(Items.First(), PageDataHandler); };
  135. Editor.PreloadPages = PreloadPages;
  136. Pages = pages ?? new DynamicEditorPages();
  137. Buttons.Children.Clear();
  138. if (buttons != null)
  139. foreach (var button in buttons)
  140. {
  141. var btn = new Button();
  142. UpdateButton(btn, button.Image, button.Name);
  143. btn.Tag = button;
  144. btn.Margin = new Thickness(5, 5, 0, 5);
  145. btn.Padding = new Thickness(5, 0, 5, 0);
  146. btn.Click += Btn_Click;
  147. Buttons.Children.Add(btn);
  148. button.Button = btn;
  149. button.Form = this;
  150. }
  151. }
  152. public void UnloadEditorPages(bool saved)
  153. {
  154. Editor.UnloadPages(saved);
  155. }
  156. protected void UpdateButton(Button button, BitmapImage? image, string text)
  157. {
  158. var stackPnl = new StackPanel();
  159. stackPnl.Orientation = Orientation.Horizontal;
  160. //stackPnl.Margin = new Thickness(2);
  161. if (image != null)
  162. {
  163. var img = new Image();
  164. img.Source = image;
  165. img.Margin = new Thickness(2);
  166. stackPnl.Children.Add(img);
  167. }
  168. if (!string.IsNullOrEmpty(text))
  169. {
  170. var lbl = new Label();
  171. lbl.Content = text;
  172. lbl.VerticalAlignment = VerticalAlignment.Stretch;
  173. lbl.VerticalContentAlignment = VerticalAlignment.Center;
  174. lbl.Margin = new Thickness(2, 0, 5, 0);
  175. stackPnl.Children.Add(lbl);
  176. }
  177. button.Content = stackPnl;
  178. }
  179. private Dictionary<string, object?> EditorValueChanged(object sender, string name, object value)
  180. {
  181. if (OnEditorValueChanged != null)
  182. return OnEditorValueChanged(sender, name, value);
  183. return DynamicGridUtils.UpdateEditorValue(_items, name, value);
  184. }
  185. private void Editor_OnEditorCreated(object sender, double height, double width)
  186. {
  187. OnEditorCreated?.Invoke(sender, height, width);
  188. Editor.VerticalAlignment = VerticalAlignment.Stretch;
  189. Editor.HorizontalAlignment = HorizontalAlignment.Stretch;
  190. OKButton.IsEnabled = !ReadOnly;
  191. }
  192. private void Editor_OnCustomiseColumns(object sender, DynamicGridColumns columns)
  193. {
  194. columns.Clear();
  195. if (_items != null && _items.Any())
  196. columns.ExtractColumns(_items.First().GetType());
  197. OnCustomiseColumns?.Invoke(this, columns);
  198. }
  199. private void Btn_Click(object sender, RoutedEventArgs e)
  200. {
  201. var button = (Button)sender;
  202. var deb = (DynamicEditorButton)button.Tag;
  203. deb.Click();
  204. }
  205. private void OKButton_Click(object sender, RoutedEventArgs e)
  206. {
  207. var errors = OnValidateData?.Invoke(this, Items);
  208. if (errors != null && errors.Any())
  209. {
  210. MessageBox.Show(
  211. string.Format("The following errors have been found with your data!\nPlease correct them and try again.\n\n- {0}",
  212. string.Join("\n- ", errors)), "Validation Error");
  213. return;
  214. }
  215. OnOK?.Invoke();
  216. // Don't Commit the changes here, because we want to refer back to thos changes when we save the item
  217. // to trigger specific processes in the database
  218. //Close();
  219. }
  220. private void CancelButton_Click(object sender, RoutedEventArgs e)
  221. {
  222. // However, if we cancel the edits, then we can safely revert the items back to their original (loaded) state
  223. foreach (var item in _items)
  224. item.CancelChanges();
  225. OnCancel?.Invoke();
  226. //Close();
  227. }
  228. public void SaveItem(CancelEventArgs e)
  229. {
  230. OnSaveItem?.Invoke(this, e);
  231. }
  232. public bool TryFindEditor(string columnname, [NotNullWhen(true)] out IDynamicEditorControl? editor)
  233. {
  234. return Editor.TryFindEditor(columnname, out editor);
  235. }
  236. public IDynamicEditorControl FindEditor(string columnname)
  237. {
  238. return Editor.FindEditor(columnname);
  239. }
  240. public object? GetEditorValue(string columnName) => FindEditor(columnName).GetValue(columnName);
  241. public void SetEditorValue(string columnName, object? value) => FindEditor(columnName).SetValue(columnName, value);
  242. public void SetLayoutType<T>() where T : DynamicEditorGridLayout => Editor.SetLayoutType<T>();
  243. private void Editor_OnSelectPage(DynamicEditorGrid sender, BaseObject[] items)
  244. {
  245. OnSelectPage?.Invoke(sender, items);
  246. }
  247. private void Editor_OnUnloadPage(IDynamicEditorPage page, bool saved)
  248. {
  249. if (!saved)
  250. page.BeforeSave(Items.First());
  251. else
  252. page.AfterSave(Items.First());
  253. }
  254. private Dictionary<string, object?>? Editor_OnAfterEditorValueChanged(DynamicEditorGrid sender, string columnname)
  255. {
  256. return OnAfterEditorValueChanged?.Invoke(sender, columnname);
  257. }
  258. private void Editor_OnReconfigureEditors(DynamicEditorGrid sender)
  259. {
  260. OnReconfigureEditors?.Invoke(sender);
  261. }
  262. private void Editor_OnGridCustomiseEditor(DynamicEditorGrid sender, DynamicGridColumn column, BaseEditor editor)
  263. {
  264. OnFormCustomiseEditor?.Invoke(this, Items, column, editor);
  265. }
  266. private decimal Editor_OnGetSequence(DynamicGridColumn column)
  267. {
  268. return CoreUtils.GetPropertySequence(_items.First().GetType(), column.ColumnName);
  269. }
  270. private void Editor_OnDefineLookups(ILookupEditorControl editor)
  271. {
  272. OnDefineLookups?.Invoke(editor);
  273. }
  274. private Document? Editor_OnGetDocument(Guid id)
  275. {
  276. return OnGetDocument?.Invoke(id);
  277. }
  278. private Document? Editor_OnFindDocument(string file)
  279. {
  280. return OnFindDocument?.Invoke(file);
  281. }
  282. private void Editor_OnSaveDocument(Document document)
  283. {
  284. OnSaveDocument?.Invoke(document);
  285. }
  286. private object?[] Editor_GetItems()
  287. {
  288. return _items;
  289. }
  290. private BaseEditor Editor_OnGetEditor(DynamicGridColumn column)
  291. {
  292. if (_items != null && _items.Any())
  293. {
  294. var property = DatabaseSchema.Property(Editor.UnderlyingType, column.ColumnName);
  295. if (property == null) return new NullEditor();
  296. if (property.Editor is NullEditor)
  297. return property.Editor;
  298. BaseEditor editor;
  299. if (property is CustomProperty)
  300. {
  301. editor = property.Editor.CloneEditor();
  302. }
  303. else
  304. {
  305. editor = OnDefineEditor?.Invoke(_items[0], column) ?? column.Editor.CloneEditor();
  306. var propEditor = property.Editor;
  307. editor.Page = propEditor.Page;
  308. editor.Caption = propEditor.Caption;
  309. }
  310. if (ReadOnly && editor.Editable.Equals(Editable.Enabled))
  311. editor.Editable = Editable.Disabled;
  312. return editor;
  313. }
  314. return new NullEditor();
  315. }
  316. private object? Editor_OnGetPropertyValue(object sender, string column)
  317. {
  318. if (!_items.Any())
  319. return null;
  320. object? result;
  321. try
  322. {
  323. result = CoreUtils.GetPropertyValue(_items.First(), column);
  324. }
  325. catch
  326. {
  327. result = _items.First().UserProperties.ContainsKey(column) ? _items.First().UserProperties[column] : null;
  328. }
  329. if (result == null)
  330. return null;
  331. foreach (var _item in _items)
  332. {
  333. object? curvalue;
  334. try
  335. {
  336. curvalue = CoreUtils.GetPropertyValue(_item, column);
  337. }
  338. catch
  339. {
  340. curvalue = _item.UserProperties.ContainsKey(column) ? _item.UserProperties[column] : null;
  341. }
  342. if (curvalue == null)
  343. return null;
  344. if (!curvalue.Equals(result))
  345. return null;
  346. }
  347. return result;
  348. }
  349. private void Editor_OnSetPropertyValue(object sender, string column, object value)
  350. {
  351. foreach (var _item in _items)
  352. if (_item.UserProperties.ContainsKey(column))
  353. _item.UserProperties[column] = value;
  354. else
  355. CoreUtils.SetPropertyValue(_item, column, value);
  356. }
  357. //public void EditLayout() => Editor.EditLayout();
  358. //public void ResetLayout() => Editor.ResetLayout();
  359. }
  360. }