MenuItem.java
package com.example.menu;
import java.util.ArrayList;
import java.util.List;
public class MenuItem {
private String name;
private List<MenuItem> children;
private int level;
public MenuItem(String name,int level) {
this.name = name;
this.level = level;
children = new ArrayList<MenuItem>();
}
public void addChild(MenuItem node) {
children.add(node);
}
public void appendChild(MenuItem child) {
if (children == null)
children = new ArrayList<MenuItem>();
children.add(child);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<MenuItem> getChildren() {
return children;
}
public void setChildren(List<MenuItem> children) {
this.children = children;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
MenuItemData.java
package com.example.menu;
import java.util.ArrayList;
import java.util.List;
public class MenuItemData {
private static List<MenuItem> menus = new ArrayList<MenuItem>();
static {
MenuItem m1 = new MenuItem("Administration",1);
MenuItem m1_lv1 = new MenuItem("Security",2);
MenuItem m1_Lv2 = new MenuItem("Accounts",3);
MenuItem m1_Lv3 = new MenuItem("User",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Role",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("User Rights",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Role Rights",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1_Lv2 = new MenuItem("Regional",3);
m1_Lv3 = new MenuItem("Clock",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Language",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Keyboard",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1_Lv2 = new MenuItem("Devices",3);
m1_Lv3 = new MenuItem("Printer",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Projector",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Mouse",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1.addChild(m1_lv1);
/***************************************************/
m1_lv1 = new MenuItem("Network",2);
m1_Lv2 = new MenuItem("Wireless",3);
m1_Lv3 = new MenuItem("Connection 1",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Connection 2",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1_Lv2 = new MenuItem("Adapter",3);
m1_Lv3 = new MenuItem("Local Area Connection",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Virtual Box Network",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1.addChild(m1_lv1);
/***************************************************/
m1_lv1 = new MenuItem("My Computer",2);
m1_Lv2 = new MenuItem("Drives",3);
m1_Lv3 = new MenuItem("C Drive",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("D Drive",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("E Drive",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1_Lv2 = new MenuItem("Favorites",3);
m1_Lv3 = new MenuItem("Desktop",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Downloads",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Recent Places",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Google Drive",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1.addChild(m1_lv1);
menus.add(m1);
/************************************************************************/
m1 = new MenuItem("ZK",1);
m1_lv1 = new MenuItem("Products",2);
m1_Lv2 = new MenuItem("ZK SpreadSheet",3);
m1_Lv3 = new MenuItem("3D Cell",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Freeze rows",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Ranged Cells",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1_Lv2 = new MenuItem("ZK Pivottable",3);
m1_Lv3 = new MenuItem("Drill Down",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Render",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Paging",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1_Lv2 = new MenuItem("ZK Calender",3);
m1_Lv3 = new MenuItem("Views",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Drag and Drop",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Time Zones",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1_Lv2 = new MenuItem("ZK Spring",3);
m1_Lv3 = new MenuItem("HTTP Request",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("HTTP Basic",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("MD4 Password",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1.addChild(m1_lv1);
/***************************************************/
m1_lv1 = new MenuItem("ZK Demo",2);
m1_Lv2 = new MenuItem("Grid",3);
m1_Lv3 = new MenuItem("Master Detail",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Data Binding",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Dynamic Data",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Data Filter",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1_Lv2 = new MenuItem("ListBox",3);
m1_Lv3 = new MenuItem("Dual ListBox",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Paging",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Auto Sort",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("List Group",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv2 = new MenuItem("Effects",3);
m1_Lv3 = new MenuItem("jQuery Effects",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Upload Effect",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Login Effect",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Display Action",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv2 = new MenuItem("Layout",3);
m1_Lv3 = new MenuItem("Portal Layout",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Complex Border Layout",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Group Box",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("Boxes",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1.addChild(m1_lv1);
/***************************************************/
m1_lv1 = new MenuItem("ZK Support",2);
m1_Lv2 = new MenuItem("Documentation",3);
m1_Lv3 = new MenuItem("Spread Sheet",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("ZK Calender",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("ZK Studio",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1_Lv2 = new MenuItem("Downloads",3);
m1_Lv3 = new MenuItem("ZK Spring",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("ZK JSP",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1.addChild(m1_lv1);
menus.add(m1);
m1 = new MenuItem("Eclipse",1);
menus.add(m1);
m1_lv1 = new MenuItem("File",2);
m1_Lv2 = new MenuItem("Project",3);
m1_Lv3 = new MenuItem("New Maven",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("New JPA",4);
m1_Lv2.addChild(m1_Lv3);
m1_Lv3 = new MenuItem("New JEE",4);
m1_Lv2.addChild(m1_Lv3);
m1_lv1.addChild(m1_Lv2);
m1.addChild(m1_lv1);
}
public static List<MenuItem> getAllMenus() {
return new ArrayList<MenuItem>(menus);
}
}
MyTreeModel.java
package com.example.menu;
import org.zkoss.zul.AbstractTreeModel;
import org.zkoss.zul.ext.Selectable;
public class MyTreeModel extends AbstractTreeModel<Object> implements Selectable<Object>{
private static final long serialVersionUID = 1L;
private MenuItem _root;
public MyTreeModel(Object root) {
// set the root
super(root);
_root = (MenuItem) root;
}
@Override
public boolean isLeaf(Object node) {
return ((MenuItem) node).getChildren().size() == 0; // at most 4 levels
}
@Override
public Object getChild(Object parent, int index) {
return ((MenuItem) parent).getChildren().get(index);
}
@Override
public int getChildCount(Object parent) {
return ((MenuItem) parent).getChildren().size();
}
public boolean isMutiple()
{
return true;
}
}
index.zul
<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<zk>
<window id="mainmenubar" apply="org.zkoss.bind.BindComposer"
border="none" onCreate="@command('onFulfill')"
viewModel="@id('vm') @init('com.example.menu.TreeMenuVM')">
<tree model="@bind(vm.model)" id="mytree"
zclass="z-filetree" rows="22"
height="auto" >
<template name="model" var="node" status="s">
<treeitem open="true">
<treerow>
<treecell label="@bind(node.name)" />
</treerow>
</treeitem>
</template>
</tree>
</window>
</zk>
TreeMenuVM.java
package com.example.menu;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Tree;
import org.zkoss.zul.TreeModel;
import org.zkoss.zul.Treeitem;
public class TreeMenuVM {
@Wire("#mytree")
private Tree mytree;
TreeModel _model;
private List<MenuItem> allMenus;
public TreeModel getModel() {
if (_model == null) {
MyTreeModel a = new MyTreeModel(getRoot());
a.setMultiple(true);
_model = a;
}
return _model;
}
@AfterCompose
public void initSetup(@ContextParam(ContextType.VIEW) Component view) {
Selectors.wireComponents(view, this, false);
mytree.setFocus(true);
}
public MenuItem getRoot() {
MenuItem superRoot = new MenuItem("Menu", 0);
MenuItem root = new MenuItem("Menu", 0);
allMenus = MenuItemData.getAllMenus();
MenuItem m1 = allMenus.get(0);
MenuItem m2 = allMenus.get(1);
MenuItem m3 = allMenus.get(2);
superRoot.addChild(root);
root.addChild(m1);
root.addChild(m2);
root.addChild(m3);
return superRoot;
}
@Command
public void onFulfill() {
doCollapseExpandAll(mytree, true);
}
public static void doCollapseExpandAll(Component component,
boolean aufklappen) {
if (component instanceof Treeitem) {
Treeitem treeitem = (Treeitem) component;
treeitem.setOpen(aufklappen);
}
Collection<?> com = component.getChildren();
if (com != null) {
for (Iterator<?> iterator = com.iterator(); iterator.hasNext();) {
doCollapseExpandAll((Component) iterator.next(), aufklappen);
}
}
}
}
Output:
Please check ZK 7 Project Start-up Kit – Dynamic Menu
No comments:
Post a Comment