// 用新树的根节点替换旧树的根节点 public static UITree replaceRootTreeNode(UITree newTree, UITree oldTree) { UITreeNode newRootNode = null; for (UIComponent child : newTree.getChildren()) { if (child instanceof UITreeNode) { newRootNode = (UITreeNode) child; break; } } if (newRootNode != null) { for (UIComponent child : oldTree.getChildren()) { if (child instanceof UITreeNode) { oldTree.getChildren().remove(child); break; } } oldTree.getChildren().add(newRootNode); } return oldTree; }
private static Integer getIndexParam(FacesContext context, UITree tree) { Map<String, Object> sessionmap = context.getExternalContext().getSessionMap(); String nodeIndexParam = tree.getClientId(context) + "_" + NODE_INDEX_KEY; if (sessionmap.get(nodeIndexParam) == null) { sessionmap.put(nodeIndexParam, 1); return 0; } else { int index = Integer.parseInt(sessionmap.get(nodeIndexParam).toString()); sessionmap.put(nodeIndexParam, index + 1); return index; } }
// 根据用户绑定的TreeDataProvider以及tree的一些属性来生成组件树 public static void buildNodesByCondition( UITree tree, TreeDataProvider dataProvider, UIComponent parent, Object parentUserData, Boolean expandAll) { Object[] children = dataProvider.getChildren(parentUserData); if (children == null) { return; } for (Object child : children) { UITreeNode newNode = new UITreeNode(); newNode.setUpdateAble(true); newNode.setId(TreeRenderHelper.createUniqueTreeNodeId(tree)); newNode.setText(dataProvider.getText(child)); String icon = dataProvider.getIcon(child); if (icon != null) { newNode.setIcon(icon); } else { newNode.setIconCls(TREE_NODE_BLANK_IMG_CLASS); } newNode.setChecked(dataProvider.isChecked(child)); newNode.setExpanded(dataProvider.isExpanded(child)); newNode.setCascade(dataProvider.isCascade(child)); newNode.setHref(dataProvider.getHref(child)); newNode.setHrefTarget(dataProvider.getHrefTarget(child)); newNode.setUserData(child); Object[] childChildren = dataProvider.getChildren(child); if (childChildren == null || childChildren.length == 0) { newNode.setLeaf(true); } else { newNode.setLeaf(false); } if (parent instanceof UITree) { ((UITree) parent).add(newNode); } if (parent instanceof UITreeNode) { ((UITreeNode) parent).add(newNode); } if (expandAll) { buildNodesByCondition(tree, dataProvider, newNode, child, expandAll); } } }
// 保存树组件的状态 public static void saveComponentTree(UITree tree) { FacesContext context = FacesContext.getCurrentInstance(); Map<String, Object> sessionmap = context.getExternalContext().getSessionMap(); sessionmap.put(tree.getClientId(context) + DELEGATE_KEY, tree); }
// 创建树组件上唯一的id public static String createUniqueTreeNodeId(UITree tree) { FacesContext context = FacesContext.getCurrentInstance(); return tree.getId() + "_" + getIndexParam(context, tree).toString(); }