Eclipse插件:创建一个动态菜单和相应的处理程序? [英] Eclipse Plugin: Creating a dynamic menu and corresponding handler?

查看:110
本文介绍了Eclipse插件:创建一个动态菜单和相应的处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力写出我认为是一个非常简单的Eclipse插件,但是我真的很难找到我的方式,因为我从未与PDE合作过。



基本上我想要做的是向Java Project上下文菜单添加一个子菜单,该菜单将在项目的根目录中列出一堆可用文件。然后在选择这些子菜单项之一后,我希望调用处理程序并传递所选文件的名称。



到目前为止,我已经设法得到通过向org.eclipse.ui.menus扩展点添加一个动态菜单对象来显示菜单。我已经定义了我自己的CompoundContributionItem,它找到相应目录中的所有文件,并填充菜单。这些菜单项中的每一个都被连接到我的处理程序(extends AbstractHandler),每当选择一个菜单项时,处理程序就会被调用。我不知道该怎么做是让我的处理程序根据选择的菜单项来做某事。如果它以某种方式通过了菜单项标签的字符串就够了,但是我猜想这可能是一个更好的方法。



我试过查看org.eclipse.debug.ui的代码,看看它们如何使用运行/调试配置来执行,因为这完全是我想要的。他们从项目的.launches目录中选择.launch文件,并将其显示在列表中。这个代码虽然非常复杂,但还有很多与之不相关的其他行为,所以作为一个初学者,我正在努力解决问题。此外,他们似乎做的不同于我做的,所以可能是我完全错了我的做法。任何帮助或指针都将不胜感激。

解决方案

我终于找到了这个答案。在plugin.xml中,可以为每个命令指定参数,例如

 < commandParameter 
id =commandParameterID
name =参数名称
optional =false>
< / commandParameter>

现在,当我动态创建每个菜单项时,我可以将我的参数添加到参数Map CommandContributionItemParameter 对象。

  CommandContributionItemParameter param = new CommandContributionItemParameter(PlatformUI.getWorkbench (),null,CommandID,CommandContributionItem.STYLE_PUSH); 
param.parameters = new HashMap< String,String>();
param.parameters.put(commandParameterID,TheValue);

以这种方式添加的参数可以在处理程序类中访问,如下所示:

  public Object execute(ExecutionEvent event)throws ExecutionException {
System.out.println(event.getParameter(commandParameterID));
返回null;
}


I am trying to write what I think is a very simple Eclipse plugin, but I am really struggling to find my way around as I have never worked with the PDE before.

Basically what I am trying to do is add a sub-menu to the Java Project context menu that will list a bunch of available files in the project's root directory. Then upon selecting one of these sub-menu items I want the handler to be called and passed the name of the file that was selected.

So far I have managed to get the menu to appear correctly by adding a dynamic menuContribution to the org.eclipse.ui.menus extension point. I have defined my own CompoundContributionItem which finds all of the files in the appropriate directory and populates the menu. Each of these menu items is hooked up to my handler (extends AbstractHandler) and the handler does get called each time a menu item is selected. What I don't know how to do is to get my handler to actually do something based on which of the menu items was selected. It would be enough if it was somehow passed the string of the menu item label, but I'm guessing there is probably a much better way of doing this.

I tried looking through the code of the org.eclipse.debug.ui to see how they do it with the run/debug configurations, because that is pretty much exactly what I want. They pick up the .launch files from the .launches directory of the project and display them in a list. The code for that is very complicated though and has a lot of other behaviour that is unassociated with it, so as a beginner I am struggling to get my head around it. Also, they seem to have done it a different way than I did, so it might be that I am totally wrong in my approach. Any help or pointers would be appreciated.

解决方案

I finally found the answer to this myself. Within plugin.xml it is possible to specify parameters for each command, e.g.

<commandParameter
    id="commandParameterID"
    name="Name of the Parameter"
    optional="false">
</commandParameter>

Now, when I am dynamically creating each menu item I can just add my parameter to the parameters Map of the CommandContributionItemParameter object.

CommandContributionItemParameter param = new CommandContributionItemParameter(PlatformUI.getWorkbench(), null, "CommandID", CommandContributionItem.STYLE_PUSH);
param.parameters = new HashMap<String, String>();
param.parameters.put("commandParameterID", "TheValue");

The parameters that are added this way are accessible in the handler class as follows:

public Object execute(ExecutionEvent event) throws ExecutionException {
    System.out.println(event.getParameter("commandParameterID"));
    return null;
}

这篇关于Eclipse插件:创建一个动态菜单和相应的处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆