2014-10-10 66 views
-1

我不知道如何在TreeViewer(JFace)項目上添加鼠標右鍵單擊偵聽器?如何在TreeViewer(JFace)項目上添加鼠標右鍵單擊偵聽器?

MenuManager menuMgr = new MenuManager(); 
menuMgr.setRemoveAllWhenShown(true); 

menuMgr.addMenuListener(new IMenuListener() { 
    @Override 
    public void menuAboutToShow(IMenuManager menuManager) { 
     ... add items to menu manager 
    } 
}); 

Control tree = treeViewer.getControl(); 

Menu menu = menuMgr.createContextMenu(tree); 

tree.setMenu(menu); 

這是使用IMenuListener,您可以添加不同的菜單項:

/* 
* Creates the tree. 
* 
* @return the tree object and create part control 
*/ 
private TreeObject CreateTree() { 
    TreeParent root = new TreeParent(""); 

    DemoTreeModel ctModel = new DemoTreeModel(); 
    List<String> listType = ctModel.getType(); 
    for (String type : listType) { 
     TreeParent parentNode1 = new TreeParent(type); 
     parentNode1.setLevel(1); 

     List<String> listMachine = ctModel.getName(type); 
     if (listMachine != null) { 
      for (String machine : listMachine) { 
       TreeParent childNode1 = new TreeParent(machine); 
       childNode1.setLevel(2); 
       parentNode1.addChild(childNode1); 
       List<String> listVersion = ctModel.getVersion(machine); 

       if (listVersion != null) { 
        for (String ver : listVersion) { 
         TreeObject version = new TreeObject(ver); 
         version.setLevel(3); 
         version.setData(ver); 
         childNode1.addChild(version); 
        } 
       } 
      } 
     } 
     root.addChild(parentNode1); 
    } 

    return root; 
} 

回答

0

您可以使用這樣的事情:

這是我的源我創建一個類的TreeView從ViewPart時延伸每次菜單顯示時,都可以適應選擇的內容。

+0

讓我試試吧,非常感謝! – Ace 2014-10-13 08:02:58

相關問題