With the stack (stack) to achieve tree menu

1. A sort of menu:

/ **
* Tree Menu: sibling is orderly, and at the same level directory's position in ascending order according to Menu
*
* @ 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); / / Sort the same level directory
allMenuMap.put (md.getMemo (), node);
)
parent = node;
)
)
return root;
)

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

private String showTree () (/ / menu permissions
Set <Long> sonMenus = new HashSet <Long> ();// assume that the menu has competence
/ / SonMenus.add (100L); / / only with url's url will be able to without the parent node is displayed
sonMenus.add (113L);
sonMenus.add (101L);
sonMenus.add (102L);
sonMenus.add (104L);

sonMenus.add (120L); / / 3 directory
sonMenus.add (122L); / / 4 directory

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>");
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 ("")) (/ / that directory
sb.append ("<div>");
sb.append ("<h1>" + dto.getMenuName () + "</ h1> \ n");
) Else (
sb.append ("<div> <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. Tree node:

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 ("wrong tree");
)

int index = parent.getChildren (). indexOf (this); / / indexof return to the list of specified elements of the index the first time, that is the first level subdirectory
if (index == -1) (
throw new IllegalArgumentException ("wrong tree");
) 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. Comparators:

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

public int compare (Menu m, Menu e) (

return m.getPosition (). compareTo (e.getPosition ());// Ascending
)

)

4. Menu:

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;
)
)

分类:Java 时间:2010-09-16 人气:201
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.208 (s). 12 q(s)