📄 firsttree3.html
字号:
<html jwcid="@Shell" title="Using The Tapestry Tree Control" stylesheet="ognl:assets.stylesheet"><head jwcid="@Block"> <link rel="stylesheet" type="text/css" href="../css/style.css"/></head><body jwcid="@Body"> <h1>Performing an action when a node is clicked</h1> <div class="note"> This page demonstrates how to use a TreeStateListener to respond when a node is clicked. </div> <table border="1"> <tr> <td valign="top"> <span class="tree" jwcid="treeView"> <span jwcid="treeDataView"> <span jwcid="treeNodeView"/> </span> </span> </td> </tr> <tr> <td colspan="4"> <code class="highlight"><span jwcid="@Insert" value="ognl:lastNodeSelectedMsg"/></code> <td> </tr> </table> <p> Here is a snippet from FirstTree3.hmtl that adds a dynamic string to the page to indicate the last node that was selected:<div class="code"><pre> <table border="1"> <tr> <td valign="top"> <span class="tree" jwcid="treeView"> <span jwcid="treeDataView"> <span jwcid="treeNodeView"/> </span> </span> </td> </tr> <tr> <td colspan="4"><code class="highlight"> <code class="highlight"> <span jwcid="@Insert" value="ognl:lastNodeSelectedMsg"/> </code></code> <td> </tr> </table></pre></div> <p> The TreeView's TreeStateListener is notified any time a node of the tree is selected. In our case the page is the TreeStateListener. This was set up on <code>FirstTree3.page</code> in the following snippet:<div class="code"><pre> <component id="treeView" type="contrib:TreeView"> <binding name="treeModel" expression='treeModel'/> <code class="highlight"><binding name="treeStateListener" expression='treeStateListener'/></code> </component></pre></div> Here are some code snippets from <code>FirstTreePage.java</code> that demonstrate how to respond to events from the tree:<div class="code"><pre> public class FirstTreePage extends BasePage implements <code class="highlight">ITreeStateListener</code> { public ITreeStateListener getTreeStateListener() { return this; } /* * @see org.apache.tapestry.contrib.tree.model.ITreeStateListener#treeStateChanged(org.apache.tapestry.contrib.tree.model.TreeStateEvent) */ public void treeStateChanged(TreeStateEvent objEvent) { // Get the StringTreeNode object that was selected TreeView treeView = (TreeView)getComponent("treeView"); if(treeView != null){ ITreeDataModel thisTreeDataModel = treeView.getTreeModel().getTreeDataModel(); Object selectedNode = thisTreeDataModel.getObject(objEvent.getNodeUID()); if(selectedNode instanceof StringTreeNode){ lastNodeSelectedValue = ((StringTreeNode)selectedNode).getValue(); } } else { // This shouldn't happen lastNodeSelectedValue = objEvent.getNodeUID().toString(); } } String lastNodeSelectedValue; public String getLastNodeSelectedMsg() { if(lastNodeSelectedValue==null){ return null; } return lastNodeSelectedValue + " was the last node selected."; }</pre></div><p> As you can see, in the <code>TreeStateChanged</code> event handler it is fairly easy to get the ITreeNode that was selected. With that information you can do whatever is appropriate for your application.<p><a href="#" jwcid="@PageLink" page="FirstTree4">Changing the open and close icons...</a><p><a href="#" jwcid="@PageLink" page="Home">Return to Home Page...</a></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -