DynamicTreeView.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using InABox.Core;
  13. using InABox.WPF;
  14. using NPOI.OpenXmlFormats.Dml.Chart;
  15. using Syncfusion.UI.Xaml.TreeGrid;
  16. using Syncfusion.Windows.Tools.Controls;
  17. namespace InABox.DynamicGrid
  18. {
  19. public class DynamicTreeNode : INotifyPropertyChanged
  20. {
  21. private DynamicTreeNodes _owner;
  22. public ObservableCollection<DynamicTreeNode> Children => _owner.GetChilden(_id);
  23. private Guid _id;
  24. public Guid ID
  25. {
  26. get { return _id; }
  27. set
  28. {
  29. _id = value;
  30. RaisedOnPropertyChanged("ID");
  31. }
  32. }
  33. private Guid _parent;
  34. public Guid Parent
  35. {
  36. get { return _parent; }
  37. set
  38. {
  39. _parent = value;
  40. RaisedOnPropertyChanged("Parent");
  41. }
  42. }
  43. private string _description;
  44. public string Description
  45. {
  46. get { return _description; }
  47. set
  48. {
  49. _description = value;
  50. RaisedOnPropertyChanged("Description");
  51. }
  52. }
  53. private ImageSource? _image;
  54. public ImageSource? Image
  55. {
  56. get { return _image; }
  57. set
  58. {
  59. _image = value;
  60. RaisedOnPropertyChanged("Image");
  61. }
  62. }
  63. public event PropertyChangedEventHandler? PropertyChanged;
  64. public void RaisedOnPropertyChanged(string propertyName)
  65. {
  66. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  67. }
  68. public DynamicTreeNode(DynamicTreeNodes owner)
  69. {
  70. _owner = owner;
  71. _description = "";
  72. }
  73. public DynamicTreeNode(DynamicTreeNodes owner, Guid id, Guid parent) : this(owner)
  74. {
  75. _id = id;
  76. _parent = parent;
  77. }
  78. }
  79. public class DynamicTreeNodes
  80. {
  81. private List<DynamicTreeNode> _nodes;
  82. public ObservableCollection<DynamicTreeNode> Nodes => new ObservableCollection<DynamicTreeNode>(_nodes.Where(x => x.Parent == Guid.Empty));
  83. public DynamicTreeNodes()
  84. {
  85. _nodes = new List<DynamicTreeNode>();
  86. }
  87. public DynamicTreeNode Add(Guid id, Guid parent)
  88. {
  89. var node = new DynamicTreeNode(this, id, parent);
  90. _nodes.Add(node);
  91. return node;
  92. }
  93. public void GetChildren(List<Guid> nodes, Guid id)
  94. {
  95. nodes.Add(id);
  96. var children = GetChilden(id);
  97. foreach (var child in children)
  98. GetChildren(nodes, child.ID);
  99. }
  100. public ObservableCollection<DynamicTreeNode> GetChilden(Guid id)
  101. {
  102. return new ObservableCollection<DynamicTreeNode>(_nodes.Where(x => x.Parent.Equals(id) && (x.ID != id)));
  103. }
  104. public void Load<T>(CoreTable table, Expression<Func<T, Guid>> id, Expression<Func<T, Guid>> parentid, Expression<Func<T, String>> description)
  105. {
  106. _nodes.Clear();
  107. foreach (var row in table.Rows)
  108. {
  109. Guid _id = row.Get<T, Guid>(id);
  110. Guid _parent = row.Get<T, Guid>(parentid);
  111. String _description = row.Get<T, String>(description);
  112. Add(_id, _parent).Description = _description;
  113. }
  114. }
  115. }
  116. public enum DynamicTreeOption
  117. {
  118. Add,
  119. Edit,
  120. Delete
  121. }
  122. public delegate void OnSelectItem(DynamicTreeNode node);
  123. public delegate void OnContextMenuOpening(DynamicTreeNode node, ContextMenu menu);
  124. public abstract class DynamicTreeView<T> : ContentControl where T : BaseObject, new()
  125. {
  126. protected abstract Expression<Func<T, Guid>> ID { get; }
  127. protected abstract Expression<Func<T, Guid>> ParentID { get; }
  128. protected abstract Expression<Func<T, String>> Description { get; }
  129. public CoreTable Data { get; private set; }
  130. private ContextMenu _menu;
  131. private SfTreeGrid _tree;
  132. private DockPanel _dock;
  133. private Grid _grid;
  134. private Button _add;
  135. private Button _edit;
  136. private Button _delete;
  137. private Label _spacer;
  138. public FluentList<DynamicTreeOption> Options { get; private set; }
  139. public event OnSelectItem OnSelectItem;
  140. public event OnContextMenuOpening OnContextMenuOpening;
  141. private double minRowHeight = 30D;
  142. private double maxRowHeight = 30D;
  143. public double MinRowHeight
  144. {
  145. get => minRowHeight;
  146. set
  147. {
  148. minRowHeight = value;
  149. CalculateRowHeight();
  150. }
  151. }
  152. public double MaxRowHeight
  153. {
  154. get => maxRowHeight;
  155. set
  156. {
  157. maxRowHeight = value;
  158. CalculateRowHeight();
  159. }
  160. }
  161. /*public double RowHeight
  162. {
  163. get => _tree.RowHeight;
  164. set => _tree.RowHeight = value;
  165. }*/
  166. public DynamicTreeView() : base()
  167. {
  168. Options = new FluentList<DynamicTreeOption>();
  169. Options.OnChanged += OptionsChanged;
  170. _grid = new Grid();
  171. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1D, GridUnitType.Star) });
  172. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1D, GridUnitType.Star) });
  173. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1D, GridUnitType.Auto) });
  174. _tree = new SfTreeGrid();
  175. _tree.ChildPropertyName = "Children";
  176. //_tree.ParentPropertyName = "Parent";
  177. _tree.AutoGenerateColumns = false;
  178. _tree.AutoExpandMode = AutoExpandMode.AllNodesExpanded;
  179. //_tree.NodeCollapsing += (o, e) => { e.Cancel = true; };
  180. _tree.HeaderRowHeight = 0D;
  181. _tree.SelectionChanged += (o,e) => OnSelectItem?.Invoke((_tree.SelectedItem as DynamicTreeNode)!);
  182. _tree.AllowSelectionOnExpanderClick = false;
  183. _menu = new ContextMenu();
  184. var additem = new MenuItem() { Header = "Add Child Folder" };
  185. additem.Click += (o, e) => { DoAddItem((_tree.SelectedItem as DynamicTreeNode)!.ID, true); };
  186. _menu.Items.Add(additem);
  187. _tree.ContextMenuOpening += _tree_ContextMenuOpening;
  188. _tree.ContextMenu = _menu;
  189. _tree.Background = new SolidColorBrush(Colors.DimGray);
  190. var cellStyle = new Style(typeof(TreeGridRowControl));
  191. cellStyle.Setters.Add(new Setter(BackgroundProperty, new SolidColorBrush(Colors.White)));
  192. _tree.RowStyle = cellStyle;
  193. _tree.SelectionBackground = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF, 0x11, 0x9E, 0xD9));
  194. _tree.Columns.Add(new TreeGridTextColumn()
  195. {
  196. MappingName = "Description"
  197. }
  198. );
  199. _tree.ColumnSizer = TreeColumnSizer.Star;
  200. _tree.RowHeight = 30D;
  201. _tree.SetValue(Grid.RowProperty, 0);
  202. _grid.Children.Add(_tree);
  203. _dock = new DockPanel();
  204. _dock.SetValue(Grid.RowProperty, 1);
  205. _grid.Children.Add(_dock);
  206. _add = CreateButton(Wpf.Resources.add.AsBitmapImage(System.Drawing.Color.White), "", "Add Item", (o) => DoAddItem(Guid.Empty, true));
  207. _add.Margin = new Thickness(0, 2, 2, 0);
  208. _add.Visibility = Visibility.Collapsed;
  209. _add.SetValue(DockPanel.DockProperty, Dock.Left);
  210. _dock.Children.Add(_add);
  211. _edit = CreateButton(Wpf.Resources.pencil.AsBitmapImage(System.Drawing.Color.White), "", "Edit Item", EditItem);
  212. _edit.Margin = new Thickness(0, 2, 2, 0);
  213. _edit.Visibility = Visibility.Collapsed;
  214. _edit.SetValue(DockPanel.DockProperty, Dock.Left);
  215. _dock.Children.Add(_edit);
  216. _delete = CreateButton(Wpf.Resources.delete.AsBitmapImage(System.Drawing.Color.White), "", "Delete Item", DeleteItem);
  217. _delete.Margin = new Thickness(2, 2, 0, 0);
  218. _delete.Visibility = Visibility.Collapsed;
  219. _delete.SetValue(DockPanel.DockProperty, Dock.Right);
  220. _dock.Children.Add(_delete);
  221. _spacer = new Label();
  222. _spacer.SetValue(DockPanel.DockProperty, Dock.Left);
  223. _dock.Children.Add(_spacer);
  224. Content = _grid;
  225. SizeChanged += DynamicTreeView_SizeChanged;
  226. }
  227. #region Public Interface
  228. public void AddItem(DynamicTreeNode? parentNode = null, bool edit = true)
  229. {
  230. var id = parentNode?.ID ?? Guid.Empty;
  231. DoAddItem(id, edit);
  232. }
  233. #endregion
  234. private void _tree_ContextMenuOpening(object sender, ContextMenuEventArgs e)
  235. {
  236. _menu.Items.Clear();
  237. if (OnContextMenuOpening is not null)
  238. {
  239. OnContextMenuOpening.Invoke((_tree.SelectedItem as DynamicTreeNode)!, _menu);
  240. if(_menu.Items.Count == 0)
  241. {
  242. e.Handled = true;
  243. }
  244. }
  245. else
  246. {
  247. _menu.AddItem("Add Item", null, (_tree.SelectedItem as DynamicTreeNode)!.ID, (id) => DoAddItem(id,true));
  248. }
  249. }
  250. private void DynamicTreeView_SizeChanged(object sender, SizeChangedEventArgs e)
  251. {
  252. CalculateRowHeight();
  253. }
  254. private void CalculateRowHeight()
  255. {
  256. if(Data != null && Data.Rows.Count > 0)
  257. {
  258. var contentHeight = _tree.ActualHeight - (_tree.Padding.Top + _tree.Padding.Bottom) - 2; // Two extra pixels of space
  259. var targetHeight = contentHeight / Data.Rows.Count;
  260. _tree.RowHeight = Math.Max(Math.Min(targetHeight, MaxRowHeight), MinRowHeight);
  261. }
  262. }
  263. private Button CreateButton(BitmapImage? image = null, string? text = null, string? tooltip = null, Action<Button>? action = null)
  264. {
  265. var button = new Button();
  266. button.SetValue(BorderBrushProperty, new SolidColorBrush(Colors.Gray));
  267. button.SetValue(BorderThicknessProperty, new Thickness(0.75));
  268. button.Height = 30;
  269. button.MinWidth = 30;
  270. button.Click += (o, e) => action?.Invoke(button);
  271. UpdateButton(button, image, text, tooltip);
  272. return button;
  273. }
  274. protected void UpdateButton(Button button, BitmapImage? image, string? text, string? tooltip = null)
  275. {
  276. var stackPnl = new StackPanel();
  277. stackPnl.Orientation = Orientation.Horizontal;
  278. if (image != null)
  279. {
  280. var img = new Image();
  281. img.Source = image;
  282. img.Margin = new Thickness(2);
  283. img.ToolTip = tooltip;
  284. stackPnl.Children.Add(img);
  285. }
  286. if (!string.IsNullOrEmpty(text))
  287. {
  288. var lbl = new Label();
  289. lbl.Content = text;
  290. lbl.VerticalAlignment = VerticalAlignment.Stretch;
  291. lbl.VerticalContentAlignment = VerticalAlignment.Center;
  292. lbl.Margin = new Thickness(2, 0, 5, 0);
  293. lbl.ToolTip = ToolTip;
  294. stackPnl.Children.Add(lbl);
  295. }
  296. button.Content = stackPnl;
  297. button.ToolTip = tooltip;
  298. }
  299. private void OptionsChanged(object sender, EventArgs args)
  300. {
  301. _add.Visibility = Options.Contains(DynamicTreeOption.Add) ? Visibility.Visible : Visibility.Collapsed;
  302. _edit.Visibility = Options.Contains(DynamicTreeOption.Edit) ? Visibility.Visible : Visibility.Collapsed;
  303. _delete.Visibility = Options.Contains(DynamicTreeOption.Delete) ? Visibility.Visible : Visibility.Collapsed;
  304. }
  305. protected virtual T DoCreateItem(Guid parent)
  306. {
  307. T result = new T();
  308. CoreUtils.SetPropertyValue(result, CoreUtils.GetFullPropertyName(ParentID, "."), parent);
  309. return result;
  310. }
  311. protected abstract T? DoLoadItem(Guid id);
  312. protected virtual bool DoEditItem(T item)
  313. {
  314. var form = new DynamicEditorForm(typeof(T));
  315. form.Items = new T[] { item };
  316. return form.ShowDialog() == true;
  317. }
  318. protected abstract void DoSaveItem(T item);
  319. protected abstract bool DoDeleteItem(Guid id);
  320. protected virtual void DoAddItem(Guid id, bool edit)
  321. {
  322. try
  323. {
  324. T item = DoCreateItem(id);
  325. if (edit)
  326. {
  327. if (DoEditItem(item))
  328. {
  329. DoSaveItem(item);
  330. Refresh();
  331. }
  332. }
  333. else
  334. {
  335. DoSaveItem(item);
  336. Refresh();
  337. }
  338. }
  339. catch (Exception e)
  340. {
  341. MessageBox.Show(e.Message);
  342. }
  343. }
  344. private void EditItem(Button button)
  345. {
  346. var node = _tree.SelectedItem as DynamicTreeNode;
  347. if (node == null)
  348. {
  349. MessageBox.Show("Please Select an item to edit!");
  350. return;
  351. }
  352. var item = DoLoadItem(node.ID);
  353. if (item != null && DoEditItem(item))
  354. {
  355. DoSaveItem(item);
  356. Refresh();
  357. }
  358. }
  359. private void DeleteItem(Button button)
  360. {
  361. var node = _tree.SelectedItem as DynamicTreeNode;
  362. if (node == null)
  363. {
  364. MessageBox.Show("Please Select an item to edit!");
  365. return;
  366. }
  367. if (DoDeleteItem(node.ID))
  368. {
  369. Refresh();
  370. }
  371. }
  372. public DynamicTreeNodes Nodes { get; set; }
  373. protected abstract void DoRefresh(Action<CoreTable?, Exception?> action);
  374. private void AfterRefresh()
  375. {
  376. var nodes = new DynamicTreeNodes();
  377. foreach (var row in Data.Rows)
  378. {
  379. var _id = row.Get(ID);
  380. var _parent = row.Get(ParentID);
  381. var _description = row.Get(Description);
  382. nodes.Add(_id, _parent).Description = _description;
  383. }
  384. Nodes = nodes;
  385. _tree.ItemsSource = nodes.Nodes;
  386. CalculateRowHeight();
  387. }
  388. public void Refresh()
  389. {
  390. DoRefresh((table, exception) =>
  391. {
  392. if(exception != null)
  393. {
  394. Dispatcher.Invoke(() =>
  395. {
  396. MessageBox.Show(String.Format("Error: {0}", exception.Message));
  397. });
  398. }
  399. else if(table is not null)
  400. {
  401. Data = table;
  402. Dispatcher.Invoke(() =>
  403. {
  404. AfterRefresh();
  405. });
  406. }
  407. });
  408. }
  409. }
  410. }