package application.bean;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import application.utilstool.RegexpTool;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
public class MenuNode {
//private Map<String, Object> fields;
private String parentID = "-1";
private String menuId = "0";
private String menuName = "0";
private String orderById = "";//暂不实现
private String remark = "";
private String icon = ""; //icon图形
private String style = "";//风格,暂不实现
private String show = "1";//是否展示,
private boolean expanded = true;//根节点是否展开
private String path;//点击菜单跳转到fxml,路径
/**
* 菜单字段的格式,其中必须有数字,如菜单id:f_f_2_menuId,2表示是第二层菜单
*/
public static final String menuFieldReg = "^[a-zA-Z_]+_\\d+_[a-zA-Z_]+";
public static final String menuFieldRegLeft = "^[a-zA-Z_]+_";
/**
* 默认菜单字段的格式
*/
public static final String menuFieldRegDefalt = "f_f_";
//子菜单
private List<MenuNode> menuSub = new ArrayList<MenuNode>();
public MenuNode() {
}
public MenuNode(String parentID, String menuId, String menuName, boolean expanded, String icon) {
this.parentID = parentID;
this.menuId = menuId;
this.menuName = menuName;
this.expanded = expanded;
this.icon = icon;
}
public MenuNode(String parentID, String menuId, String menuName, String orderById, String remark, String icon,
String style, List<MenuNode> menuSub) {
super();
this.parentID = parentID;
this.menuId = menuId;
this.menuName = menuName;
this.orderById = orderById;
this.remark = remark;
this.icon = icon;
this.style = style;
this.menuSub = menuSub;
}
public MenuNode(String parentID, String menuId, String menuName, String orderById, String remark, String icon,
String style, String show, boolean expanded, String path) {
super();
this.parentID = parentID;
this.menuId = menuId;
this.menuName = menuName;
this.orderById = orderById;
this.remark = remark;
this.icon = icon;
this.style = style;
this.show = show;
this.expanded = expanded;
this.path = path;
}
/**
* Desc:通过list获取菜单类MenuNode,核心算法,
* @author jld.zhangzhou
* @datetime 2020-03-31 16:05:16
* @modify_record:
* @param muneList
* @param arg arg[0] is pattern '[a-zA-Z_]+_' ,like 'ab_c_d_c_ ',so, the field of list's row must be like 'ab_c_d_c_2_menuId'
* that means menu is of 2th level,and the field's value is related to menuId of class MenuNode;
* @return
* @throws Exception
*/
public static MenuNode getMenu(List<Map<String, Object>> muneList,MenuNode rootMenuNode, String... arg) throws Exception {
MenuNode rootMenu = rootMenuNode;
String menuStartWith = menuFieldRegDefalt;
if(arg.length > 0) {//针对arg有参数,
menuStartWith = arg[0];
if(!RegexpTool.isMatch(menuStartWith, menuFieldRegLeft)) {
throw new Exception(String.format("arg[0] pattern is not '%s'", menuFieldRegLeft) );
}
}
/**
* 1、获取菜单层次
*/
int maxLevel = 0;//菜单最大层数
boolean f_1th_map = true;//第一次遍历muneList标记
for(Map<String, Object> map : muneList) {//遍历数据行(f1_*,f1_*,f2_*,f2_*格式的数据)(后改成f_f_1_*)
if(f_1th_map) {//第一次遍历记录maxLevel
for(Entry<String, Object> entry : map.entrySet()) {
if(!RegexpTool.isMatch(entry.getKey(), menuFieldRegLeft + ".*")) {
continue;
}else {
if(!RegexpTool.isMatch(entry.getKey(), menuFieldReg)) {
throw new Exception(String.format("muneList field about mune %s pattern is not '%s'", entry.getKey(), menuFieldReg));
}
}
String levelStr = RegexpTool.getOneByReg(entry.getKey(), "(?<="+menuStartWith+")\\d+");
int level = Integer.parseInt(levelStr == null ? "0" : levelStr);//获取当前菜单层级。
if(maxLevel < level) {
maxLevel = level;
}
}
f_1th_map = false;
}
int i = 0;
List<MenuNode> enuNodeList_1 = new ArrayList<>();
MenuNode menuNode = null;
String parentId = "";
while(maxLevel > i) {//依次处理 f1,f2,f3,f4...........,该行各个层级的数据,放入rootMenu 对应的位置。
if(i == 0) {
enuNodeList_1 = rootMenu.getMenuSub();
}else {
enuNodeList_1 = menuNode.getMenuSub();
}
/**
* 获取map值。
*/
String parentID = (String) map.get(String.format("%s%d_%s", menuStartWith,i+1,"parentID"));
String menuId = (String) map.get(String.format("%s%d_%s", menuStartWith,i+1,"menuId"));
String menuName = (String) map.get(String.format("%s%d_%s", menuStartWith,i+1,"menuName"));
String orderById = (String) map.get(String.format("%s%d_%s", menuStartWith,i+1,"orderById"));
String remark = (String) map.get(String.format("%s%d_%s", menuStartWith,i+1,"remark"));
String icon = (String) map.get(String.format("%s%d_%s", menuStartWith,i+1,"icon"));
String style = (String) map.get(String.format("%s%d_%s", menuStartWith,i+1,"style"));
String show = (String) map.get(String.format("%s%d_%s", menuStartWith,i+1,"show"));
String path = (String) map.get(String.format("%s%d_%s", menuStartWith,i+1,"path"));
boolean expanded = (boolean) (map.get(String.format("%s%d_%s", menuStartWith,i+1,"expanded")) == null ? false :map.get(String.format("%s%d_%s", menuStartWith,i+1,"expanded")));
if(menuId == null) {
break;
}
if(enuNodeList_1.size() == 0) {
menuNode = new MenuNode(parentID == null ? "-1" : parentID, menuId == null ? "0" : menuId, menuName == null ? "0" : menuName, orderById == null ? "" : orderById,
remark == null ? "" : remark, icon == null ? "" : icon, style == null ? "" : style, show==null ? "1" : show, expanded, path);
enuNodeList_1.add(menuNode);
}else {
//如果size>0,且相等于当前,则menuNode重新指向;如果size=0且不相等,则新建。
boolean f_sizem0e = false;//flag
for(MenuNode m : enuNodeList_1) {
if(m.getMenuId().equals(menuId)) {
menuNode = m;
f_sizem0e = true;
break;
}
}
if(!f_sizem0e) {
menuNode = new MenuNode(parentID == null ? "-1" : parentID, menuId == null ? "0" : menuId, menuName == null ? "0" : menuName, orderById == null ? "" : orderById,
remark == null ? "" : remark, icon == null ? "" : icon, style == null ? "" : style, show==null ? "1" : show, expanded, path);
enuNodeList_1.add(menuNode);
}
}
enuNodeList_1 = menuNode.getMenuSub();//重新指向下一个menuNode
i++;
}
}
return rootMenu;
}
public static MenuNode getMenu(List<Map<String, Object>> muneList, String... arg) throws Exception {
return MenuNode.getMenu(muneList, new MenuNode(), arg);
}
/**
* Desc:get treeview by MenuNode
* @author jld.zhangzhou
* @datetime 2020-04-13 11:03:01
* @modify_record:
* @param menuNode
* @return
*/
public static TreeView<String> getMenuTreeview(MenuNode menuNode) {
if(menuNode == null) {
return null;
}
TreeItem<String> rootNode = new TreeItem<String>(menuNode.getMenuName());
rootNode.setExpanded(menuNode.getExpanded());
MenuNode.setMenuNodeToTreeItem(rootNode, menuNode);
TreeView<String> tree = new TreeView<> (rootNode);
return tree;
}
/**
* Desc:get treeview by muneList
* @author jld.zhangzhou
* @datetime 2020-04-13 16:53:39
* @modify_record:
* @param muneList
* @return
* @throws Exception
*/
public static TreeView<String> getMenuTreeview(List<Map<String, Object>> muneList) throws Exception {
return MenuNode.getM
javafx treeview 树形菜单渲染公用接口
需积分: 27 136 浏览量
2020-05-23
16:13:15
上传
评论
收藏 944KB 7Z 举报
ErnestZZ
- 粉丝: 7
- 资源: 2
最新资源
- note test a bc d e f g h
- MERGE.csv.zip
- 15白落梅:你是锦瑟我为流年:三毛的万水千山-3491776.mobi
- 微信小程序源码 实现 城市切换 demo 根据城市首字母排序城市 选择城市
- 12因为懂得 所以慈悲-张爱玲的倾城往事 (白落梅经典散文系列)-3321089.mobi
- 基于 C# 实现的串口温度监控软件
- 基于STM32F103RBT6单片机+SIM868自动收货机主控板硬件(原理图+PCB)工程文件.zip
- 微信小程序源码 车源宝 二手车交易平台 源码下载
- 专注于在线教育领域,打造更好的网课系统,网校系统,知识付费系统
- ADM487EARZ-REEL7 产品规格书pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈