简体   繁体   English

如何以编程方式在eclipse插件中添加右键菜单

[英]How to add right click menu in eclipse plugin programmatically

I have an eclipse plugin with a View part. 我有一个带有View部分的eclipse插件。 In this View part I have a table and I want to add a submenu when a user right clicks on a row in that table. 在此“视图”部分中,我有一个表,我想在用户右键单击该表中的行时添加一个子菜单。 How can I do this programatically? 如何以编程方式执行此操作?

I have defined the command and menuContribution in my plugin.xml: 我已经在我的plugin.xml中定义了command和menuContribution:

<extension
     point="org.eclipse.ui.menus">
   <menuContribution
        allPopups="true"
        class="com.sintec.eclipseplugins.clippy.menu.DeleteMenuContribution"
        locationURI="menu:delete?after=additions">
   </menuContribution>
</extension>

I have also created the contribution class extending ExtensionContributionFactory: 我还创建了扩展ExtensionContributionFactory的贡献类:

public class DeleteMenuContribution extends ExtensionContributionFactory {

@Override
public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
    // build a couple of command-based contribution parameters
    CommandContributionItemParameter pAA = new CommandContributionItemParameter(
        serviceLocator,
        "DeleteCommand",
        "com.sintec.eclipseplugins.clippy.delete",
        SWT.PUSH);
    pAA.label = "Delete Command";

    // create actual contribution items and add them to the given additions reference
    CommandContributionItem itemAA = new CommandContributionItem(pAA);
    itemAA.setVisible(true);
    additions.addContributionItem(itemAA, null);

}

}

Aaaand last but not least I have this code my ViewPart where I want to add the menu to the table: 最后但并非最不重要的一点是,我将ViewPart的这段代码添加到了要将菜单添加到表中的位置:

IMenuManager mgr = new MenuManager().findMenuUsingPath("menu:delete?after=additions");
MenuManager mgr2 = new MenuManager();
mgr2.add(mgr);
tableViewer.getTable().setMenu(mgr2.createContextMenu(tableViewer.getTable()));

The problem is that the menu can not be found. 问题是找不到菜单。 I'm pretty sure I'm missing something ... Is the menu URI correct? 我敢肯定我遗漏了一些...菜单URI正确吗?

我有点at ...但是为什么不给菜单分配一个id并在MenuManager中使用find(id)方法呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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