DynamicEditorForm.xaml.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Windows;
  6. using InABox.Core;
  7. using InABox.Wpf;
  8. using InABox.WPF;
  9. using Syncfusion.Windows.Shared;
  10. using Syncfusion.Windows.Tools.Controls;
  11. namespace InABox.DynamicGrid;
  12. public class DynamicEditorFormModel
  13. {
  14. /// <summary>
  15. /// Constructor of the UtilityViewModel class.
  16. /// </summary>
  17. public DynamicEditorFormModel()
  18. {
  19. var utilities = new ObservableCollection<UtilityItem>();
  20. utilities.Add(new UtilityItem
  21. { Name = "Help", Icon = Resources.help.AsBitmapImage(), Text = "", Mode = SizeMode.Normal, Command = HelpCommand });
  22. Utilities = utilities;
  23. }
  24. /// <summary>
  25. /// Collection containing the complete details of the items to be bound in the title bar.
  26. /// </summary>
  27. public ObservableCollection<UtilityItem> Utilities { get; }
  28. /// <summary>
  29. /// Commmand for the Help button.
  30. /// </summary>
  31. public DelegateCommand HelpCommand => new(HelpCommandAction);
  32. public static string Slug { get; set; }
  33. /// <summary>
  34. /// Action that is performed when clicking the help button.
  35. /// </summary>
  36. private void HelpCommandAction(object param)
  37. {
  38. Process.Start("https://prsdigital.com.au/wiki/index.php/" + Slug);
  39. }
  40. }
  41. /// <summary>
  42. /// Interaction logic for DynamicEditor.xaml
  43. /// </summary>
  44. public partial class DynamicEditorForm : ThemableChromelessWindow, IDynamicEditorForm, ISubPanel
  45. {
  46. #region IDynamicEditorForm
  47. public bool ReadOnly { get => Form.ReadOnly; set => Form.ReadOnly = value; }
  48. public BaseObject[] Items
  49. {
  50. get => Form.Items;
  51. set
  52. {
  53. Form.Items = value;
  54. }
  55. }
  56. public DynamicEditorPages? Pages { get => Form.Pages; }
  57. public event OnBeforeLoad? OnBeforeLoad;
  58. public void BeforeLoad() => OnBeforeLoad?.Invoke(this);
  59. public event OnAfterLoad? OnAfterLoad;
  60. public void AfterLoad() => OnAfterLoad?.Invoke(this);
  61. public event OnValidateData? OnValidateData { add => Form.OnValidateData += value; remove => Form.OnValidateData -= value; }
  62. public OnCustomiseColumns? OnCustomiseColumns
  63. {
  64. get => Form.OnCustomiseColumns;
  65. set => Form.OnCustomiseColumns = value;
  66. }
  67. public OnDefineLookupFilter? OnDefineFilter { get => Form.OnDefineFilter; set { Form.OnDefineFilter = value; } }
  68. public OnDefineLookup? OnDefineLookups { get => Form.OnDefineLookups; set { Form.OnDefineLookups = value; } }
  69. public DefineEditorEventHandler? OnDefineEditor { get => Form.OnDefineEditor; set { Form.OnDefineEditor = value; } }
  70. public event OnFormCustomiseEditor? OnFormCustomiseEditor;
  71. public OnReconfigureEditors? OnReconfigureEditors { get => Form.OnReconfigureEditors; set { Form.OnReconfigureEditors = value; } }
  72. public event OnAfterEditorValueChanged? OnAfterEditorValueChanged { add => Form.OnAfterEditorValueChanged += value; remove => Form.OnAfterEditorValueChanged -= value; }
  73. public event EditorValueChangedHandler? OnEditorValueChanged { add => Form.OnEditorValueChanged += value; remove => Form.OnEditorValueChanged -= value; }
  74. public event OnSelectPage? OnSelectPage { add => Form.OnSelectPage += value; remove => Form.OnSelectPage -= value; }
  75. public DynamicGridSaveEvent? OnSaveItem { get => Form.OnSaveItem; set { Form.OnSaveItem = value; } }
  76. public event IDynamicEditorForm.OnReloadEventHandler? OnReload { add => Form.OnReload += value; remove => Form.OnReload -= value; }
  77. public IDynamicEditorControl FindEditor(string columnName) => Form.FindEditor(columnName);
  78. public object? GetEditorValue(string columnName) => Form.GetEditorValue(columnName);
  79. public void SetEditorValue(string columnName, object? value) => Form.SetEditorValue(columnName, value);
  80. public void UnloadEditorPages(bool saved) => Form.UnloadEditorPages(saved);
  81. #endregion
  82. public bool? Result { get; set; }
  83. public DynamicEditorForm()
  84. {
  85. InitializeComponent();
  86. Form.OnEditorCreated += Editor_OnEditorCreated;
  87. Form.OnOK += Form_OnOK;
  88. Form.OnCancel += Form_OnCancel;
  89. Form.OnFormCustomiseEditor += (sender, items, column, editor) => OnFormCustomiseEditor?.Invoke(sender, items, column, editor);
  90. }
  91. public DynamicEditorForm(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  92. Func<Type, CoreTable>? pageDataHandler = null, bool preloadPages = false): this()
  93. {
  94. Setup(type, pages, buttons, pageDataHandler, preloadPages);
  95. }
  96. public void Setup(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  97. Func<Type, CoreTable?>? pageDataHandler = null, bool preloadPages = false)
  98. {
  99. Form.Setup(type, pages, buttons, pageDataHandler, preloadPages);
  100. }
  101. public void SetLayoutType<T>() where T : DynamicEditorGridLayout => Form.SetLayoutType<T>();
  102. public void SetLayout(DynamicEditorGridLayout layout) => Form.SetLayout(layout);
  103. // Providing new implementation to avoid using DIalogResult, which breaks if non-modal.
  104. public new bool? ShowDialog()
  105. {
  106. base.ShowDialog();
  107. return Result;
  108. }
  109. private void Form_OnCancel()
  110. {
  111. if (bChanged)
  112. {
  113. var result = MessageWindow.ShowYesNoCancel("Save Changes?", "Confirm");
  114. switch (result)
  115. {
  116. case MessageWindowResult.Yes:
  117. Result = true;
  118. Close();
  119. break;
  120. case MessageWindowResult.No:
  121. Result = false;
  122. Close();
  123. break;
  124. }
  125. }
  126. else
  127. {
  128. Result = false;
  129. Close();
  130. }
  131. }
  132. private void Form_OnOK()
  133. {
  134. Result = true;
  135. Close();
  136. }
  137. private void Editor_OnEditorCreated(object sender, double height, double width)
  138. {
  139. }
  140. private void Window_Closing(object sender, CancelEventArgs e)
  141. {
  142. if (bChanged && Result == null)
  143. {
  144. var result = MessageWindow.ShowYesNoCancel("Save Changes?", "Confirm");
  145. switch (result)
  146. {
  147. case MessageWindowResult.Yes:
  148. Result = true;
  149. break;
  150. case MessageWindowResult.No:
  151. Result = false;
  152. break;
  153. case MessageWindowResult.Cancel:
  154. e.Cancel = true;
  155. return;
  156. }
  157. }
  158. if (Result == true)
  159. Form.SaveItem(e);
  160. SubPanelClosed?.Invoke(this);
  161. }
  162. private bool bChanged = false;
  163. private void Form_OnOnChanged(object? sender, EventArgs e)
  164. {
  165. bChanged = true;
  166. }
  167. private void Form_OnReload(IDynamicEditorForm form)
  168. {
  169. SetSize();
  170. }
  171. private void SetSize()
  172. {
  173. var screen = WpfScreen.GetScreenFrom(new Point(Left, Top));
  174. double spareheight = 90;
  175. double sparewidth = 25;
  176. var desiredheight = Form.ContentHeight + spareheight;
  177. var desiredwidth = Form.ContentWidth + sparewidth;
  178. var maxheight = screen.WorkingArea.Height - 0;
  179. Height = desiredheight > maxheight ? maxheight : desiredheight;
  180. var maxwidth = screen.WorkingArea.Width - 0;
  181. Width = desiredwidth > maxwidth ? maxwidth : desiredwidth;
  182. var scaption = Form.Items[0].GetType().GetCaption();
  183. Title = "Edit " + scaption.SplitCamelCase();
  184. }
  185. #region ISubPanel
  186. public event ISubPanel.ClosedEvent? SubPanelClosed;
  187. void ISubPanel.Shutdown(CancelEventArgs? cancel)
  188. {
  189. if (bChanged)
  190. {
  191. this.Focus();
  192. var window = cancel is null
  193. ? MessageWindow.NewYesNo($"You have unsaved changes: do you wish to save this {CoreUtils.Neatify(Form.Items[0].GetType().GetCaption())}?", "Save changes?")
  194. : MessageWindow.NewYesNoCancel($"You have unsaved changes: do you wish to save this {CoreUtils.Neatify(Form.Items[0].GetType().GetCaption())}?", "Save changes?");
  195. var result = window.Display().Result;
  196. switch (result)
  197. {
  198. case MessageWindowResult.Yes:
  199. Result = true;
  200. Close();
  201. break;
  202. case MessageWindowResult.No:
  203. Result = false;
  204. Close();
  205. break;
  206. case MessageWindowResult.Cancel:
  207. if(cancel is not null)
  208. {
  209. cancel.Cancel = true;
  210. }
  211. return;
  212. }
  213. }
  214. else
  215. {
  216. Close();
  217. }
  218. }
  219. #endregion
  220. }