DynamicGridColumnNameSelectorWindow.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 bool _onlyVisible;
  34. public bool OnlyVisible
  35. {
  36. get => _onlyVisible;
  37. set
  38. {
  39. _onlyVisible = value;
  40. Refresh(false, true);
  41. DoPropertyChanged();
  42. }
  43. }
  44. private List<DynamicGridColumnNameSelectorItem> _items;
  45. public string SearchText { get; set; } = "";
  46. public DynamicGridColumnNameSelectorGrid(IDynamicGridColumnSchema schema)
  47. {
  48. var itemMap = new Dictionary<string, DynamicGridColumnNameSelectorItem>();
  49. var items = new List<DynamicGridColumnNameSelectorItem>();
  50. var parentCols = new Dictionary<string, List<DynamicGridColumnNameSelectorItem>>();
  51. foreach (var column in schema.ColumnNames)
  52. {
  53. var item = new DynamicGridColumnNameSelectorItem();
  54. var props = column.Split('.');
  55. string? parent = null;
  56. for (int i = 0; i < props.Length - 1; ++i)
  57. {
  58. if (parent is null)
  59. {
  60. parent = props[i];
  61. }
  62. else
  63. {
  64. parent = $"{parent}.{props[i]}";
  65. }
  66. parentCols.GetValueOrAdd(parent).Add(item);
  67. }
  68. item.ColumnName = column;
  69. item.ParentColumn = parent;
  70. item.Display = props[^1];
  71. item.IsParent = false;
  72. item.Comment = schema.GetComment(column) ?? "";
  73. item.IsVisible = schema.IsVisible(column);
  74. items.Add(item);
  75. }
  76. foreach (var (col, children) in parentCols)
  77. {
  78. var lastColIdx = col.LastIndexOf('.');
  79. var item = new DynamicGridColumnNameSelectorItem
  80. {
  81. ColumnName = col,
  82. IsParent = true,
  83. Comment = schema.GetComment(col) ?? "",
  84. IsVisible = children.Any(x => x.IsVisible)
  85. };
  86. if (lastColIdx == -1)
  87. {
  88. item.ParentColumn = null;
  89. item.Display = col;
  90. }
  91. else
  92. {
  93. item.ParentColumn = col[..lastColIdx];
  94. item.Display = col[(lastColIdx + 1)..];
  95. }
  96. items.Add(item);
  97. }
  98. items.Sort((a, b) => a.ColumnName.CompareTo(b.ColumnName));
  99. _items = items;
  100. }
  101. protected override void DoReconfigure(DynamicGridOptions options)
  102. {
  103. base.DoReconfigure(options);
  104. options.Clear();
  105. options.FilterRows = true;
  106. }
  107. protected override void SelectItems(CoreRow[]? rows)
  108. {
  109. base.SelectItems(rows);
  110. CanSave = rows is not null && rows.Any(x => !LoadItem(x).IsParent);
  111. if(rows is not null)
  112. {
  113. foreach(var row in rows)
  114. {
  115. Component.ExpandRow(row);
  116. }
  117. }
  118. }
  119. protected override DynamicGridColumns LoadColumns()
  120. {
  121. var columns = new DynamicGridColumns<DynamicGridColumnNameSelectorItem>();
  122. columns.Add(x => x.Display, caption: "Name", width: 250);
  123. columns.Add(x => x.Comment);
  124. return columns;
  125. }
  126. private UIComponent? _uiComponent;
  127. private UIComponent Component
  128. {
  129. get
  130. {
  131. _uiComponent ??= new UIComponent(this);
  132. return _uiComponent;
  133. }
  134. }
  135. private class UIComponent : DynamicGridTreeUIComponent<DynamicGridColumnNameSelectorItem, string?>
  136. {
  137. DynamicGridColumnNameSelectorGrid Grid;
  138. public UIComponent(DynamicGridColumnNameSelectorGrid grid) : base(x => x.ColumnName, x => x.ParentColumn, null)
  139. {
  140. Parent = grid;
  141. Grid = grid;
  142. ExpandMode = DynamicTreeGridExpandMode.None;
  143. ShowHeader = false;
  144. MinRowHeight = 25;
  145. GridLines = DynamicTreeGridLines.Horizontal;
  146. }
  147. protected override Brush? GetCellForeground(CoreRow row, DynamicColumnBase column)
  148. {
  149. if(column is DynamicGridColumn gc && gc.ColumnName == nameof(DynamicGridColumnNameSelectorItem.Comment))
  150. {
  151. return Colors.Gray.ToBrush();
  152. }
  153. else
  154. {
  155. var item = Grid.LoadItem(row);
  156. if (item.IsParent)
  157. {
  158. return Colors.Gray.ToBrush();
  159. }
  160. else
  161. {
  162. return base.GetCellForeground(row, column);
  163. }
  164. }
  165. }
  166. protected override FontStyle? GetCellFontStyle(CoreRow row, DynamicColumnBase column)
  167. {
  168. if(column is DynamicGridColumn gc && gc.ColumnName == nameof(DynamicGridColumnNameSelectorItem.Comment))
  169. {
  170. return FontStyles.Italic;
  171. }
  172. else
  173. {
  174. var item = Grid.LoadItem(row);
  175. if (item.IsParent)
  176. {
  177. return FontStyles.Italic;
  178. }
  179. else
  180. {
  181. return base.GetCellFontStyle(row, column);
  182. }
  183. }
  184. }
  185. }
  186. protected override IDynamicGridUIComponent<DynamicGridColumnNameSelectorItem> CreateUIComponent() => Component;
  187. public event PropertyChangedEventHandler? PropertyChanged;
  188. protected void DoPropertyChanged([CallerMemberName] string propertyName = "")
  189. {
  190. PropertyChanged?.Invoke(this, new(propertyName));
  191. }
  192. protected override void Reload(Filters<DynamicGridColumnNameSelectorItem> criteria, Columns<DynamicGridColumnNameSelectorItem> columns, ref SortOrder<DynamicGridColumnNameSelectorItem>? sort, CancellationToken token, Action<CoreTable?, Exception?> action)
  193. {
  194. if (SearchText.IsNullOrWhiteSpace())
  195. {
  196. Items = _items.ToList();
  197. }
  198. else
  199. {
  200. Items = new();
  201. foreach(var item in _items)
  202. {
  203. if (!item.IsParent && item.ColumnName.Contains(SearchText, StringComparison.CurrentCultureIgnoreCase))
  204. {
  205. Items.Add(item);
  206. }
  207. }
  208. foreach(var item in _items)
  209. {
  210. if (item.IsParent && Items.Any(x => x.ColumnName.StartsWith(item.ColumnName + ".")))
  211. {
  212. Items.Add(item);
  213. }
  214. }
  215. }
  216. if (OnlyVisible)
  217. {
  218. Items.RemoveAll(x => !x.IsVisible);
  219. }
  220. base.Reload(criteria, columns, ref sort, token, action);
  221. }
  222. public static bool SelectColumnName(Type type, IEnumerable<string> columnNames, out string value, bool showVisibilityButton = false)
  223. {
  224. return SelectColumnName(new DynamicGridObjectColumnSchema(type, columnNames), out value, showVisibilityButton: showVisibilityButton);
  225. }
  226. public static bool SelectColumnName(IDynamicGridColumnSchema schema, out string value, bool showVisibilityButton = false)
  227. {
  228. var grid = new DynamicGridColumnNameSelectorGrid(schema)
  229. {
  230. OnlyVisible = showVisibilityButton
  231. };
  232. grid.Refresh(true, true);
  233. var lbl = new Label
  234. {
  235. Content = "Search:",
  236. Margin = new Thickness(0, 0, 5, 5)
  237. };
  238. var search = new TextBox
  239. {
  240. Background = Colors.LightYellow.ToBrush(),
  241. Height = 25,
  242. Margin = new Thickness(0, 0, 0, 5),
  243. VerticalContentAlignment = VerticalAlignment.Center,
  244. };
  245. search.TextChanged += (o, e) =>
  246. {
  247. grid.SearchText = search.Text;
  248. grid.Refresh(false, true);
  249. };
  250. var onlyVisible = new CheckBox()
  251. {
  252. Content = "Only Visible?",
  253. VerticalAlignment = VerticalAlignment.Center,
  254. Margin = new(5, 0, 0, 5)
  255. };
  256. onlyVisible.Bind(CheckBox.IsCheckedProperty, grid, x => x.OnlyVisible);
  257. var control = new Grid();
  258. control.AddColumn(GridUnitType.Auto);
  259. control.AddColumn(GridUnitType.Star);
  260. if (showVisibilityButton)
  261. {
  262. control.AddColumn(GridUnitType.Auto);
  263. }
  264. else
  265. {
  266. control.AddColumn(0);
  267. }
  268. control.AddRow(GridUnitType.Auto);
  269. control.AddRow(GridUnitType.Star);
  270. control.AddChild(lbl, 0, 0);
  271. control.AddChild(search, 0, 1);
  272. control.AddChild(onlyVisible, 0, 2);
  273. control.AddChild(grid, 1, 0, colSpan: 3);
  274. var window = new DynamicContentDialog(control)
  275. {
  276. Title = "Select Column",
  277. Width = 600,
  278. Height = 600,
  279. WindowStartupLocation = WindowStartupLocation.CenterScreen
  280. };
  281. window.Bind(DynamicContentDialog.CanSaveProperty, grid, x => x.CanSave);
  282. grid.OnCellDoubleClick += (o, e) =>
  283. {
  284. if (e.Row is null) return;
  285. var item = grid.LoadItem(e.Row);
  286. if (!item.IsParent)
  287. {
  288. window.DialogResult = true;
  289. }
  290. };
  291. if(window.ShowDialog() == true && grid.SelectedRows.FirstOrDefault() is CoreRow row)
  292. {
  293. var item = grid.LoadItem(row);
  294. if (!item.IsParent)
  295. {
  296. value = item.ColumnName;
  297. return true;
  298. }
  299. }
  300. value = "";
  301. return false;
  302. }
  303. }
  304. public class DynamicGridColumnNameSelectorItem : BaseObject
  305. {
  306. public string ColumnName { get; set; } = "";
  307. public string Display { get; set; } = "";
  308. public string? ParentColumn { get; set; }
  309. public string Comment { get; set; } = "";
  310. public bool IsVisible { get; set; }
  311. public bool IsParent { get; set; }
  312. }