MultiSelectDialog.cs 11 KB

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