CoreTreeNodes.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 => _owner.GetChilden(_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. public event PropertyChangedEventHandler? PropertyChanged;
  54. public void RaisedOnPropertyChanged(string propertyName)
  55. {
  56. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  57. }
  58. public CoreTreeNode(CoreTreeNodes owner)
  59. {
  60. _owner = owner;
  61. _description = "";
  62. }
  63. public CoreTreeNode(CoreTreeNodes owner, Guid id, Guid parent) : this(owner)
  64. {
  65. _id = id;
  66. _parent = parent;
  67. }
  68. public CoreTreeNode GetParent() => _owner.Nodes.FirstOrDefault(x => x.ID == _parent);
  69. public int Index()
  70. {
  71. var parent = GetParent();
  72. return parent != null
  73. ? parent.Children.IndexOf(this) + 1
  74. : _owner.Nodes.IndexOf(this) + 1;
  75. }
  76. public String Number
  77. {
  78. get
  79. {
  80. String result = Index().ToString();
  81. var parent = GetParent();
  82. while (parent != null)
  83. {
  84. int index = parent.Index();
  85. result = $"{index}.{result}";
  86. parent = parent.GetParent();
  87. }
  88. return result;
  89. }
  90. }
  91. }
  92. public class CoreTreeNodes
  93. {
  94. private List<CoreTreeNode> _nodes;
  95. public ObservableCollection<CoreTreeNode> Nodes => new ObservableCollection<CoreTreeNode>(_nodes.Where(x => x.Parent == Guid.Empty));
  96. public CoreTreeNode? this[Guid id] => _nodes.FirstOrDefault(x => x.ID == id);
  97. public CoreTreeNodes()
  98. {
  99. _nodes = new List<CoreTreeNode>();
  100. }
  101. public CoreTreeNode Add(Guid id, Guid parent)
  102. {
  103. var node = new CoreTreeNode(this, id, parent);
  104. _nodes.Add(node);
  105. return node;
  106. }
  107. public CoreTreeNode Find(Guid id) => _nodes.FirstOrDefault(x => x.ID == id);
  108. public void GetChildren(List<Guid> nodes, Guid id)
  109. {
  110. nodes.Add(id);
  111. var children = GetChilden(id);
  112. foreach (var child in children)
  113. GetChildren(nodes, child.ID);
  114. }
  115. public ObservableCollection<CoreTreeNode> GetChilden(Guid id)
  116. {
  117. return new ObservableCollection<CoreTreeNode>(_nodes.Where(x => x.Parent.Equals(id) && (x.ID != id)));
  118. }
  119. public void Load<T>(CoreTable table, Expression<Func<T, Guid>> id, Expression<Func<T, Guid>> parentid, Expression<Func<T, String>> description)
  120. {
  121. _nodes.Clear();
  122. foreach (var row in table.Rows)
  123. {
  124. Guid _id = row.Get<T, Guid>(id);
  125. Guid _parent = row.Get<T, Guid>(parentid);
  126. String _description = row.Get<T, String>(description);
  127. Add(_id, _parent).Description = _description;
  128. }
  129. }
  130. }
  131. }