CoreTreeNodes.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. namespace InABox.Core
  8. {
  9. public class CoreTreeNode : INotifyPropertyChanged
  10. {
  11. private CoreTreeNodes _owner;
  12. public ObservableCollection<CoreTreeNode> Children => new ObservableCollection<CoreTreeNode>(_owner.GetChildrenOfParent(_id));
  13. private Guid _id;
  14. public Guid ID
  15. {
  16. get { return _id; }
  17. set
  18. {
  19. _id = value;
  20. RaisedOnPropertyChanged("ID");
  21. }
  22. }
  23. private Guid _parent;
  24. public Guid Parent
  25. {
  26. get { return _parent; }
  27. set
  28. {
  29. _parent = value;
  30. RaisedOnPropertyChanged("Parent");
  31. }
  32. }
  33. private object _image;
  34. public object Image
  35. {
  36. get => _image;
  37. set
  38. {
  39. _image = value;
  40. RaisedOnPropertyChanged("Image");
  41. }
  42. }
  43. private CoreRow _row;
  44. public CoreRow Row
  45. {
  46. get => _row;
  47. set
  48. {
  49. _row = value;
  50. RaisedOnPropertyChanged("Row");
  51. }
  52. }
  53. public object? this[string column]
  54. {
  55. get => Row[column];
  56. }
  57. public event PropertyChangedEventHandler? PropertyChanged;
  58. public void RaisedOnPropertyChanged(string propertyName)
  59. {
  60. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  61. }
  62. public CoreTreeNode(CoreTreeNodes owner, CoreRow row)
  63. {
  64. _owner = owner;
  65. _row = row;
  66. }
  67. public CoreTreeNode(CoreTreeNodes owner, Guid id, Guid parent, CoreRow row) : this(owner, row)
  68. {
  69. _id = id;
  70. _parent = parent;
  71. }
  72. public CoreTreeNode? GetParent() => _owner[_parent];
  73. public int Index()
  74. {
  75. var parent = GetParent();
  76. return parent != null
  77. ? parent.Children.IndexOf(this) + 1
  78. : _owner.Nodes.IndexOf(this) + 1;
  79. }
  80. public void InvalidateData()
  81. {
  82. RaisedOnPropertyChanged("Row");
  83. }
  84. public String Number
  85. {
  86. get
  87. {
  88. String result = Index().ToString();
  89. var parent = GetParent();
  90. while (parent != null)
  91. {
  92. int index = parent.Index();
  93. result = $"{index}.{result}";
  94. parent = parent.GetParent();
  95. }
  96. return result;
  97. }
  98. }
  99. }
  100. public class CoreTreeNodes
  101. {
  102. private List<CoreTreeNode> _nodes;
  103. private Dictionary<Guid, CoreTreeNode> _nodeMap;
  104. public ObservableCollection<CoreTreeNode> Nodes => new ObservableCollection<CoreTreeNode>(_nodes.Where(x => x.Parent == Guid.Empty));
  105. public CoreTreeNode? this[Guid id] => _nodes.FirstOrDefault(x => x.ID == id);
  106. public CoreTreeNodes()
  107. {
  108. _nodes = new List<CoreTreeNode>();
  109. _nodeMap = new Dictionary<Guid, CoreTreeNode>();
  110. }
  111. public CoreTreeNode Add(Guid id, Guid parent, CoreRow row)
  112. {
  113. var node = new CoreTreeNode(this, id, parent, row);
  114. _nodes.Add(node);
  115. _nodeMap[id] = node;
  116. return node;
  117. }
  118. public CoreTreeNode Find(Guid id) => _nodeMap.GetValueOrDefault(id);
  119. public CoreTreeNode? Find(CoreRow row) => _nodes.FirstOrDefault(x => x.Row == row);
  120. /// <summary>
  121. /// Gets a given node and recursively all its children given by <paramref name="id"/>.
  122. /// </summary>
  123. /// <param name="id"></param>
  124. /// <returns></returns>
  125. public IEnumerable<CoreTreeNode> GetChildren(Guid id)
  126. {
  127. if(!_nodeMap.TryGetValue(id, out var node))
  128. {
  129. yield break;
  130. }
  131. yield return node;
  132. var children = GetChildrenOfParent(id);
  133. foreach (var child in children)
  134. foreach (var item in GetChildren(child.ID))
  135. yield return item;
  136. }
  137. /// <summary>
  138. /// Get all the direct children of the parent given by <paramref name="id"/>.
  139. /// </summary>
  140. /// <param name="id"></param>
  141. /// <returns></returns>
  142. public IEnumerable<CoreTreeNode> GetChildrenOfParent(Guid id)
  143. {
  144. return _nodes.Where(x => x.Parent.Equals(id) && (x.ID != id));
  145. }
  146. public void Load<T>(CoreTable table, Expression<Func<T, Guid>> id, Expression<Func<T, Guid>> parentid)
  147. {
  148. _nodes.Clear();
  149. foreach (var row in table.Rows)
  150. {
  151. var _id = row.Get(id);
  152. var _parent = row.Get(parentid);
  153. Add(_id, _parent, row);
  154. }
  155. }
  156. }
  157. }