简体   繁体   中英

Replacing menu item in Jface popupmenu

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);
          }         
    }
 }                      
});

添加所有操作并使用javax.swing.JComponent#setVisible(boolean)

使用IAction.setId(String id)设置自定义动作的唯一ID,以便以后可以使用IMenuManager.remove(String id)删除这些动作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM