开发者

Dynamic label of a popup action in eclipse plugin development

开发者 https://www.devze.com 2023-03-19 06:08 出处:网络
I want to create a simple eclipse plugin, which does: When you right click a java project, it will show a popup menu which has a item h开发者_JS百科as label \"N java files found in this project\", whe

I want to create a simple eclipse plugin, which does: When you right click a java project, it will show a popup menu which has a item h开发者_JS百科as label "N java files found in this project", where "N" is the file count.

I have an idea that I can update the label in "selectionChanged":

public class CountAction implements IObjectActionDelegate {
    public void selectionChanged(IAction action, ISelection selection) {
        action.setText(countJavaFiles());
    }
}

But it doesn't work if I don't click that menu item, since the CountAction has not been loaded, that selectionChanged won't be invoked when you right-click on the project.

I have spent a lot of time on this, but not solved. Please help me.


An alternative to the article suggested by @kett_chup, is to use IElementUpdater. Simply

  • your handler must implement IElementUpdater
  • the handler.updateElement((UIElement element, Map parameters) must set the wanted text using element.setText("new text") - this new text will show up in menus and toolbars
  • whenever you need/want to update the command text use ICommandService.refreshElements(String commandId, Map filter) with your particular command ID - the global command service usually is just fine

The IElementUpdater interface can also be used to change the checked state - for commands with style=toggle - as well as the icons and the tool tip.


At last, I found a very easy way to implement this:

I don't need to change my code(the sample code in question), but I need to add a small startup class:

import org.eclipse.ui.IStartup;

public class MyStartUp implements IStartup {

    @Override
    public void earlyStartup() {
        // Initial the action
        new CountAction();
    }
}

And add following to plugin.xml:

<extension
     point="org.eclipse.ui.startup">
  <startup
        class="myplugin.MyStartUp">
  </startup>

This MyStartUp will load an instance of that action at startup, then selectionChanged will be invoked each time when I right-click the projects or files.

0

精彩评论

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