DynamicGridColumnNameSelectorWindow.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using InABox.Core;
  2. using InABox.WPF;
  3. using Syncfusion.Pdf.Parsing;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Runtime.CompilerServices;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Shapes;
  20. namespace InABox.DynamicGrid;
  21. public class DynamicGridColumnNameSelectorGrid : DynamicItemsListGrid<DynamicGridColumnNameSelectorItem>, INotifyPropertyChanged
  22. {
  23. private bool _canSave;
  24. public bool CanSave
  25. {
  26. get => _canSave;
  27. set
  28. {
  29. _canSave = value;
  30. DoPropertyChanged();
  31. }
  32. }
  33. private List<DynamicGridColumnNameSelectorItem> _items;
  34. public string SearchText { get; set; }
  35. public DynamicGridColumnNameSelectorGrid(Type type, string[] columnNames)
  36. {
  37. var items = new List<DynamicGridColumnNameSelectorItem>();
  38. var parentCols = new HashSet<string>();
  39. foreach (var column in columnNames)
  40. {
  41. var props = column.Split('.');
  42. string? parent = null;
  43. for (int i = 0; i < props.Length - 1; ++i)
  44. {
  45. if (parent is null)
  46. {
  47. parent = props[i];
  48. }
  49. else
  50. {
  51. parent = $"{parent}.{props[i]}";
  52. }
  53. parentCols.Add(parent);
  54. }
  55. var item = new DynamicGridColumnNameSelectorItem
  56. {
  57. ColumnName = column,
  58. ParentColumn = parent,
  59. Display = props[^1],
  60. IsParent = false,
  61. Comment = DatabaseSchema.Property(type, column)?.Comment ?? ""
  62. };
  63. items.Add(item);
  64. }
  65. foreach (var col in parentCols)
  66. {
  67. var lastColIdx = col.LastIndexOf('.');
  68. var item = new DynamicGridColumnNameSelectorItem
  69. {
  70. ColumnName = col,
  71. IsParent = true,
  72. Comment = DatabaseSchema.Property(type, col)?.Comment ?? ""
  73. };
  74. if (lastColIdx == -1)
  75. {
  76. item.ParentColumn = null;
  77. item.Display = col;
  78. }
  79. else
  80. {
  81. item.ParentColumn = col[..lastColIdx];
  82. item.Display = col[(lastColIdx + 1)..];
  83. }
  84. items.Add(item);
  85. }
  86. items.Sort((a, b) => a.ColumnName.CompareTo(b.ColumnName));
  87. _items = items;
  88. }
  89. protected override void DoReconfigure(DynamicGridOptions options)
  90. {
  91. base.DoReconfigure(options);
  92. options.Clear();
  93. options.FilterRows = true;
  94. }
  95. protected override void SelectItems(CoreRow[]? rows)
  96. {
  97. base.SelectItems(rows);
  98. CanSave = rows is not null && rows.Any(x => !LoadItem(x).IsParent);
  99. if(rows is not null)
  100. {
  101. foreach(var row in rows)
  102. {
  103. Component.ExpandRow(row);
  104. }
  105. }
  106. }
  107. protected override DynamicGridColumns LoadColumns()
  108. {
  109. var columns = new DynamicGridColumns<DynamicGridColumnNameSelectorItem>();
  110. columns.Add(x => x.Display, caption: "Name", width: 250);
  111. columns.Add(x => x.Comment);
  112. return columns;
  113. }
  114. private UIComponent? _uiComponent;
  115. private UIComponent Component
  116. {
  117. get
  118. {
  119. _uiComponent ??= new UIComponent(this);
  120. return _uiComponent;
  121. }
  122. }
  123. private class UIComponent : DynamicGridTreeUIComponent<DynamicGridColumnNameSelectorItem, string?>
  124. {
  125. DynamicGridColumnNameSelectorGrid Grid;
  126. public UIComponent(DynamicGridColumnNameSelectorGrid grid) : base(x => x.ColumnName, x => x.ParentColumn, null)
  127. {
  128. Parent = grid;
  129. Grid = grid;
  130. ExpandMode = DynamicTreeGridExpandMode.None;
  131. ShowHeader = false;
  132. MinRowHeight = 25;
  133. GridLines = DynamicTreeGridLines.Horizontal;
  134. }
  135. protected override Brush? GetCellForeground(CoreRow row, DynamicColumnBase column)
  136. {
  137. if(column is DynamicGridColumn gc && gc.ColumnName == nameof(DynamicGridColumnNameSelectorItem.Comment))
  138. {
  139. return Colors.Gray.ToBrush();
  140. }
  141. else
  142. {
  143. var item = Grid.LoadItem(row);
  144. if (item.IsParent)
  145. {
  146. return Colors.Gray.ToBrush();
  147. }
  148. else
  149. {
  150. return base.GetCellForeground(row, column);
  151. }
  152. }
  153. }
  154. protected override FontStyle? GetCellFontStyle(CoreRow row, DynamicColumnBase column)
  155. {
  156. if(column is DynamicGridColumn gc && gc.ColumnName == nameof(DynamicGridColumnNameSelectorItem.Comment))
  157. {
  158. return FontStyles.Italic;
  159. }
  160. else
  161. {
  162. var item = Grid.LoadItem(row);
  163. if (item.IsParent)
  164. {
  165. return FontStyles.Italic;
  166. }
  167. else
  168. {
  169. return base.GetCellFontStyle(row, column);
  170. }
  171. }
  172. }
  173. }
  174. protected override IDynamicGridUIComponent<DynamicGridColumnNameSelectorItem> CreateUIComponent() => Component;
  175. public event PropertyChangedEventHandler? PropertyChanged;
  176. protected void DoPropertyChanged([CallerMemberName] string propertyName = "")
  177. {
  178. PropertyChanged?.Invoke(this, new(propertyName));
  179. }
  180. protected override void Reload(Filters<DynamicGridColumnNameSelectorItem> criteria, Columns<DynamicGridColumnNameSelectorItem> columns, ref SortOrder<DynamicGridColumnNameSelectorItem>? sort, CancellationToken token, Action<CoreTable?, Exception?> action)
  181. {
  182. if (SearchText.IsNullOrWhiteSpace())
  183. {
  184. Items = _items;
  185. }
  186. else
  187. {
  188. Items = new();
  189. foreach(var item in _items)
  190. {
  191. if (!item.IsParent && item.ColumnName.Contains(SearchText, StringComparison.CurrentCultureIgnoreCase))
  192. {
  193. Items.Add(item);
  194. }
  195. }
  196. foreach(var item in _items)
  197. {
  198. if (item.IsParent && Items.Any(x => x.ColumnName.StartsWith(item.ColumnName + ".")))
  199. {
  200. Items.Add(item);
  201. }
  202. }
  203. }
  204. base.Reload(criteria, columns, ref sort, token, action);
  205. }
  206. public static bool SelectColumnName(Type type, string[] columnNames, out string value)
  207. {
  208. var grid = new DynamicGridColumnNameSelectorGrid(type, columnNames)
  209. {
  210. };
  211. grid.Refresh(true, true);
  212. var lbl = new Label
  213. {
  214. Content = "Search:",
  215. Margin = new Thickness(0, 0, 5, 5)
  216. };
  217. var search = new TextBox
  218. {
  219. Background = Colors.LightYellow.ToBrush(),
  220. Height = 25,
  221. Margin = new Thickness(0, 0, 0, 5),
  222. VerticalContentAlignment = VerticalAlignment.Center,
  223. };
  224. search.TextChanged += (o, e) =>
  225. {
  226. grid.SearchText = search.Text;
  227. grid.Refresh(false, true);
  228. };
  229. var control = new Grid();
  230. control.AddColumn(GridUnitType.Auto);
  231. control.AddColumn(GridUnitType.Star);
  232. control.AddRow(GridUnitType.Auto);
  233. control.AddRow(GridUnitType.Star);
  234. control.AddChild(lbl, 0, 0);
  235. control.AddChild(search, 0, 1);
  236. control.AddChild(grid, 1, 0, colSpan: 2);
  237. var window = new DynamicContentDialog(control)
  238. {
  239. Title = "Select Column",
  240. Width = 600,
  241. Height = 600,
  242. WindowStartupLocation = WindowStartupLocation.CenterScreen
  243. };
  244. window.Bind(DynamicContentDialog.CanSaveProperty, grid, x => x.CanSave);
  245. grid.OnCellDoubleClick += (o, e) =>
  246. {
  247. if (e.Row is null) return;
  248. var item = grid.LoadItem(e.Row);
  249. if (!item.IsParent)
  250. {
  251. window.DialogResult = true;
  252. }
  253. };
  254. if(window.ShowDialog() == true && grid.SelectedRows.FirstOrDefault() is CoreRow row)
  255. {
  256. var item = grid.LoadItem(row);
  257. if (!item.IsParent)
  258. {
  259. value = item.ColumnName;
  260. return true;
  261. }
  262. }
  263. value = "";
  264. return false;
  265. }
  266. }
  267. public class DynamicGridColumnNameSelectorItem : BaseObject
  268. {
  269. public string ColumnName { get; set; } = "";
  270. public string Display { get; set; } = "";
  271. public string? ParentColumn { get; set; }
  272. public string Comment { get; set; } = "";
  273. public bool IsParent { get; set; }
  274. }