With the stack (stack) to achieve tree menu

sponsored links
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;
)
)
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of With the stack (stack) to achieve tree menu

  • ajax implementation struts2.1 communication

    First of all go download http://code.google.com/p/jsonplugin/downloads/list first Also rememberPlus buried strtus lib inside the official jar on the json packet Then add the test action Pages jqurey Medium struts.xml add And try

  • DWR Demo

    Dwr.jar download under the lib on Medium in web.xml add the following code: Category deal with the preparation of background: DwrTest.java and the introduction of category DwrTestForm.java Add dwr.xml with the preparation of a web.xml directory: Prep ...

  • struts2 ajax verify the existence of input values

    struts2 ajax verify the existence of input values Today to do the struts2 ajax validate input regarding the existence of the function, and now share with everyone, I hope everyone many opinions!!! input.jsp page code: <html> <body> <s: ...

  • Hibernate access picture sample

    General web users to upload picture at treatment would normally uses two types of strategies: First, put directly into the picture in the database Blob field; II database are stored only at the picture on the path of the server information?, Pictures stor

  • hibernate query cache

    hibernate query cache Query cache is cached result set of common property On the entity object cache only the result set id Query cache life cycle, when the associated table happened to amend, then the query cache of the life cycle of The End Query cache

  • hibernate how to store binary, image and other major fields.

    model categories: reportByte binary type for the database fields. public class PfReportStyle implements java.io.Serializable , Cloneable { // Fields /** * */ private static final long serialVersionUID = 1L; private Long id; private byte[] reportByte; // C

  • hibernate study of the second

    Persistence of three main points: 1, a statement for persistent fields accessors (accessors) and whether the variable signs (mutators) Property statement is not necessarily required for the public's. Hibernate can be default, protected or private ...

  • hibernate (jpa) composite primary key annotation statement Ways

    In the design of the database tables are designed with a composite primary key of the table, that table's record by more than one field joint identification, such as: Table CREATE TABLE TB_HOUR_DATA ( STAT_DATE DATE NOT NULL, PATH_ID NUMBER(20) NOT NULL,

  • jboss ejb3 Message Driven Bean

    Super Medium ejb hate. . . . . . . . . . . . . . . . . . . ================================================ To configure a Message Driven Bean in a different application server parameters are not the same. Currently only passed the test jboss. Message Dri

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries