0

I want to show a popup menu in JFace's TreeViewer.

The menu should contain 3 constant menu items that never change, and additional item that varies depending to a tree node that was clicked on (selected).

One option is to use setRemoveAllWhenShown(true), but this will delete all menu items each time including the constant items.

I want to avoid that.

So to conclude my task:

  • If use right clicks on tree without selecting any node, show just the constant items.
  • If use right clicks on specific node, show the constant items (remove the previous additional item if exists) and add additional item for this node (it also can be replaced if this option is available).

My code so far:

//Add Some Actions
menuManager.add(..);
menuManager.add(..);
menuManager.add(..);
menuManager.add(new Separator());

//This will delete all items inluding the constant, I want to avoid that        
//menuManager.setRemoveAllWhenShown(true);

menuManager.addMenuListener(new IMenuListener() {           
  public void menuAboutToShow(IMenuManager manager) {
    IStructuredSelection selection = (IStructuredSelection) mTreeViewer.getSelection();
    if (!selection.isEmpty()) {
          BaseItm selected = (BaseItm) selection.getFirstElement();

          if (selected instanceof sometype) {                                                             
             //Remove additional item IF exists
             manager.add(sepcificActionForThisNode);
          }         
    }
 }                      
});
flavio.donze
  • 7,432
  • 9
  • 58
  • 91
davids
  • 23
  • 1
  • 5

2 Answers2

0

add all Actions and use javax.swing.JComponent#setVisible(boolean)

ollins
  • 1,851
  • 12
  • 16
  • Action doesn't have setVisible method, do you mean I should use ActionContributionItem? Anyway I would have to hide all previous items and show the need one every time, right? – davids Oct 19 '11 at 14:17
  • Another issue with this if I have 20 specific menu items, every time I click on an item in tree I would have to: 1)hide the previously visible item (I would have to store it somewhere) 2)loop through manage.getItems() and find the action that I need to show – davids Oct 19 '11 at 15:09
  • jep ActionContributionItem is fine. But setRemoveAllWhenShown(true) works fine for me too. It's cheep. What are your concerns? – ollins Oct 19 '11 at 17:55
0

Use IAction.setId(String id) to set a unique ID for custom action(s) so that those actions could later be removed using IMenuManager.remove(String id).

Martti Käärik
  • 3,601
  • 18
  • 28