我相信我找到了解决方案。这里是:
1 - 在 tree.xhtml 中,我必须为每个节点类型包含一个带有treeNode元素的树元素。
<p:tree id="treeSingle"
value="#{treeBean.root}"
var="node"
selectionMode="single"
selection="#{treeBean.selectedNode}"
dynamic="true"
style="width:98%;height:100%;">
<p:treeNode type="#{treeBean.LEAF_TYPE}">
<h:outputText value="#{node}" />
</p:treeNode>
<p:treeNode type="#{treeBean.ACTION_TYPE}">
<h:outputText value="#{node}" />
</p:treeNode>
<p:treeNode type="#{treeBean.VALUE_TYPE}">
<h:outputText value="#{node}" />
</p:treeNode>
</p:tree>
2 - 然后在 tree.xhtml 中,我还必须为每个节点类型定义菜单,如下所示:
<p:contextMenu for="treeSingle" nodeType="#{treeBean.LEAF_TYPE}">
<p:menuitem value="View" actionListener="#{treeBean.displaySelectedSingle}" icon="ui-icon-search" update=":infoMessages"/>
<p:menuitem value="Delete" update="treeSingle" actionListener="#{treeBean.deleteNode}" icon="ui-icon-close"/>
</p:contextMenu>
<p:contextMenu for="treeSingle" nodeType="#{treeBean.ACTION_TYPE}">
<p:menuitem value="Execute" actionListener="#{treeBean.execute}" update=":infoMessages"/>
</p:contextMenu>
<p:contextMenu for="treeSingle" nodeType="#{treeBean.VALUE_TYPE}">
<p:menuitem value="Edit" actionListener="#{treeBean.edit}" icon="ui-icon-edit" update=":infoMessages"/>
<p:menuitem value="Delete" update="treeSingle" actionListener="#{treeBean.deleteNode}" icon="ui-icon-close"/>
</p:contextMenu>
3 - 然后在我的 TreeBean 中,我像这样公开了以下属性:
private static final String LEAF_TYPE = "leaf";
private static final String ACTION_TYPE = "action";
private static final String VALUE_TYPE = "value";
public String getLEAF_TYPE(){
return LEAF_TYPE;
}
public String getACTION_TYPE(){
return ACTION_TYPE;
}
public String getVALUE_TYPE(){
return VALUE_TYPE;
}
4 - 最后在 TreeBean 中,我创建了这样的节点:
TreeNode node = new DefaultTreeNode(ACTION_TYPE, data, parentNode);
TreeNode node = new DefaultTreeNode(LEAF_TYPE, data, parentNode);
TreeNode node = new DefaultTreeNode(VALUE_TYPE, data, parentNode);