tags:

views:

91

answers:

2

Hiho,

currently I have a working popup menu which appears when I click on a treeview item. But I want to show different popups for different tree view entries. I don't get a idea how to do so...

Here is my code for creating the menu:

 MenuManager menuMgr = new MenuManager("#PopupMenu"); 
 menuMgr.setRemoveAllWhenShown(true);
 menuMgr.addMenuListener(new IMenuListener() {
     @Override
     public void menuAboutToShow(IMenuManager manager) {
         Action action = new Action() {
      public void run() {
                // So something
      }
  };
  action.setText("Set as working file");
  manager.add(action);
 }

 });

 Menu menu = menuMgr.createContextMenu(getTree());
 getTree().setMenu(menu);
A: 

Two ideas. For both you need to listen to selections on the TreeView, because that's the only way to determine which Menu (or special content) you want to show.

Then you could either set the correct menu to the the tree right after you know which one to use or contribute the needed items to the existing menu (that's how it's done in the eclipse framework).

Andreas_D
The last one sounds good to me, I will try it.
InsertNickHere
A: 

You should propably use a MouseListener on the tree:

final Tree tree = new Tree(parent, ...);
tree.addMouseListener(new MouseAdapter() {
    @override
    public void mouseDown(MouseEvent me) {
        if(tree.getSelection() instanceof MySpecificTreeNode) {
            // create menu...
        }
    }
});
dacwe
I guess your answer would also work, but I prefer the other. Can not upvote yours, sorry.
InsertNickHere
If the code is part of a RCP app then one should register the TreeView as a SelectionProvider and listen more globally
Andreas_D