CoreTreeNodes.cs 5.4 KB

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