title: What event should be used on a p:tree to select a tree node and have a context menu?
body: <p>I'm creating a PrimeFaces (5.3) tree with a context menu. Selected nodes should be stored in <code>#{myBean.selectedNode}</code>. When I select a node using the left mouse button the correct node is set. But, when I try to run an action on a node from a context menu, <strong>without selecting it first</strong>, the correct node isn't set (the setter in my bean is not called).</p>
  
  <p>I'm following the <a href="http://primefaces.org/showcase/ui/data/tree/contextMenu.xhtml" rel="nofollow">example in the PrimeFaces showcase</a>. As you can see, in the PrimeFaces showcase you are able to immediately right click a node, click "View", and the growl will display the correct node.</p>
  
  <p>Here is my setup:</p>
  
  <h2>Bean</h2>
  
  <p>It is <code>ViewScoped</code> and there is a <code>private TreeNode selectedNode</code> with getter and setter.</p>
  
  <p>Here are the interesting bits:</p>
  
  <pre><code>public void onNodeSelect(NodeSelectEvent event) {
      MyTreeNode myTreeNode = (MyTreeNode) event.getTreeNode();
      myController.setSelected(myTreeNode.getEntity());
  }
  
  public void addChild(String name) {
      MyTreeNode myTreeNode = (MyTreeNode) selectedNode;
      MyTreeNode childNode = myTreeNode.addChild(name);
      myController.setSelected(childNode.getEntity());
      myController.insert();
  }
  </code></pre>
  
  <h2>XHTML</h2>
  
  <pre><code>&lt;h:form id="mainForm"&gt;
      &lt;p:tree value="#{myBean.root}" var="node"
              id="myTree" dynamic="true"
              selectionMode="single" selection="#{myBean.selectedNode}"&gt;
          &lt;p:treeNode expandedIcon="ui-icon-folder-open" collapsedIcon="ui-icon-folder-collapsed"
                      type="myType"&gt;
              &lt;h:outputText value="#{node}"/&gt;
          &lt;/p:treeNode&gt;
          &lt;p:ajax event="select" listener="#{myBean.onNodeSelect}" /&gt;
      &lt;/p:tree&gt;
  
      &lt;p:contextMenu for="myTree"&gt;
          &lt;p:menuitem action="#{myBean.addChild('new')}"
                      value="Add"
                      process="@this"
                      update=":mainForm:myTree"/&gt;
      &lt;/p:contextMenu&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>I was able to solve this problem by replacing the <code>PrimeFaces.widget.BaseTree.nodeRightClick</code> function in JavaScript in order to trigger the <code>fireNodeSelectEvent</code> on right click.</p>
  
  <pre><code>PrimeFaces.widget.BaseTree.prototype.nodeRightClick = function(e, a) {
      PrimeFaces.clearSelection();
      if ($(e.target).is(":not(.ui-tree-toggler)")) {
          var d = a.parent(), b = a.hasClass("ui-tree-selectable");
          if (b &amp;&amp; this.cfg.selectionMode) {
              var c = this.isNodeSelected(d);
              if (!c) {
                  if (this.isCheckboxSelection()) {
                      this.toggleCheckboxNode(d)
                  } else {
                      this.unselectAllNodes();
                      // Fixed right click selecting
                      // original code: this.selectNode(d, true)
                      this.selectNode(d); // &lt;-- Fix
                  }
              }
              this.fireContextMenuEvent(d)
          }
      }
  }
  </code></pre>
  
  <p>This seemed like a bug to me, so I created an <a href="https://github.com/primefaces/primefaces/issues/1133" rel="nofollow">issue</a> on GitHub. This issues was closed as "won't fix" with the comment "Please use contextMenu event".</p>
  
  <p>I've checked the tree and contextMenu section of the documentation twice. What event should be used where? I've <a href="https://github.com/primefaces/primefaces/issues/1133#issuecomment-184109002" rel="nofollow">asked</a> the same question at GitHub, but there was no response.</p>