开发者

Eclipse Plugin: Creating a dynamic menu and corresponding handler?

开发者 https://www.devze.com 2023-03-23 16:44 出处:网络
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.

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


Possibly this simple menu creator helps you a step further (or the surrounding classes within the project underlying the link) or maybe the plugin.xml of the popup menu within the same project

0

精彩评论

暂无评论...
验证码 换一张
取 消