Tutorial: Eclipse dynamic popup menus

This tutorial describes how to create dynamic popup menus in Eclipse using extension-point “org.eclipse.ui.popupMenus”.

Some applications require dynamic menus for dynamic contents. Sometimes there is no benefit to declare all possible actions in the plugin manifest as there is no way to know all possibilities. In those cases dynamic menus are required when the menu structure depends on the selection context of the workbench. This tutorial will show you how to create a dynamic menu during runtime contributed on any view where a IFile resource is selected without a declaration of the menu inside the plugin manifest.

  • First of all, you have to create a plugin project using the extension point “org.eclipse.ui.popupMenus”.

create new popup-menu extension

  • Configure your new extension with your own values

configure extension

  • Remove all content of the run method, as our action will not perform anything and will just be the root container of our submenu.

empty run method

  • On each selection change event, ensure that our action has an appropriate menu creator to build the items of our submenu or to build other submenus.

set a menu creator to the action

  • Now at the end, set our action to be a pulldown command. Therefore open the plugin manifest editor of our project and select the action style to “pulldown”.

category where to select pulldown style

the pulldown selection

  • To test our new menu, just start a Eclipse workbench using our plugin and after that you can see something like the following.

voilla, it works