TreeNode.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using System;
  2. using System.Collections.Generic;
  3. namespace FairyGUI
  4. {
  5. /// <summary>
  6. ///
  7. /// </summary>
  8. [Obsolete("Use GTree and GTreeNode instead")]
  9. public class TreeNode
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public object data;
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. public TreeNode parent { get; private set; }
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. public TreeView tree { get; private set; }
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. public GComponent cell { get; internal set; }
  27. /// <summary>
  28. ///
  29. /// </summary>
  30. public int level { get; private set; }
  31. private List<TreeNode> _children;
  32. private bool _expanded;
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. /// <param name="hasChild"></param>
  37. public TreeNode(bool hasChild)
  38. {
  39. if (hasChild)
  40. _children = new List<TreeNode>();
  41. }
  42. /// <summary>
  43. ///
  44. /// </summary>
  45. public bool expanded
  46. {
  47. get
  48. {
  49. return _expanded;
  50. }
  51. set
  52. {
  53. if (_children == null)
  54. return;
  55. if (_expanded != value)
  56. {
  57. _expanded = value;
  58. if (tree != null)
  59. {
  60. if (_expanded)
  61. tree.AfterExpanded(this);
  62. else
  63. tree.AfterCollapsed(this);
  64. }
  65. }
  66. }
  67. }
  68. /// <summary>
  69. ///
  70. /// </summary>
  71. public bool isFolder
  72. {
  73. get { return _children != null; }
  74. }
  75. /// <summary>
  76. ///
  77. /// </summary>
  78. public string text
  79. {
  80. get
  81. {
  82. if (cell != null)
  83. return cell.text;
  84. else
  85. return null;
  86. }
  87. }
  88. /// <summary>
  89. ///
  90. /// </summary>
  91. /// <param name="child"></param>
  92. /// <returns></returns>
  93. public TreeNode AddChild(TreeNode child)
  94. {
  95. AddChildAt(child, _children.Count);
  96. return child;
  97. }
  98. /// <summary>
  99. ///
  100. /// </summary>
  101. /// <param name="child"></param>
  102. /// <param name="index"></param>
  103. /// <returns></returns>
  104. public TreeNode AddChildAt(TreeNode child, int index)
  105. {
  106. if (child == null)
  107. throw new Exception("child is null");
  108. int numChildren = _children.Count;
  109. if (index >= 0 && index <= numChildren)
  110. {
  111. if (child.parent == this)
  112. {
  113. SetChildIndex(child, index);
  114. }
  115. else
  116. {
  117. if (child.parent != null)
  118. child.parent.RemoveChild(child);
  119. int cnt = _children.Count;
  120. if (index == cnt)
  121. _children.Add(child);
  122. else
  123. _children.Insert(index, child);
  124. child.parent = this;
  125. child.level = this.level + 1;
  126. child.SetTree(this.tree);
  127. if (this.cell != null && this.cell.parent != null && _expanded)
  128. tree.AfterInserted(child);
  129. }
  130. return child;
  131. }
  132. else
  133. {
  134. throw new Exception("Invalid child index");
  135. }
  136. }
  137. /// <summary>
  138. ///
  139. /// </summary>
  140. /// <param name="child"></param>
  141. /// <returns></returns>
  142. public TreeNode RemoveChild(TreeNode child)
  143. {
  144. int childIndex = _children.IndexOf(child);
  145. if (childIndex != -1)
  146. {
  147. RemoveChildAt(childIndex);
  148. }
  149. return child;
  150. }
  151. /// <summary>
  152. ///
  153. /// </summary>
  154. /// <param name="index"></param>
  155. /// <returns></returns>
  156. public TreeNode RemoveChildAt(int index)
  157. {
  158. if (index >= 0 && index < numChildren)
  159. {
  160. TreeNode child = _children[index];
  161. _children.RemoveAt(index);
  162. child.parent = null;
  163. if (tree != null)
  164. {
  165. child.SetTree(null);
  166. tree.AfterRemoved(child);
  167. }
  168. return child;
  169. }
  170. else
  171. {
  172. throw new Exception("Invalid child index");
  173. }
  174. }
  175. /// <summary>
  176. ///
  177. /// </summary>
  178. /// <param name="beginIndex"></param>
  179. /// <param name="endIndex"></param>
  180. public void RemoveChildren(int beginIndex = 0, int endIndex = -1)
  181. {
  182. if (endIndex < 0 || endIndex >= numChildren)
  183. endIndex = numChildren - 1;
  184. for (int i = beginIndex; i <= endIndex; ++i)
  185. RemoveChildAt(beginIndex);
  186. }
  187. /// <summary>
  188. ///
  189. /// </summary>
  190. /// <param name="index"></param>
  191. /// <returns></returns>
  192. public TreeNode GetChildAt(int index)
  193. {
  194. if (index >= 0 && index < numChildren)
  195. return _children[index];
  196. else
  197. throw new Exception("Invalid child index");
  198. }
  199. /// <summary>
  200. ///
  201. /// </summary>
  202. /// <param name="child"></param>
  203. /// <returns></returns>
  204. public int GetChildIndex(TreeNode child)
  205. {
  206. return _children.IndexOf(child);
  207. }
  208. /// <summary>
  209. ///
  210. /// </summary>
  211. /// <returns></returns>
  212. public TreeNode GetPrevSibling()
  213. {
  214. if (parent == null)
  215. return null;
  216. int i = parent._children.IndexOf(this);
  217. if (i <= 0)
  218. return null;
  219. return parent._children[i - 1];
  220. }
  221. /// <summary>
  222. ///
  223. /// </summary>
  224. /// <returns></returns>
  225. public TreeNode GetNextSibling()
  226. {
  227. if (parent == null)
  228. return null;
  229. int i = parent._children.IndexOf(this);
  230. if (i < 0 || i >= parent._children.Count - 1)
  231. return null;
  232. return parent._children[i + 1];
  233. }
  234. /// <summary>
  235. ///
  236. /// </summary>
  237. /// <param name="child"></param>
  238. /// <param name="index"></param>
  239. public void SetChildIndex(TreeNode child, int index)
  240. {
  241. int oldIndex = _children.IndexOf(child);
  242. if (oldIndex == -1)
  243. throw new Exception("Not a child of this container");
  244. int cnt = _children.Count;
  245. if (index < 0)
  246. index = 0;
  247. else if (index > cnt)
  248. index = cnt;
  249. if (oldIndex == index)
  250. return;
  251. _children.RemoveAt(oldIndex);
  252. _children.Insert(index, child);
  253. if (this.cell != null && this.cell.parent != null && _expanded)
  254. tree.AfterMoved(child);
  255. }
  256. /// <summary>
  257. ///
  258. /// </summary>
  259. /// <param name="child1"></param>
  260. /// <param name="child2"></param>
  261. public void SwapChildren(TreeNode child1, TreeNode child2)
  262. {
  263. int index1 = _children.IndexOf(child1);
  264. int index2 = _children.IndexOf(child2);
  265. if (index1 == -1 || index2 == -1)
  266. throw new Exception("Not a child of this container");
  267. SwapChildrenAt(index1, index2);
  268. }
  269. /// <summary>
  270. ///
  271. /// </summary>
  272. /// <param name="index1"></param>
  273. /// <param name="index2"></param>
  274. public void SwapChildrenAt(int index1, int index2)
  275. {
  276. TreeNode child1 = _children[index1];
  277. TreeNode child2 = _children[index2];
  278. SetChildIndex(child1, index2);
  279. SetChildIndex(child2, index1);
  280. }
  281. /// <summary>
  282. ///
  283. /// </summary>
  284. public int numChildren
  285. {
  286. get { return (null == _children) ? 0 : _children.Count; }
  287. }
  288. internal void SetTree(TreeView value)
  289. {
  290. tree = value;
  291. if (tree != null && tree.treeNodeWillExpand != null && _expanded)
  292. tree.treeNodeWillExpand(this, true);
  293. if (_children != null)
  294. {
  295. int cnt = _children.Count;
  296. for (int i = 0; i < cnt; i++)
  297. {
  298. TreeNode node = _children[i];
  299. node.level = level + 1;
  300. node.SetTree(value);
  301. }
  302. }
  303. }
  304. }
  305. }