tomahawk拡張タグ tree2
まずfaces-config.xml内のバッキングビーン設定を忘れないこと!
showNav="false"でナビアイコンなしに。
binding="#{treeBacker.tree}"+入力フォーム設置でファイル直接指定。
showRootNode="false"で最上位フォルダ非表示(?)。
>>JSP部分ソース(tree.jsp) <%@ page contentType="text/html; charset=Shift_JIS"%> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" /> <title></title> </head> <body> <f:view><h:form> <t:tree2 id="clientTree" value="#{treeBacker.treeData}" var="node" varNodeToggler="t"> <f:facet name="section"> <h:panelGroup> <f:facet name="expand"> <t:graphicImage value="images/yellow-folder-open.png" rendered="#{t.nodeExpanded}" border="0" /> </f:facet> <f:facet name="collapse"> <t:graphicImage value="images/yellow-folder-closed.png" rendered="#{!t.nodeExpanded}" border="0" /> </f:facet> <h:outputText value="#{node.description}" styleClass="nodeFolder" /> </h:panelGroup> </f:facet> <f:facet name="person"> <h:panelGroup> <f:facet name="expand"> <t:graphicImage value="images/yellow-folder-open.png" rendered="#{t.nodeExpanded}" border="0" /> </f:facet> <f:facet name="collapse"> <t:graphicImage value="images/yellow-folder-closed.png" rendered="#{!t.nodeExpanded}" border="0" /> </f:facet> <h:outputText value="#{node.description}" styleClass="nodeFolder" /> <h:outputText value=" (#{node.childCount})" styleClass="childCount" /> </h:panelGroup> </f:facet> </t:tree2> </h:form></f:view> </body> </html>
>>バッキングビーン(TreeBacker.java)ほぼサンプルコピペ。 import org.apache.myfaces.custom.tree2.HtmlTree; import org.apache.myfaces.custom.tree2.TreeNode; import org.apache.myfaces.custom.tree2.TreeNodeBase; import org.apache.myfaces.custom.tree2.TreeModel; import org.apache.myfaces.custom.tree2.TreeModelBase; import javax.faces.context.FacesContext; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.validator.ValidatorException; import javax.faces.event.ActionEvent; import java.io.Serializable; public class TreeBacker implements Serializable { private static final long serialVersionUID = 1L; private TreeModelBase _treeModel; private HtmlTree _tree; public TreeNode getTreeData() { TreeNode treeData = new TreeNodeBase("section", "会社", false); TreeNodeBase personNode = new TreeNodeBase("section", "部", false); TreeNodeBase folderNode = new TreeNodeBase("section", "メンバー", false); folderNode.getChildren().add(new TreeNodeBase("person", "ひとりめ", true)); folderNode.getChildren().add(new TreeNodeBase("person", "ふたりめ", true)); folderNode.getChildren().add(new TreeNodeBase("person", "さんにんめ", true)); personNode.getChildren().add(folderNode); treeData.getChildren().add(personNode); return treeData; } public TreeModel getExpandedTreeData() { return new TreeModelBase(getTreeData()); } public void setTree(HtmlTree tree) { _tree = tree; } public HtmlTree getTree() { return _tree; } public String expandAll() { _tree.expandAll(); return null; } private String _nodePath; public void setNodePath(String nodePath) { _nodePath = nodePath; } public String getNodePath() { return _nodePath; } public void checkPath(FacesContext context, UIComponent component, java.lang.Object value) { // make sure path is valid (leaves cannot be expanded or renderer will complain) FacesMessage message = null; String[] path = _tree.getPathInformation(value.toString()); for (int i = 0; i < path.length; i++) { String nodeId = path[i]; try { _tree.setNodeId(nodeId); } catch (Exception e) { throw new ValidatorException(message, e); } if (_tree.getNode().isLeaf()) { message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid node path (cannot expand a leaf): " + nodeId, "Invalid node path (cannot expand a leaf): " + nodeId); throw new ValidatorException(message); } } } public void expandPath(ActionEvent event) { _tree.expandPath(_tree.getPathInformation(_nodePath)); } }
こんな感じになった。
tree.jsp内の"#{node.description}"あたりがエラー判定になっているが、
実際にはブラウザ上でちゃんと表示されていてなぞぃ。
なぜか#{node.childCount}はエラーと言われてないあたりも不思議。