MultiSelectDialog.cs 11 KB

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