jsTree pre1.0使用笔记(下)

上篇大致讲了原理,这篇主要罗列代码。代码写的不是很好,大致思路应该能够看的出来,仅供参考:

  
  
  
  
  1. public String getSrcStructureStr(String appProfileId, String id, String onlyShowCheckedExcept, String showCalledFunction, String onlyShowInterfaceAndMethod) { 
  2.          
  3.         StringBuilder sb = new StringBuilder(); 
  4.          
  5.         // 请求所有包名 
  6.         if (id.equals("0")) { 
  7.             List<String> packageNames = null
  8.              
  9.             if (onlyShowCheckedExcept.equals("true")) { 
  10.                 packageNames = getAllPackageNamesWithThrowExceptFunction(appProfileId, onlyShowInterfaceAndMethod); 
  11.             } else { 
  12.                 packageNames = getAllPackageNames(appProfileId, onlyShowInterfaceAndMethod); 
  13.             } 
  14.              
  15.             Collection<String> commonPackagePrefixes = getThreeLevelCommonPrefixes(packageNames); 
  16.              
  17.             JSONArray jsonArr = new JSONArray(); 
  18.              
  19.             for(String packageName : commonPackagePrefixes) { 
  20.                 TreeNode nodeP = new TreeNode(); 
  21.                 nodeP.setId("pkg:" + packageName); 
  22.                 nodeP.setRel("pkg"); 
  23.                 nodeP.setPkg(packageName); 
  24.                 nodeP.setTitle(packageName); 
  25.                 //nodeP.setTitle("pkg:" + packageName); 
  26.                 nodeP.setState(TreeNode.NODE_STATE_CLOSED); 
  27.                  
  28.                 JSONObject jsonObj = nodeP.toJsonObject(); 
  29.                 if (jsonObj != null) { 
  30.                     jsonArr.put(jsonObj); 
  31.                 } 
  32.             } 
  33.              
  34.             return jsonArr.toString(); 
  35.         } else 
  36.          
  37.         // 请求所有类节点 
  38.         if (id.startsWith("pkg:") && (id.indexOf(",cls:") <= 0)) { 
  39.             // 包名长度为0,这种情形不会出现 
  40.             if (id.length() == 4) { 
  41.                 return "[]"
  42.             } 
  43.  
  44.             // "pkg:"占4个位置 
  45.             String packageName = id.substring(4); 
  46.              
  47.             JSONArray jsonArr = new JSONArray(); 
  48.              
  49.             List<String> packageNames = null
  50.             if (onlyShowCheckedExcept.equals("true")) { 
  51.                 packageNames = getAllPackageNamesWithThrowExceptFunction(appProfileId, onlyShowInterfaceAndMethod); 
  52.             } else { 
  53.                 packageNames = getAllPackageNames(appProfileId, onlyShowInterfaceAndMethod); 
  54.             } 
  55.              
  56.             // 获取下两层真子包名 
  57.             Collection<String> subPackageNames = getNextTwoLevelCommonPrefixes(packageNames, packageName); 
  58.              
  59.             for(String subPackageName : subPackageNames) { 
  60.                 TreeNode nodeP = new TreeNode(); 
  61.                 nodeP.setId("pkg:" + subPackageName); 
  62.                 nodeP.setRel("pkg"); 
  63.                 nodeP.setPkg(subPackageName); 
  64.                 nodeP.setTitle(subPackageName.substring(packageName.length() + 1)); 
  65.                 //nodeP.setTitle("pkg:" + subPackageName); 
  66.                 nodeP.setState(TreeNode.NODE_STATE_CLOSED); 
  67.                                  
  68.                 JSONObject jsonObj = nodeP.toJsonObject(); 
  69.                 if (jsonObj != null) { 
  70.                     jsonArr.put(jsonObj); 
  71.                 } 
  72.             } 
  73.              
  74.             List<String> clsNames = null
  75.              
  76.             if (onlyShowCheckedExcept.equals("true")) { 
  77.                 clsNames = getAllClassNamesWithThrowExceptFunction(appProfileId, packageName, onlyShowInterfaceAndMethod); 
  78.             } else { 
  79.                 clsNames = getAllClassNames(appProfileId, packageName, onlyShowInterfaceAndMethod); 
  80.             } 
  81.              
  82.             for(String clsName : clsNames) { 
  83.                  
  84.                 TreeNode nodeC = new TreeNode(); 
  85.                 nodeC.setId("pkg:" + packageName + ",cls:" + clsName); 
  86.                 nodeC.setRel("cls"); 
  87.                 nodeC.setPkg(packageName); 
  88.                 nodeC.setCls(clsName); 
  89.                 nodeC.setTitle(clsName); 
  90.                 //nodeC.setTitle("pkg:" + packageName + ",cls:" + clsName); 
  91.                 nodeC.setState(TreeNode.NODE_STATE_CLOSED); 
  92.                 sb.append(nodeC.toJsonString()); 
  93.                  
  94.                 JSONObject jsonObj = nodeC.toJsonObject(); 
  95.                 if (jsonObj != null) { 
  96.                     jsonArr.put(jsonObj); 
  97.                 } 
  98.             } 
  99.              
  100.             return jsonArr.toString(); 
  101.         } else  
  102.              
  103.         // 请求所有方法节点 
  104.         if (id.startsWith("pkg:") && (id.indexOf(",cls:") > 0) && (id.indexOf(",method:") <= 0)) { 
  105.             // 包名长度为0,或者包名长度为1,但是类名长度为0,这种情形不会出现 
  106.             if(id.length() <= 10) { 
  107.                 return "[]"
  108.             } 
  109.              
  110.             int pkgNameLastPos = id.indexOf(",cls:"); 
  111.             // "pkg:"占4个位置 
  112.             String packageName = id.substring(4, pkgNameLastPos); 
  113.             // ",cls:"占5个位置 
  114.             String clsName = id.substring(pkgNameLastPos + 5); 
  115.              
  116.             List<MethodSignatureBean> methodBeans = getAllMethodNames(appProfileId, packageName, clsName); 
  117.  
  118.             JSONArray jsonArr = new JSONArray(); 
  119.              
  120.             for(MethodSignatureBean methodBean : methodBeans) { 
  121.                  
  122.                 if ((onlyShowCheckedExcept.equals("true")) && (methodBean.exceptions.equals(""))) { 
  123.                     continue
  124.                 } 
  125.                  
  126.                 // 过滤出调用方法中含有指定接口和接口方法的类 
  127.                 if ((onlyShowInterfaceAndMethod != null) && (!onlyShowInterfaceAndMethod.equals(""))) { 
  128.                     if (!methodBean.calledMethods.contains(onlyShowInterfaceAndMethod)) { 
  129.                         continue
  130.                     } 
  131.                 } 
  132.                  
  133.                 TreeNode nodeM = new TreeNode(); 
  134.                 nodeM.setId("pkg:" + packageName + ",cls:" + clsName + ",method:" + methodBean.methodName); 
  135.                 nodeM.setRel("method"); 
  136.                 nodeM.setPkg(packageName); 
  137.                 nodeM.setCls(clsName); 
  138.                 nodeM.setMethod(methodBean.methodName + methodBean.methodDesc); 
  139.                  
  140.                 String title = methodBean.methodName + TypeSignature.shortenSignature(methodBean.parameters) +  
  141.                     " " + TypeSignature.shortenSignature(methodBean.returnType); 
  142.                 if (!methodBean.exceptions.equals("")) { 
  143.                     title += " : " + methodBean.exceptions; 
  144.                 } 
  145.                  
  146.                 nodeM.setTitle(title); 
  147.                  
  148.                 String calledMethods = methodBean.calledMethods; 
  149.                  
  150.                 if (showCalledFunction.equals("true")&&(calledMethods != null)&&(!calledMethods.equals(""))) { 
  151.                     nodeM.setState(TreeNode.NODE_STATE_CLOSED); 
  152.                 } else { 
  153.                     nodeM.setState(TreeNode.NODE_STATE_LEAF); 
  154.                 } 
  155.                  
  156.                 JSONObject jsonObj = nodeM.toJsonObject(); 
  157.                 if (jsonObj != null) { 
  158.                     jsonArr.put(jsonObj); 
  159.                 } 
  160.             } 
  161.              
  162.             return jsonArr.toString(); 
  163.         } else 
  164.         // 请求所有函数内的调用指令节点 
  165.         if (id.startsWith("pkg:") && (id.indexOf(",cls:") > 0) && (id.indexOf(",method:") > 0)) { 
  166.             int pkgNameLastPos = id.indexOf(",cls:"); 
  167.             // "pkg:"占4个位置 
  168.             String packageName = id.substring(4, pkgNameLastPos); 
  169.              
  170.             int clsNameLastPos = id.indexOf(",method:"); 
  171.             // ",cls:"占5个位置 
  172.             String clsName = id.substring(pkgNameLastPos + 5, clsNameLastPos); 
  173.              
  174.             // ",method:"占8个位置 
  175.             String methodName = id.substring(clsNameLastPos + 8); 
  176.              
  177.             if (packageName.equals("") || clsName.equals("") || methodName.equals("")) { 
  178.                 return "[]"
  179.             } 
  180.              
  181.             List<MethodSignatureBean> methodBeans = getExactMethod(appProfileId, packageName, clsName, methodName); 
  182.             int count = methodBeans.size(); 
  183.             if (count == 0) { 
  184.                 return "[]"
  185.             } 
  186.              
  187.             MethodSignatureBean methodBean = methodBeans.get(0); 
  188.              
  189.             String calledMethods = methodBean.calledMethods; 
  190.              
  191.             String[] calledMethodsArr = calledMethods.split("%"); 
  192.              
  193.             JSONArray jsonArr = new JSONArray(); 
  194.              
  195.             for(String calledMethod : calledMethodsArr) { 
  196.                 // 跳过 
  197.                 if (calledMethod.equals("")) { 
  198.                     continue
  199.                 } 
  200.                  
  201.                 // 过滤出调用方法中含有指定接口和接口方法的类 
  202.                 if ((onlyShowInterfaceAndMethod != null) && (!onlyShowInterfaceAndMethod.equals(""))) { 
  203.                     if (!calledMethod.contains(onlyShowInterfaceAndMethod)) { 
  204.                         continue
  205.                     } 
  206.                 } 
  207.                  
  208.                 String[] calledClsMethodDescArr = calledMethod.split("@"); 
  209.                 String calledClsName = calledClsMethodDescArr[0]; 
  210.                 String calledMethodName = calledClsMethodDescArr[1]; 
  211.                 String calledMethodDescAndIp = calledClsMethodDescArr[2]; 
  212.                 String[] calledMethodDescAndIpArr = calledMethodDescAndIp.split("#"); 
  213.                 String calledMethodDesc = calledMethodDescAndIpArr[0]; 
  214.                 String calledIp = calledMethodDescAndIpArr[1]; 
  215.                  
  216.                 TreeNode nodeM = new TreeNode(); 
  217.                 nodeM.setId("pkg:" + packageName + ",cls:" + clsName + ",method:" + methodBean.methodName); 
  218.                 nodeM.setRel("called_method"); 
  219.                 nodeM.setPkg(packageName); 
  220.                 nodeM.setCls(clsName); 
  221.                 nodeM.setMethod(methodBean.methodName + methodBean.methodDesc); 
  222.                 nodeM.setCalled_cls(calledClsName); 
  223.                 nodeM.setCalled_method(calledMethodName + "@" + calledMethodDesc + "#" + calledIp); 
  224.                  
  225.                 TypeSignature ts = new TypeSignature(calledMethodDesc); 
  226.                 String calledReturnType = ts.getReturnType(); 
  227.                 String calledParameters = ts.getParameters(); 
  228.                 String shortenMethodDesc = TypeSignature.shortenSignature(calledParameters) + " " + 
  229.                     TypeSignature.shortenSignature(calledReturnType); 
  230.                  
  231.                  
  232.                  
  233.                  
  234.          
  235.                 String title = TypeSignature.shortenSignature(calledClsName.replace("/"".")) + " " + calledMethodName + shortenMethodDesc 
  236.                     + " #" + calledIp; 
  237.                  
  238.                 nodeM.setTitle(title); 
  239.                 nodeM.setState(TreeNode.NODE_STATE_LEAF); 
  240.                                  
  241.                 JSONObject jsonObj = nodeM.toJsonObject(); 
  242.                 if (jsonObj != null) { 
  243.                     jsonArr.put(jsonObj); 
  244.                 } 
  245.             } 
  246.              
  247.             return jsonArr.toString(); 
  248.              
  249.         } 
  250.          
  251.         return "[]"
  252.     } 

TreeNode是从jquery插件中抽取出来,再经过些许改造,也完整地贴出来:

 

  
  
  
  
  1. public class TreeNode { 
  2.  
  3.     public static final String NODE_STATE_CLOSED = "closed"
  4.     public static final String NODE_STATE_LEAF = "leaf"
  5.     public static final String NODE_STATE_OPEN = "open"
  6.  
  7.     private Map<String, Object> attr; 
  8.     private Collection<TreeNode> children; 
  9.     private Map<String, Object> data; 
  10.     private String icon; 
  11.     private String id; 
  12.     private String state = TreeNode.NODE_STATE_CLOSED; 
  13.     private String title; 
  14.     private String type; 
  15.      
  16.     // 五和增加:jstree为了规避IE的兼容性,图标类型用rel属性来标识(同时,它也可以代表节点类型) 
  17.     private String rel; 
  18.      
  19.     // 五和:为了方便界面的javascript处理,直接将值返回给界面 
  20.     // 所在包名 
  21.     private String pkg; 
  22.     // 所在类名 
  23.     private String cls; 
  24.     // 所在函数名 
  25.     private String method; 
  26.      
  27.     // 被调用函数所在类 
  28.     private String called_cls; 
  29.      
  30.     // 被调用函数 
  31.     private String called_method; 
  32.  
  33.     public TreeNode() { 
  34.         super(); 
  35.     } 
  36.  
  37.     public TreeNode(String title) { 
  38.         super(); 
  39.         this.title = title; 
  40.     } 
  41.  
  42.     public TreeNode(String title, Collection<TreeNode> children) { 
  43.         super(); 
  44.         setTitle(title); 
  45.         setChildren(children); 
  46.     } 
  47.  
  48.     public TreeNode(String id, String title) { 
  49.         super(); 
  50.         setId(id); 
  51.         setTitle(title); 
  52.     } 
  53.  
  54.     public TreeNode(String id, String title, Collection<TreeNode> children) { 
  55.         super(); 
  56.         setId(id); 
  57.         setTitle(title); 
  58.         setChildren(children); 
  59.     } 
  60.  
  61.     public Map<String, Object> getAttr() { 
  62.         return attr; 
  63.     } 
  64.  
  65.     public Collection<TreeNode> getChildren() { 
  66.         return children; 
  67.     } 
  68.  
  69.     public Map<String, Object> getData() { 
  70.         return data; 
  71.     } 
  72.  
  73.     /** 
  74.      * Get the Tree Node Title 
  75.      */ 
  76.     // public String getDatay() { 
  77.     // return title; 
  78.     // } 
  79.     public String getIcon() { 
  80.         return icon; 
  81.     } 
  82.  
  83.     public String getId() { 
  84.         return id; 
  85.     } 
  86.  
  87.     public String getState() { 
  88.         return state; 
  89.     } 
  90.  
  91.     public String getTitle() { 
  92.         return title; 
  93.     } 
  94.  
  95.     public String getType() { 
  96.         return type; 
  97.     } 
  98.  
  99.     public void setAttr(Map<String, Object> attr) { 
  100.         this.attr = attr; 
  101.     } 
  102.  
  103.     /** 
  104.      * Set the Tree Node Childrens 
  105.      *  
  106.      * @param children 
  107.      */ 
  108.     public void setChildren(Collection<TreeNode> children) { 
  109.         this.children = children; 
  110.     } 
  111.  
  112.     public void setData(Map<String, Object> data) { 
  113.         this.data = data; 
  114.     } 
  115.  
  116.     /** 
  117.      * Set the Tree Node Icon 
  118.      *  
  119.      * @param icon 
  120.      */ 
  121.     public void setIcon(String icon) { 
  122.         if (this.data == null) { 
  123.             data = new HashMap<String, Object>(); 
  124.         } 
  125.  
  126.         if (this.data.containsKey("icon")) { 
  127.             this.data.remove("icon"); 
  128.         } 
  129.         this.data.put("icon", icon); 
  130.         this.icon = icon; 
  131.     } 
  132.  
  133.     /** 
  134.      * Set the Tree Node Id 
  135.      *  
  136.      * @param icon 
  137.      */ 
  138.     public void setId(String id) { 
  139.  
  140.         this.id = id; 
  141.         if (this.attr == null) { 
  142.             attr = new HashMap<String, Object>(); 
  143.         } 
  144.  
  145.         if (this.attr.containsKey("id")) { 
  146.             this.attr.remove("id"); 
  147.         } 
  148.         this.attr.put("id", id); 
  149.     } 
  150.  
  151.     /** 
  152.      * Set the Tree Node State open, closed or leaf 
  153.      *  
  154.      * @param state 
  155.      */ 
  156.     public void setState(String state) { 
  157.         this.state = state; 
  158.     } 
  159.  
  160.     /** 
  161.      * Set the Tree Node Title 
  162.      *  
  163.      * @param title 
  164.      */ 
  165.     public void setTitle(String title) { 
  166.         if (this.data == null) { 
  167.             data = new HashMap<String, Object>(); 
  168.         } 
  169.  
  170.         if (this.data.containsKey("title")) { 
  171.             this.data.remove("title"); 
  172.         } 
  173.         this.data.put("title", title); 
  174.         this.title = title; 
  175.     } 
  176.  
  177.     /** 
  178.      * Set the Tree Node Type 
  179.      *  
  180.      * @param type 
  181.      */ 
  182.     public void setType(String type) { 
  183.         this.type = type; 
  184.     } 
  185.  
  186.     public String getRel() { 
  187.         return rel; 
  188.     } 
  189.  
  190.     public void setRel(String rel) { 
  191.         this.rel = rel; 
  192.     } 
  193.  
  194.     public String getPkg() { 
  195.         return pkg; 
  196.     } 
  197.  
  198.     public void setPkg(String pkg) { 
  199.         this.pkg = pkg; 
  200.     } 
  201.  
  202.     public String getCls() { 
  203.         return cls; 
  204.     } 
  205.  
  206.     public void setCls(String cls) { 
  207.         this.cls = cls; 
  208.     } 
  209.  
  210.     public String getMethod() { 
  211.         return method; 
  212.     } 
  213.  
  214.     public void setMethod(String method) { 
  215.         this.method = method; 
  216.     } 
  217.      
  218.     public String getCalled_cls() { 
  219.         return called_cls; 
  220.     } 
  221.  
  222.     public void setCalled_cls(String called_cls) { 
  223.         this.called_cls = called_cls; 
  224.     } 
  225.  
  226.     public String getCalled_method() { 
  227.         return called_method; 
  228.     } 
  229.  
  230.     public void setCalled_method(String called_method) { 
  231.         this.called_method = called_method; 
  232.     } 
  233.  
  234.     @Override 
  235.     public String toString() { 
  236.         StringBuilder builder = new StringBuilder(); 
  237.         builder.append("TreeNode [id=").append(id).append(", title=").append( 
  238.                 title).append(", icon=").append(icon).append(", state="
  239.                 .append(state).append(", attr=").append(attr).append( 
  240.                         ", children=").append(children).append("]"); 
  241.         return builder.toString(); 
  242.     } 
  243.  
  244.     /** 
  245.      * {"attr":{"id":"pkg:com.taobao.defensor.hello"},"children":null,"data":{"title":"com.taobao.defensor.hello"},"icon":null,"id":"pkg:com.taobao.defensor.hello","state":"closed","title":"com.taobao.defensor.hello","type":null} 
  246.      *  
  247.      * @return 
  248.      */ 
  249.     public String toJsonString() { 
  250.  
  251.         try { 
  252.             JSONObject jo = new JSONObject(); 
  253.             JSONObject attr = new JSONObject(); 
  254.             attr.put("id", id); 
  255.             attr.put("rel", rel); 
  256.             attr.put("pkg", pkg); 
  257.             attr.put("cls", cls); 
  258.             attr.put("method", method); 
  259.             attr.put("called_cls", called_cls); 
  260.             attr.put("called_method", called_method); 
  261.             jo.put("attr", attr); 
  262.              
  263.             jo.put("children", (Object) null); 
  264.  
  265.             JSONObject data = new JSONObject(); 
  266.             data.put("title", title); 
  267.  
  268.             jo.put("data", data); 
  269.  
  270.             jo.put("icon", icon); 
  271.  
  272.             jo.put("id", id); 
  273.  
  274.             jo.put("state", state); 
  275.  
  276.             jo.put("title", title); 
  277.  
  278.             jo.put("type", (Object) null); 
  279.  
  280.             return jo.toString(); 
  281.         } catch (JSONException e) { 
  282.             // TODO Auto-generated catch block 
  283.             e.printStackTrace(); 
  284.         } 
  285.  
  286.         return ""
  287.     } 
  288.      
  289.     public JSONObject toJsonObject() { 
  290.  
  291.         try { 
  292.             JSONObject jo = new JSONObject(); 
  293.             JSONObject attr = new JSONObject(); 
  294.             attr.put("id", id); 
  295.             attr.put("rel", rel); 
  296.             attr.put("pkg", pkg); 
  297.             attr.put("cls", cls); 
  298.             attr.put("method", method); 
  299.             attr.put("called_cls", called_cls); 
  300.             attr.put("called_method", called_method); 
  301.             jo.put("attr", attr); 
  302.              
  303.             jo.put("children", (Object) null); 
  304.  
  305.             JSONObject data = new JSONObject(); 
  306.             data.put("title", title); 
  307.  
  308.             jo.put("data", data); 
  309.  
  310.             jo.put("icon", icon); 
  311.  
  312.             jo.put("id", id); 
  313.  
  314.             jo.put("state", state); 
  315.  
  316.             jo.put("title", title); 
  317.  
  318.             jo.put("type", (Object) null); 
  319.  
  320.             return jo; 
  321.         } catch (JSONException e) { 
  322.             // TODO Auto-generated catch block 
  323.             e.printStackTrace(); 
  324.         } 
  325.  
  326.         return null
  327.     } 

注意这些代码:

 

  
  
  
  
  1. if (showCalledFunction.equals("true")&&(calledMethods != null)&&(!calledMethods.equals(""))) { 
  2.                     nodeM.setState(TreeNode.NODE_STATE_CLOSED); 
  3.                 } else { 
  4.                     nodeM.setState(TreeNode.NODE_STATE_LEAF); 
  5.                 } 

是用来控制文件夹图标的开合,或者是否是叶子节点。

因为是第一次使用jsTree,写代码那会,对jsTree理解的也不透。所以,采用了分析id的值来获取子节点,其实,可以做的更简单,多设置几个请求参数,然后,这些请求参数的值一直跟随树上的节点,这样可以少掉解析id的步骤。

代码是个巨型函数,将就着看。如果你确实看懂了,相信,也就学会如何使用jsTree了。:)

 

你可能感兴趣的:(treenode,jstree,1.0)