packagecom.cn.cs.util;importjava.util.ArrayList;importjava.util.Collections;importjava.util.List;importcom.cn.cs.model.SiteResource;public classTreeUtil {private Listnodes;public TreeUtil(Listnodes) {this.nodes=nodes;
}/*** 创建树
*@return
*/
public ListbuildTree(){
List list = new ArrayList();
//获取最顶级菜单for(SiteResource node : nodes) {if(node.getNodeCode().length()==2) {
TreeMessage tm= newTreeMessage();
tm.setId(Integer.parseInt(node.getId()));
tm.setNodeIndex(Integer.parseInt(node.getNodeIndex().toString()));
tm.setNodeCode(node.getNodeCode());
tm.setText(node.getName());
tm.setChecked(true);
list.add(tm);
}
}
list=getSortChildren(list);for(TreeMessage node : list) {
build(node);
}returnlist;
}/*** 构建权限树
* 获取菜单中的所有子菜单,并添加到父菜单中*/
private voidbuild(TreeMessage node){
List children =getChildren(node);if (!children.isEmpty()) {
node.setChildren(children);for(TreeMessage child : children) {
build(child);
}
}
}/**
* @Description: TODO 获取子节点
* @return*/
private ListgetChildren(TreeMessage node){
List children = new ArrayList();
String nodeCode=node.getNodeCode();for(SiteResource child : nodes) {if(nodeCode.equals(child.getPcode())) {
TreeMessage tm= newTreeMessage();
tm.setId(Integer.parseInt(child.getId()));
tm.setNodeCode(child.getNodeCode());
tm.setNodeIndex(Integer.parseInt(child.getNodeIndex().toString()));
tm.setText(child.getName());
tm.setChecked(true);
Attributes attributes= newAttributes();
attributes.setUrl(child.getUrl());
tm.setAttributes(attributes);
children.add(tm);
}
}returngetSortChildren(children);
}/**
*
* @Title: getChildren
* @Description: TODO 获取排序子节点
* @param node
* @return*/
private List getSortChildren(Listchildren){
ComparatorResource my= newComparatorResource(); //这是一个自定义比较器,按照下标顺序排序
Collections.sort(children,my) ;returnchildren;
}
}