CryptDemon CryptDemon - 9 months ago 120
Java Question

Programmatically add options to pulldown button in eclipse

In eclipse I have commands defined for buttons in the main button toolbar they have. I have one command/button in there that's set as a pulldown button and I'd like to programmatically add options to it. Kinda like how you can hit the little drop down button on the play button in eclipse and see different run scenarios. I want to be able to add options like that to my pulldown menu. I can't do it through the plugin editor because I need to generate the menu options dynamically.

So say I have the following pulldown button defined in my plugin.xml file. How do I add options to the pull down programmatically?

label="Im a pulldown menu"


Please find the below code.

private void addContextMenu(SampleContributionFactory fac) {
     final IMenuService menuService = (IMenuService) PlatformUI.getWorkbench().getService (IMenuService.class);

class SampleContributionFactory extends AbstractContributionFactory{

    SampleContributionFactory(final String menuID) {
        super("menu:" + menuID, null);

    public void createContributionItems(IServiceLocator serviceLocator,
            IContributionRoot additions) {      
        // add Command Contribution item
        additions.addContributionItem(<YOUR CONTRIBUTION ITEM>, null);
    // add one more Command Contribution item

Now create an object of SampleContributionFactory as below.

  SampleContributionFactory fac = new SampleContributionFactory ("");

and call the method


TODO : add command contribution items in the SampleContributionFactory as required by you.