DynamicEditorForm.xaml.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. // Providing new implementation to avoid using DIalogResult, which breaks if non-modal.
  103. public new bool? ShowDialog()
  104. {
  105. base.ShowDialog();
  106. return Result;
  107. }
  108. private void Form_OnCancel()
  109. {
  110. if (bChanged)
  111. {
  112. var result = MessageWindow.ShowYesNoCancel("Save Changes?", "Confirm");
  113. switch (result)
  114. {
  115. case MessageWindowResult.Yes:
  116. Result = true;
  117. Close();
  118. break;
  119. case MessageWindowResult.No:
  120. Result = false;
  121. Close();
  122. break;
  123. }
  124. }
  125. else
  126. {
  127. Result = false;
  128. Close();
  129. }
  130. }
  131. private void Form_OnOK()
  132. {
  133. Result = true;
  134. Close();
  135. }
  136. private void Editor_OnEditorCreated(object sender, double height, double width)
  137. {
  138. }
  139. private void Window_Closing(object sender, CancelEventArgs e)
  140. {
  141. if (bChanged && Result == null)
  142. {
  143. var result = MessageWindow.ShowYesNoCancel("Save Changes?", "Confirm");
  144. switch (result)
  145. {
  146. case MessageWindowResult.Yes:
  147. Result = true;
  148. break;
  149. case MessageWindowResult.No:
  150. Result = false;
  151. break;
  152. case MessageWindowResult.Cancel:
  153. e.Cancel = true;
  154. return;
  155. }
  156. }
  157. if (Result == true)
  158. Form.SaveItem(e);
  159. SubPanelClosed?.Invoke(this);
  160. }
  161. private bool bChanged = false;
  162. private void Form_OnOnChanged(object? sender, EventArgs e)
  163. {
  164. bChanged = true;
  165. }
  166. private void Form_OnReload(IDynamicEditorForm form)
  167. {
  168. SetSize();
  169. }
  170. private void SetSize()
  171. {
  172. var screen = WpfScreen.GetScreenFrom(new Point(Left, Top));
  173. double spareheight = 90;
  174. double sparewidth = 25;
  175. var desiredheight = Form.ContentHeight + spareheight;
  176. var desiredwidth = Form.ContentWidth + sparewidth;
  177. var maxheight = screen.WorkingArea.Height - 0;
  178. Height = desiredheight > maxheight ? maxheight : desiredheight;
  179. var maxwidth = screen.WorkingArea.Width - 0;
  180. Width = desiredwidth > maxwidth ? maxwidth : desiredwidth;
  181. var scaption = Form.Items[0].GetType().GetCaption();
  182. Title = "Edit " + scaption.SplitCamelCase();
  183. }
  184. #region ISubPanel
  185. public event ISubPanel.ClosedEvent? SubPanelClosed;
  186. void ISubPanel.Shutdown(CancelEventArgs? cancel)
  187. {
  188. if (bChanged)
  189. {
  190. this.Focus();
  191. var window = cancel is null
  192. ? MessageWindow.NewYesNo($"You have unsaved changes: do you wish to save this {CoreUtils.Neatify(Form.Items[0].GetType().GetCaption())}?", "Save changes?")
  193. : MessageWindow.NewYesNoCancel($"You have unsaved changes: do you wish to save this {CoreUtils.Neatify(Form.Items[0].GetType().GetCaption())}?", "Save changes?");
  194. var result = window.Display().Result;
  195. switch (result)
  196. {
  197. case MessageWindowResult.Yes:
  198. Result = true;
  199. Close();
  200. break;
  201. case MessageWindowResult.No:
  202. Result = false;
  203. Close();
  204. break;
  205. case MessageWindowResult.Cancel:
  206. if(cancel is not null)
  207. {
  208. cancel.Cancel = true;
  209. }
  210. return;
  211. }
  212. }
  213. else
  214. {
  215. Close();
  216. }
  217. }
  218. #endregion
  219. }