TreeViewModel.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System.Collections.ObjectModel;
  2. using System.ComponentModel.Composition;
  3. using System.IO;
  4. using Helper;
  5. using Microsoft.Practices.Prism.Mvvm;
  6. using Microsoft.Practices.Prism.PubSubEvents;
  7. namespace Tree
  8. {
  9. [Export(typeof (TreeViewModel)), PartCreationPolicy(CreationPolicy.NonShared)]
  10. public class TreeViewModel: BindableBase
  11. {
  12. public IEventAggregator EventAggregator { get; set; }
  13. private AllTreeData allTreeData;
  14. private readonly ObservableCollection<TreeNodeViewModel> treeNodes =
  15. new ObservableCollection<TreeNodeViewModel>();
  16. public TreeViewModel(IEventAggregator eventAggregator)
  17. {
  18. this.EventAggregator = eventAggregator;
  19. }
  20. public ObservableCollection<TreeNodeViewModel> TreeNodes
  21. {
  22. get
  23. {
  24. return this.treeNodes;
  25. }
  26. }
  27. private TreeNodeViewModel Root
  28. {
  29. get
  30. {
  31. return this.treeNodes.Count == 0? null : this.treeNodes[0];
  32. }
  33. }
  34. public void SelectNodeChange(TreeNodeViewModel treeNodeViewModel)
  35. {
  36. this.EventAggregator.GetEvent<SelectNodeChangeEvent>().Publish(treeNodeViewModel);
  37. }
  38. public void Add(TreeNodeViewModel treeNode, TreeNodeViewModel parent)
  39. {
  40. // 如果父节点是折叠的,需要先展开父节点
  41. if (parent != null && parent.IsFolder)
  42. {
  43. this.UnFold(parent);
  44. }
  45. this.treeNodes.Add(treeNode);
  46. if (parent != null)
  47. {
  48. parent.Children.Add(treeNode);
  49. }
  50. TreeLayout.ExcuteLayout(this.Root);
  51. }
  52. private void RecursionRemove(TreeNodeViewModel treeNodeViewModel)
  53. {
  54. for (int i = 0; i < treeNodeViewModel.Children.Count; ++i)
  55. {
  56. this.RecursionRemove(treeNodeViewModel.Children[i]);
  57. }
  58. this.treeNodes.Remove(treeNodeViewModel);
  59. }
  60. public void Remove(TreeNodeViewModel treeNodeViewModel)
  61. {
  62. this.RecursionRemove(treeNodeViewModel);
  63. treeNodeViewModel.Parent.Children.Remove(treeNodeViewModel);
  64. TreeLayout.ExcuteLayout(this.Root);
  65. }
  66. private void RecursionMove(
  67. TreeNodeViewModel treeNodeViewModel, double offsetX, double offsetY)
  68. {
  69. treeNodeViewModel.X += offsetX;
  70. treeNodeViewModel.Y += offsetY;
  71. foreach (var node in treeNodeViewModel.Children)
  72. {
  73. this.RecursionMove(node, offsetX, offsetY);
  74. }
  75. }
  76. public void MoveToPosition(double offsetX, double offsetY)
  77. {
  78. this.RecursionMove(this.Root, offsetX, offsetY);
  79. }
  80. public void MoveToNode(TreeNodeViewModel from, TreeNodeViewModel to)
  81. {
  82. // from节点不能是to节点的父级节点
  83. TreeNodeViewModel tmpNode = to;
  84. while (tmpNode != null)
  85. {
  86. if (tmpNode.IsRoot)
  87. {
  88. break;
  89. }
  90. if (tmpNode.Id == from.Id)
  91. {
  92. return;
  93. }
  94. tmpNode = tmpNode.Parent;
  95. }
  96. if (from.IsFolder)
  97. {
  98. this.UnFold(from);
  99. }
  100. if (to.IsFolder)
  101. {
  102. this.UnFold(to);
  103. }
  104. from.Parent.Children.Remove(from);
  105. to.Children.Add(from);
  106. from.Parent = to;
  107. TreeLayout.ExcuteLayout(this.Root);
  108. }
  109. /// <summary>
  110. /// 折叠节点
  111. /// </summary>
  112. /// <param name="treeNodeViewModel"></param>
  113. public void Fold(TreeNodeViewModel treeNodeViewModel)
  114. {
  115. foreach (var node in treeNodeViewModel.Children)
  116. {
  117. this.RecursionRemove(node);
  118. }
  119. treeNodeViewModel.IsFolder = true;
  120. TreeLayout.ExcuteLayout(this.Root);
  121. }
  122. /// <summary>
  123. /// 展开节点,一级一级展开,一次只展开下层子节点,比如下层节点是折叠的,那下下层节点不展开
  124. /// </summary>
  125. /// <param name="unFoldNode"></param>
  126. public void UnFold(TreeNodeViewModel unFoldNode)
  127. {
  128. foreach (var tn in unFoldNode.Children)
  129. {
  130. this.RecursionAdd(tn);
  131. }
  132. unFoldNode.IsFolder = false;
  133. TreeLayout.ExcuteLayout(this.Root);
  134. }
  135. private void RecursionAdd(TreeNodeViewModel treeNodeViewModel)
  136. {
  137. if (!this.treeNodes.Contains(treeNodeViewModel))
  138. {
  139. this.treeNodes.Add(treeNodeViewModel);
  140. }
  141. ObservableCollection<TreeNodeViewModel> children = treeNodeViewModel.Children;
  142. if (treeNodeViewModel.IsFolder)
  143. {
  144. return;
  145. }
  146. foreach (var tn in children)
  147. {
  148. this.RecursionAdd(tn);
  149. }
  150. }
  151. /// <summary>
  152. /// 序列化保存
  153. /// </summary>
  154. public void Save(string filePath)
  155. {
  156. //this.RecursionSave(treeNodeDataArray, this.Root);
  157. //byte[] bytes = ProtobufHelper.ToBytes(treeNodeDataArray);
  158. //using (Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
  159. //{
  160. // stream.Write(bytes, 0, bytes.Length);
  161. //}
  162. }
  163. private void RecursionSave(AllTreeData allTreeData, TreeNodeViewModel node)
  164. {
  165. //if (node == null)
  166. //{
  167. // return;
  168. //}
  169. //allTreeData.Add(node.TreeNodeData);
  170. //foreach (TreeNodeViewModel childNode in node.Children)
  171. //{
  172. // this.RecursionSave(allTreeData, childNode);
  173. //}
  174. }
  175. /// <summary>
  176. /// 从配置中加载
  177. /// </summary>
  178. /// <param name="filePath"></param>
  179. public void Load(string filePath)
  180. {
  181. this.TreeNodes.Clear();
  182. byte[] bytes = File.ReadAllBytes(filePath);
  183. this.allTreeData = ProtobufHelper.FromBytes<AllTreeData>(bytes);
  184. allTreeData.Init();
  185. }
  186. private void RecursionLoad(
  187. AllTreeData allTreeData, TreeNodeData treeNodeData,
  188. TreeNodeViewModel parentNode)
  189. {
  190. var node = new TreeNodeViewModel(treeNodeData, parentNode);
  191. this.Add(node, parentNode);
  192. foreach (int id in treeNodeData.Children)
  193. {
  194. TreeNodeData childNodeData = allTreeData[id];
  195. this.RecursionLoad(allTreeData, childNodeData, node);
  196. }
  197. TreeLayout.ExcuteLayout(this.Root);
  198. }
  199. }
  200. }