2011-10-19 24 views
0

我想在JFace的TreeViewer中顯示一個彈出式菜單。替換Jface中的菜單項popupmenu

菜單應該包含3個永不改變的常量菜單項,以及根據點擊(選定)的樹節點而變化的附加項。

一個選項是使用setRemoveAllWhenShown(true),但每次包括常量項目都會刪除所有菜單項。

我想避免這種情況。

所以來結束我的任務:

  • 如果樹使用權點擊而不選擇節點,只顯示常數項。
  • 如果在特定節點上使用右鍵單擊,顯示常量項目(刪除前面的附加項目(如果存在)併爲此節點添加附加項目(如果此選項可用,也可以替換它)。

我迄今爲止代碼:

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

回答

0

添加所有的操作和使用

+0

動作沒有setVisible方法,你的意思是我應該使用ActionContributionItem?無論如何,我將不得不隱藏所有以前的項目,並顯示需要每一次,對吧? – davids

+0

如果我有20個特定的菜單項,每次我點擊一個樹中的項目,我將不得不:1)隱藏以前可見的項目(我將其存儲在某處)2)通過manage.getItems循環()並找到我需要顯示的動作 – davids

+0

jep ActionContributionItem很好。但是setRemoveAllWhenShown(true)對我也很好。這很好。你有什麼擔憂? – ollins