MultiSelectDialog.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using InABox.Clients;
  9. using InABox.Configuration;
  10. using InABox.Core;
  11. using InABox.Wpf;
  12. using Syncfusion.Data;
  13. using Button = System.Windows.Controls.Button;
  14. namespace InABox.DynamicGrid;
  15. public interface IMultiSelectDialog
  16. {
  17. bool ShowDialog(String? column = null, String? filter = null, FilterType filtertype = FilterType.Contains);
  18. Guid[] IDs();
  19. CoreTable Data();
  20. Entity[] Items(IColumns? columns = null);
  21. bool CanAdd { get; set; }
  22. }
  23. public class MultiSelectDialogSettings : BaseObject, IUserConfigurationSettings
  24. {
  25. public DynamicGridSelectedFilterSettings Filters { get; set; } = new();
  26. }
  27. /// <summary>
  28. /// Represents a dialog to select <typeparamref name="T"/>(s), given a filter and a set of columns. It can either do multi-selecting or single-selecting.
  29. /// </summary>
  30. /// <remarks>
  31. /// This is the standard way to do a selection dialog. To access all selected IDs, use <see cref="IDs"/>; to access the data according to the columns
  32. /// provided in the constructor, use <see cref="Data"/>; use <see cref="Items"/> if you want to load all the items with the selected IDs with a custom
  33. /// set of columns.
  34. /// </remarks>
  35. public class MultiSelectDialog<T> : IMultiSelectDialog where T : Entity, IRemotable, IPersistent, new()
  36. {
  37. //private Expression<Func<T, object>>[] _columns = new Expression<Func<T, object>>[] { };
  38. private readonly Columns<T>? _columns;
  39. private readonly Filter<T>? _filter;
  40. private readonly DynamicDataGrid<T> datagrid;
  41. //private readonly Grid grid;
  42. private readonly Button CancelButton;
  43. private readonly Button OKButton;
  44. private ThemableWindow? window;
  45. public bool CanAdd { get; set; } = true;
  46. public MultiSelectDialog(Filter<T>? filter, Columns<T>? columns, bool multiselect = true)
  47. {
  48. _filter = filter;
  49. //grid = new Grid();
  50. //grid.Margin = new Thickness(5);
  51. //grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1.0F, GridUnitType.Star) });
  52. //grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(40.0F, GridUnitType.Pixel) });
  53. //grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(80.0F, GridUnitType.Pixel) });
  54. //grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Star) });
  55. //grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(80.0F, GridUnitType.Pixel) });
  56. //grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(80.0F, GridUnitType.Pixel) });
  57. datagrid = new DynamicDataGrid<T>();
  58. datagrid.Margin = new Thickness(5);
  59. datagrid.Reconfigure(options =>
  60. {
  61. options.BeginUpdate();
  62. options.Clear();
  63. options.SelectColumns = true;
  64. options.FilterRows = true;
  65. if (multiselect)
  66. options.MultiSelect = true;
  67. options.AddRows = CanAdd;
  68. options.RecordCount = true;
  69. options.EndUpdate();
  70. });
  71. datagrid.Reconfigure();
  72. OKButton = datagrid.AddButton("OK", null, OKClick, DynamicGridButtonPosition.Right);
  73. OKButton.Width = 80;
  74. OKButton.IsEnabled = false;
  75. CancelButton = datagrid.AddButton("Cancel", null, CancelClick, DynamicGridButtonPosition.Right);
  76. CancelButton.Width = 80;
  77. datagrid.OnReload += Grid_OnReload;
  78. datagrid.OnDoubleClick += Grid_DoubleClick;
  79. datagrid.ColumnsTag = "MSD";
  80. if (columns != null)
  81. {
  82. _columns = columns;
  83. foreach (var column in columns)
  84. datagrid.AddHiddenColumn(column.Property);
  85. //datagrid.HiddenColumns.AddRange(columns);
  86. }
  87. else
  88. {
  89. var cols = LookupFactory.DefineColumns<T>()
  90. .AddColumns(ColumnTypeFlags.IncludeOptional | ColumnTypeFlags.IncludeForeignKeys | ColumnTypeFlags.IncludeLinked);
  91. foreach (var col in cols)
  92. datagrid.AddHiddenColumn(col.ToString());
  93. }
  94. var _settings = new UserConfiguration<MultiSelectDialogSettings>(datagrid.GetTag()).Load();
  95. datagrid.FilterComponent.SetSettings(_settings.Filters,false);
  96. datagrid.FilterComponent.OnFiltersSelected += (settings) =>
  97. {
  98. var _settings = new MultiSelectDialogSettings() { Filters = settings };
  99. new UserConfiguration<MultiSelectDialogSettings>(datagrid.GetTag()).Save(_settings);
  100. };
  101. datagrid.SetValue(Grid.RowProperty, 0);
  102. datagrid.SetValue(Grid.ColumnProperty, 0);
  103. datagrid.SetValue(Grid.ColumnSpanProperty, 4);
  104. datagrid.OnSelectItem += Datagrid_OnSelectItem;
  105. //grid.Children.Add(datagrid);
  106. // ClearButton = new Button();
  107. // ClearButton.Margin = new Thickness(0, 5, 5, 0);
  108. // ClearButton.Content = "Clear";
  109. // ClearButton.Click += ClearButton_Click;
  110. // ClearButton.SetValue(Grid.RowProperty, 1);
  111. // ClearButton.SetValue(Grid.ColumnProperty, 0);
  112. // grid.Children.Add(ClearButton);
  113. //
  114. // OKButton = new Button();
  115. // OKButton.Margin = new Thickness(5, 5, 0, 0);
  116. // OKButton.Content = "OK";
  117. // OKButton.Click += OKButton_Click;
  118. // OKButton.SetValue(Grid.RowProperty, 1);
  119. // OKButton.SetValue(Grid.ColumnProperty, 2);
  120. // OKButton.IsEnabled = false;
  121. // grid.Children.Add(OKButton);
  122. //
  123. // var CancelButton = new Button();
  124. // CancelButton.Margin = new Thickness(5, 5, 0, 0);
  125. // CancelButton.Content = "Cancel";
  126. // CancelButton.Click += CancelButton_Click;
  127. // CancelButton.SetValue(Grid.RowProperty, 1);
  128. // CancelButton.SetValue(Grid.ColumnProperty, 3);
  129. // grid.Children.Add(CancelButton);
  130. }
  131. private bool OKClick(Button button, CoreRow[] rows)
  132. {
  133. window!.DialogResult = true;
  134. window!.Close();
  135. return false;
  136. }
  137. private bool CancelClick(Button button, CoreRow[] rows)
  138. {
  139. window!.DialogResult = false;
  140. window!.Close();
  141. return false;
  142. }
  143. public bool ShowDialog(string? column = null, string? value = null, FilterType filtertype = FilterType.Contains) =>
  144. ShowDialogInternal(null, column, value, filtertype);
  145. public bool ShowDialog(string title) =>
  146. ShowDialogInternal(title, null, null, default);
  147. private bool ShowDialogInternal(string? title, string? column, string? value, FilterType filtertype)
  148. {
  149. window = new ThemableWindow
  150. {
  151. Title = title ?? "Select Items",
  152. WindowStyle = WindowStyle.SingleBorderWindow,
  153. Content = datagrid
  154. };
  155. datagrid.Refresh(true, true);
  156. if (!column.IsNullOrWhiteSpace() && !value.IsNullOrWhiteSpace())
  157. datagrid.AddVisualFilter(column, value, filtertype);
  158. if (window.ShowDialog() == true)
  159. return true;
  160. return false;
  161. }
  162. private Window GetWindow([CallerMemberName] string methodName = "")
  163. {
  164. return window ?? throw new Exception($"Must call ShowDialog() before {methodName}()");
  165. }
  166. public Guid[] IDs()
  167. {
  168. var window = GetWindow();
  169. if (window.DialogResult == true)
  170. {
  171. return datagrid.SelectedRows.ToArray(r => r.Get<T, Guid>(x => x.ID));
  172. }
  173. else if (window.DialogResult == false)
  174. {
  175. return [Guid.Empty];
  176. }
  177. else
  178. {
  179. return Array.Empty<Guid>();
  180. }
  181. }
  182. public CoreTable Data()
  183. {
  184. var window = GetWindow();
  185. var result = new CoreTable();
  186. result.Columns.Add(new CoreColumn { ColumnName = "ID", DataType = typeof(Guid) });
  187. if(_columns is not null)
  188. {
  189. foreach (var column in _columns.Where(x => !string.Equals(x.Property, "ID")))
  190. result.Columns.Add(new CoreColumn { ColumnName = column.Property, DataType = column.Type });
  191. }
  192. if (window.DialogResult == true)
  193. {
  194. if (datagrid?.Data != null && datagrid.SelectedRows.Length != 0)
  195. foreach (var sel in datagrid.SelectedRows)
  196. {
  197. var row = result.NewRow();
  198. foreach (var column in result.Columns)
  199. {
  200. var value = sel[column.ColumnName];
  201. row.Set(column.ColumnName, value);
  202. }
  203. result.Rows.Add(row);
  204. }
  205. }
  206. else if (window.DialogResult == false)
  207. {
  208. var row = result.NewRow(true);
  209. result.Rows.Add(row);
  210. }
  211. return result;
  212. }
  213. public T[] Items(Columns<T>? columns = null)
  214. {
  215. var window = GetWindow();
  216. if (window.DialogResult == true)
  217. {
  218. var ids = datagrid.SelectedRows.ToArray(r => r.Get<T, Guid>(x => x.ID));
  219. if (ids.Length > 0)
  220. {
  221. return new Client<T>().Query(new Filter<T>(x => x.ID).InList(ids), columns).ToArray<T>();
  222. }
  223. }
  224. else if (window.DialogResult == false)
  225. return [new T()];
  226. return [];
  227. }
  228. Entity[] IMultiSelectDialog.Items(IColumns? columns) => Items(columns as Columns<T>);
  229. private void Grid_DoubleClick(object sender, HandledEventArgs args)
  230. {
  231. args.Handled = true;
  232. window!.DialogResult = true;
  233. window.Close();
  234. }
  235. private void Datagrid_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  236. {
  237. OKButton.IsEnabled = e.Rows?.Any() == true;
  238. }
  239. // private void CancelButton_Click(object sender, RoutedEventArgs e)
  240. // {
  241. // window!.DialogResult = false;
  242. // window!.Close();
  243. // }
  244. //
  245. // private void OKButton_Click(object sender, RoutedEventArgs e)
  246. // {
  247. // window!.DialogResult = true;
  248. // window.Close();
  249. // }
  250. //
  251. // private void ClearButton_Click(object sender, RoutedEventArgs e)
  252. // {
  253. // window!.DialogResult = false;
  254. // window.Close();
  255. // }
  256. private void Grid_OnReload(object sender, Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sortby)
  257. {
  258. if (_filter != null)
  259. criteria.Add(_filter);
  260. }
  261. public static bool SelectItem([NotNullWhen(true)] out T? item, Filter<T>? filter = null, Columns<T>? columns = null, string? title = null)
  262. {
  263. var dlg = new MultiSelectDialog<T>(filter, columns, multiselect: false);
  264. if (dlg.ShowDialogInternal(title, null, null, default))
  265. {
  266. item = dlg.Data().ToObjects<T>().FirstOrDefault();
  267. return item is not null;
  268. }
  269. else
  270. {
  271. item = null;
  272. return false;
  273. }
  274. }
  275. public static bool SelectItems([NotNullWhen(true)] out T[]? items, Filter<T>? filter = null, Columns<T>? columns = null, string? title = null)
  276. {
  277. var dlg = new MultiSelectDialog<T>(filter, columns, multiselect: true);
  278. if (dlg.ShowDialogInternal(title, null, null, default))
  279. {
  280. items = dlg.Data().ToArray<T>();
  281. return true;
  282. }
  283. else
  284. {
  285. items = null;
  286. return false;
  287. }
  288. }
  289. }
  290. public static class MultiSelectDialog
  291. {
  292. public static bool SelectItem<T>([NotNullWhen(true)] out T? item, Filter<T>? filter = null, Columns<T>? columns = null, string? title = null)
  293. where T : Entity, IRemotable, IPersistent, new()
  294. {
  295. return MultiSelectDialog<T>.SelectItem(out item, filter: filter, columns: columns, title: title);
  296. }
  297. public static bool SelectItems<T>([NotNullWhen(true)] out T[]? items, Filter<T>? filter = null, Columns<T>? columns = null, string? title = null)
  298. where T : Entity, IRemotable, IPersistent, new()
  299. {
  300. return MultiSelectDialog<T>.SelectItems(out items, filter: filter, columns: columns, title: title);
  301. }
  302. }