title: Context menu for TreeViewer based on selected node - SWT
body: <p>I need to create a context menu for a TreeViewer in an Eclipse plugin project. But, the menu should not contain constant items, they should vary depending on the type of the node that is selected. For example, my treeViewer has the following hierarchy:</p>
  
  <pre><code>Node A
   |
   --Node B
    |
     --Node C
  </code></pre>
  
  <p>For node A - I want to show a menu with an action, but for nodes B and C I don't want to show anything (no menu).
  I managed to create the menu for node A, but then I can't get rid of it when some other type of node is selected. My code looks like:</p>
  
  <pre><code>treeViewer.addSelectionChangedListener(
              new ISelectionChangedListener(){
                  public void selectionChanged(SelectionChangedEvent event) {
                      if(event.getSelection() instanceof IStructuredSelection) {
                          IStructuredSelection selection = (IStructuredSelection)event.getSelection();            
                          Object o = selection.getFirstElement();     
  
                          MenuManager menuMgr = new MenuManager();
  
                          if (o instanceof NodeA){
  
                              Menu menu = menuMgr.createContextMenu(treeViewer.getControl());
                              treeViewer.getControl().setMenu(menu);
                              getSite().registerContextMenu(menuMgr, treeViewer);
  
                              menuMgr.add(new SomeAction());
  
                          }else {
                              //what ?
                          }
                      }
  
                  }
              }   
      );
  </code></pre>
  
  <p>On the else branch I tried to call <code>dispose()</code>,<code>removeAll()</code> on the MenuManager...nothing works!</p>
  
  <p>Any help is appreciated, thanks.</p>