title: How to make a Dynamic Context Menu for an Eclipse plug-in?
body: <p>I am trying to make an Eclipse plugin which would be capable of dynamically change the context menu of the navigation view according to other plugins using an extension point that first plugin provides.</p>
  
  <p>With a little Schematic : <a href="http://hpics.li/bddf781" rel="nofollow">http://hpics.li/bddf781</a></p>
  
  <p>The aim is that all the commands provided by plugin 2, 3 ... can be accessed in the navigation context menu thanks to plugin 1</p>
  
  <p>I've already googled but I can't get anything to work.</p>
  
  <p>For instance :</p>
  
  <p>package com.sigasi;</p>
  
  <pre><code> import java.util.Date;
  
   import org.eclipse.jface.action.ContributionItem;
   import org.eclipse.swt.SWT;
   import org.eclipse.swt.events.SelectionAdapter;
   import org.eclipse.swt.events.SelectionEvent;
   import org.eclipse.swt.widgets.Menu;
   import org.eclipse.swt.widgets.MenuItem;
  
  
   public class MyDynamicMenu extends ContributionItem {
  
      public MyDynamicMenu() {
      }
  
      public MyDynamicMenu(String id) {
      super(id);
      }
  
  @Override
  public void fill(Menu menu, int index) {
      //Here you could get selection and decide what to do
      //You can also simply return if you do not want to show a menu
  
      //create the menu item
      MenuItem menuItem = new MenuItem(menu, SWT.CHECK, index);
      menuItem.setText("My menu item (" + new Date() + ")");
      menuItem.addSelectionListener(new SelectionAdapter() {
          public void widgetSelected(SelectionEvent e) {
              //what to do when menu is subsequently selected.
              System.err.println("Dynamic menu selected");
          }
      });
  }
  }
  </code></pre>
  
  <p>with :</p>
  
  <pre><code>&lt;extension point="org.eclipse.ui.menus"&gt;
  &lt;menuContribution locationURI="popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu?after=additions"&gt;
      &lt;dynamic
                class="com.sigasi.MydynamicMenu"
                id="com.sigasi.myDynamicMenu"&gt;
      &lt;/dynamic&gt;
    &lt;/menuContribution&gt;
  &lt;/extension&gt;
  </code></pre>
  
  <p>Source :  <a href="http://www.sigasi.com/content/dynamic-menu-items-eclipse" rel="nofollow">http://www.sigasi.com/content/dynamic-menu-items-eclipse</a></p>
  
  <p>Which could be great if it wouldn't add just one item to the Menu. I tried to adapt it to my need without success.</p>
  
  <p>Edit : I tried to change the super class of the java class up there to CompoundContributionItem; it allows to add multiple items to the menu but seems to replace existing ones depending on the index instead of simply adding it. Moreover, I still couldn't insert a Menu. Here is what I did :</p>
  
  <pre><code>package fr.cardinjoseph.extensionpoint.definition;
  
  import java.util.HashMap;
  import java.util.Map;
  
  import org.eclipse.jface.action.ContributionItem;
  import org.eclipse.jface.action.IContributionItem;
  import org.eclipse.swt.SWT;
  import org.eclipse.swt.events.SelectionAdapter;
  import org.eclipse.swt.events.SelectionEvent;
  import org.eclipse.swt.widgets.Menu;
  import org.eclipse.swt.widgets.MenuItem;
  import org.eclipse.ui.IWorkbench;
  import org.eclipse.ui.IWorkbenchWindow;
  import org.eclipse.ui.PlatformUI;
  import org.eclipse.ui.actions.CompoundContributionItem;
  import org.eclipse.ui.menus.CommandContributionItem;
  import org.eclipse.ui.menus.CommandContributionItemParameter;
  
  public class MyDynamicMenu extends CompoundContributionItem {
  
      ExtensionsFinder ef;
  
      public MyDynamicMenu() {
          ef = new ExtensionsFinder();
          ef.evaluateExtensions();
      }
  
      public MyDynamicMenu(String id) {
          super(id);
      }
  
      @Override
      public void fill(Menu menu, int index) {
          // Here you could get selection and decide what to do
          // You can also simply return if you do not want to show a menu
  
          // create the menu item
  
            Menu commandMenu = new Menu(menu);
  
  
           MenuItem test = new MenuItem(menu, index); test.setText("Test");
           test.addSelectionListener(new SelectionAdapter() { public void
           widgetSelected(SelectionEvent e) {  System.err.println("Dynamic menu selected"); }
           });
  
  
      }
  
      @Override
      protected IContributionItem[] getContributionItems() {
          // TODO Auto-generated method stub
          int size = ef.getExtensions().size();
          IContributionItem[] list = new IContributionItem[size];
  
          IWorkbench wb = PlatformUI.getWorkbench();
          IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
  
          for(int i = 0 ; i&lt; size ; i++){
              list[i] = new CommandContributionItem(new CommandContributionItemParameter(win, "MenuCommandeId_"+i, "CommandeId_"+i, SWT.None));
          }
  
          System.out.println("test");
  
          return list;
      }
  
      @Override
      public boolean isDynamic(){
          return true;
      }
  
  }
  </code></pre>