TreeNode1.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package com.hcks.cmfds.commons.lang;
  2. import java.io.Serializable;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. /**
  6. * easui中的tree_data.json数据,只能有一个root节点
  7. * [{
  8. "id":1,
  9. "text":"Folder1",
  10. "iconCls":"icon-save",
  11. "children":[{
  12. "text":"File1",
  13. "checked":true
  14. }]
  15. }]
  16. * 提供静态方法formatTree(List<TreeJson> list) 返回结果
  17. * TreeJson.formatTree(treeJsonlist) ;
  18. * @author lwb
  19. *
  20. */
  21. public class TreeNode1 implements Serializable {
  22. /**
  23. *
  24. */
  25. private static final long serialVersionUID = 1L;
  26. private String id ;
  27. private String pid ;
  28. private String text ;
  29. private String url ;
  30. private String iconCls ;
  31. private String state ;
  32. private String checked ;
  33. private List<TreeNode1> children = new ArrayList<TreeNode1>() ;
  34. /******** setter and getter **********/
  35. public static List<TreeNode1> formatTree(List<TreeNode1> list) {
  36. TreeNode1 root = new TreeNode1();
  37. TreeNode1 node = new TreeNode1();
  38. List<TreeNode1> treelist = new ArrayList<TreeNode1>();// 拼凑好的json格式的数据
  39. List<TreeNode1> parentnodes = new ArrayList<TreeNode1>();// parentnodes存放所有的父节点
  40. if (list != null && list.size() > 0) {
  41. root = list.get(0) ;
  42. //循环遍历oracle树查询的所有节点
  43. for (int i = 1; i < list.size(); i++) {
  44. node = list.get(i);
  45. if(node.getPid().equals(root.getId())){
  46. //为tree root 增加子节点
  47. parentnodes.add(node) ;
  48. root.getChildren().add(node) ;
  49. }else{//获取root子节点的孩子节点
  50. getChildrenNodes(parentnodes, node);
  51. parentnodes.add(node) ;
  52. }
  53. }
  54. }
  55. treelist.add(root) ;
  56. return treelist ;
  57. }
  58. private static void getChildrenNodes(List<TreeNode1> parentnodes, TreeNode1 node) {
  59. //循环遍历所有父节点和node进行匹配,确定父子关系
  60. for (int i = parentnodes.size() - 1; i >= 0; i--) {
  61. TreeNode1 pnode = parentnodes.get(i);
  62. //如果是父子关系,为父节点增加子节点,退出for循环
  63. if (pnode.getId().equals(node.getPid())) {
  64. pnode.setState("closed") ;//关闭二级树
  65. pnode.getChildren().add(node) ;
  66. return ;
  67. } else {
  68. //如果不是父子关系,删除父节点栈里当前的节点,
  69. //继续此次循环,直到确定父子关系或不存在退出for循环
  70. parentnodes.remove(i) ;
  71. }
  72. }
  73. }
  74. public String getId() {
  75. return id;
  76. }
  77. public void setId(String id) {
  78. this.id = id;
  79. }
  80. public String getPid() {
  81. return pid;
  82. }
  83. public void setPid(String pid) {
  84. this.pid = pid;
  85. }
  86. public String getText() {
  87. return text;
  88. }
  89. public void setText(String text) {
  90. this.text = text;
  91. }
  92. public String getIconCls() {
  93. return iconCls;
  94. }
  95. public void setIconCls(String iconCls) {
  96. this.iconCls = iconCls;
  97. }
  98. public String getState() {
  99. return state;
  100. }
  101. public void setState(String state) {
  102. this.state = state;
  103. }
  104. public String getChecked() {
  105. return checked;
  106. }
  107. public void setChecked(String checked) {
  108. this.checked = checked;
  109. }
  110. public String getUrl() {
  111. return url;
  112. }
  113. public void setUrl(String url) {
  114. this.url = url;
  115. }
  116. public List<TreeNode1> getChildren() {
  117. return children;
  118. }
  119. public void setChildren(List<TreeNode1> children) {
  120. this.children = children;
  121. }
  122. }