用栈(stack)实现树形菜单

1.具有排序的菜单:

/**
 * 树形菜单:兄弟结点是有序的,同级目录按Menu的position升序排列
 *
 * @author Administrator
 *
 */
public class StackOrderTree {

 private Map<Long, Menu> menuMap = InitBean.getMenuMap();
 private static MenuComparator<Menu> mc = new MenuComparator<Menu>();

 private Node<Menu> createMenuTree(Set<Long> sonMenus) {
  Map<String, Node<Menu>> allMenuMap = new HashMap<String, Node<Menu>>();
  Node<Menu> root = new Node<Menu>(null);
  Node<Menu> parent = null;
  Node<Menu> node = null;

  for (Long e : sonMenus) {
   Menu menu = menuMap.get(e);
   Stack<Menu> stack = new Stack<Menu>();
   while (menu != null) {
    stack.push(menu);
    if (menu.getParent() == null) {
     menu = null;
    } else {
     menu = menuMap.get(menu.getParent().getId());
    }
   }

   parent = root;
   while (!stack.isEmpty()) {
    Menu md = stack.pop();
    if (parent.containsTheChild(md)) {
     node = parent.getTheChild(md);
    } else {
     node = new Node<Menu>(md, parent, mc); //同级目录排序
     allMenuMap.put(md.getMemo(), node);
    }
    parent = node;
   }
  }
  return root;
 }

 public void setMenuMap(Map<Long, Menu> menuMap) {
  this.menuMap = menuMap;
 }

 private String showTree() {//菜单权限
  Set<Long> sonMenus = new HashSet<Long>();//假设拥有权限的菜单
  // sonMenus.add(100L); //只需要带url的就能把不带url的父结点显示出来
  sonMenus.add(113L);
  sonMenus.add(101L);
  sonMenus.add(102L);
  sonMenus.add(104L);

  sonMenus.add(120L); // 三级目录
  sonMenus.add(122L); // 四级目录

  Node<Menu> root = createMenuTree(sonMenus);
  StringBuilder sb = new StringBuilder();

  for (int i = 0; i < root.getChildren().size(); i++) {
   Node<Menu> node = root.getChildren().get(i);
   String selectStr = "";
   sb.append("<h3 " + selectStr
     + "><span><img src='/images/a.jpg' alt='"
     + node.getData().getMenuName() + "' /></span>"
     + node.getData().getMenuName() + "</h3>\n");

   sb.append("<div class='list_menu'>");
   List list = node.getChildren();
   for (Node<Menu> son : node.getChildren()) {
    createMenuHtml(sb, son);
   }
   sb.append("</div>\n");
  }
  return sb.toString();
 }

 private void createMenuHtml(StringBuilder sb, Node<Menu> root) {
  Menu dto = (Menu) root.getData();
  if (dto.getMenuUrl().equals("")) {// 表示目录
   sb.append("<div class='list_menu'>");
   sb.append("<h1>" + dto.getMenuName() + "</h1>\n");
  } else {
   sb.append("<div class='shortcut_sub'><a target='mainframe' href='"
     + dto.getMenuUrl() + "'>" + dto.getMenuName()
     + "</a></div>\n");
  }

  for (Node<Menu> node : root.getChildren()) {
   createMenuHtml(sb, node);
   if (node.isLast())
    sb.append("</div>");
  }
 }

 public static void main(String args[]) {
  StackOrderTree st = new StackOrderTree();
  String s = st.showTree();
  System.out.println(s);
 }
}

2.树形结点:

public class Node<T> {

 private T data;
 private Node<T> parent;
 private List<Node<T>> children=new ArrayList<Node<T>>();
 
 public Node(T data){
  this.data=data;
 }
 
 public Node(T data,Node<T> parent){
  this.data=data;
  this.parent=parent;
  
  parent.addChild(this);
 }
 
  public void addChild(Node<T> node) {
         if(node != null) {
             node.parent = this;
             children.add(node); 
         }
     }
 
  public Node(T data, Node<T> parent, Comparator<T> comparator) {
         this.data = data;
         this.parent = parent;

         parent.addChild(this, comparator);
     }   

     public void addChild(Node<T> node, Comparator<T> comparator) {
      if(node != null) {
             node.parent = this;
            
             boolean insert = false;
             for(int i = 0; i < children.size(); i++) {
              if(comparator.compare(node.getData(), children.get(i).getData()) == -1) {
               children.add(i, node);
               insert = true;
               break;
              }
             }
            
             if(!insert) {
              children.add(node);
             }
         }
     }
 
 public boolean containsTheChild(T data) {
     return getTheChild(data) != null;
    }
 
  public Node<T> getTheChild(T data) {
      Node<T> child = null;
      if(hasChild()) {
       for(Node<T> node : children) {
        if(data.equals(node.getData())) {
         child = node;
         break;
        }
       }
      }
      
      return child;
   }
  public boolean isLeaf() {
         return children == null || children.isEmpty();
     }

  public boolean hasChild() {
         return !isLeaf();
  }
 
  public boolean isLast() {
         if(parent == null) {
             return true; //root node
         } else {
             if(parent.getChildren() == null || parent.getChildren().isEmpty()) {
                 throw new IllegalArgumentException("错误的树结构");
             }
            
             int index = parent.getChildren().indexOf(this);//indexof 返回列表中首次出现指定元素的索引,也就是第一级子目录
             if(index == -1) {
                 throw new IllegalArgumentException("错误的树结构");
             } else {
                 return index == (parent.getChildren().size() - 1);
             }
         }
 }
 
 public T getData() {
  return data;
 }
 public void setData(T data) {
  this.data = data;
 }
 public Node<T> getParent() {
  return parent;
 }
 public void setParent(Node<T> parent) {
  this.parent = parent;
 }
 public List<Node<T>> getChildren() {
  return children;
 }
 
}

3.比较器:

public class MenuComparator<T extends Menu> implements Comparator<T> {

 public int compare(Menu m, Menu e) {

  return m.getPosition().compareTo(e.getPosition());// 升序
 }

}

4.菜单:

public class Menu {

 private Long id;

 private String menuName;

 private String memo;

 private Menu parent;

 private String menuUrl;

 private Long position;

 public Menu(Long id, String menuName, String memo, Long position,
   Menu parent, String menuUrl) {
  this.id = id;
  this.menuName = menuName;
  this.memo = memo;
  this.position = position;
  this.parent = parent;
  this.menuUrl = menuUrl;
 }

 public String getMenuName() {
  return menuName;
 }

 public void setMenuName(String menuName) {
  this.menuName = menuName;
 }

 public String getMemo() {
  return memo;
 }

 public void setMemo(String memo) {
  this.memo = memo;
 }

 public Menu getParent() {
  return parent;
 }

 public void setParent(Menu parent) {
  this.parent = parent;
 }

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public void setMenuUrl(String menuUrl) {
  this.menuUrl = menuUrl;
 }

 public String getMenuUrl() {
  return menuUrl;
 }

 public void setPosition(Long position) {
  this.position = position;
 }

 public Long getPosition() {
  return position;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值