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

相关文章

  • css to achieve the directory tree navigation menu 2010-03-20

    <! DOCTYPE html PUBLIC "- / / W3C / / DTD XHTML 1.0 Transitional / / EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <Html xmlns = " http://www.w3.org/1999/xhtml "> <head> <meta http-equi

  • Infinite tree drop-down menu ---- 2010-10-12

    Write your own tree drop-down menu, you can repeat with infinite level. Directly copied the entire file here, copy and paste can be observed directly effects. Oh, I remember that reference an external js libraries. <! DOCTYPE html PUBLIC "- / / W3

  • Simple multi-level tree navigation menu 2010-03-27

    Simple multi-level tree navigation menu <ul> <li> <a href="#"> My Favorites </ a> <ul> <li> <a href="#"> personal website </ a> <ul> <li> <a href = " http://www.butong

  • public void testStatementAddBatch () public void testConnCommit () 2011-09-01

    public void testStatementAddBatch() { Connection conn = null; Statement stmt = null; try { conn = getDBConnectionMsSqlBySuperAdmin(); stmt = conn.createStatement(); for(int i = 0; i < 100; i++) { String sql = " sql " + i; stmt.addBatch(sql);

  • web.xml is the file loading process, each node load order summary 2009-07-22

    Today, 2010-3-11 day, to go to work without incident, want to come to the project described in web.xml file loading process to be a summary posted on here to Memo: web.xml loading process (step): 1. Start WEB item, the container (eg: Tomcat) will rea

  • How to TWaver Node object in the right menu item added 2011-05-04

    Select the tree node, an extra button, you can use tree.iconsComponentsFunction (use reference AlarmMappingDemo). Menu with Menu.createMenu (use reference Flex how to customize the Menu's Icon) If you right-click the tree node, the node selected for

  • extJs asynchronous tree while asynchronous start two nodes, return to the child node display disorder? 2010-05-07

    extjs Asynchronous tree: I set the tree two nodes expanded = true, while asynchronous start two nodes, the results returned to the child nodes are the result of the second asynchronous request. Will you heroes how to solve ah? Meet the following two

  • To the tree with right-click menu, and the realization of the corresponding menu functions 2010-03-19

    15.4 to the tree with right-click menu, and the realization of the corresponding menu functions Examples of the effect of this section, as shown in Figure 15.5. TreeViewer right-click menu to add the methods and TableViewer similar to, but also used

  • Used in the picture gallery here SDCARD 2010-05-04

    The same as the List Gallery, also can be displayed in tabular form, which appears in the central level. (Vertical do not know if is said to modify the source was.). Gallery can be found on the network almost always removed from the APIDEMOS years, o

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

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