GTreeNode.cs 10 KB

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