First we should contribute a menu group say dynamicMenuGroup as follows :

<menuContribution
locationURI=”popup:org.eclipse.ui.popup.any?after=editGroup”>
<menu
icon=”icons/dynamic-menu.png”
id=”dynamicMenu”
label=”%DynamicMenu.label”
mnemonic=”%DynamicMenu.mnemonic”
tooltip=”%DynamicMenu.tooltip”>
<separator
name=”dynamicMenuGroup”
visible=”false”>

Next we need to make a dynamic menu contribution
extension
id=”.eclipse.examples.dynamic.popup.menu”
name=”Dynamic popup menu”
point=”org.eclipse.ui.menus”>
<menuContribution
locationURI=”popup:dynamicMenu?after=dynamicMenuGroup”>
<dynamic
class=”com.eclipse.examples.ShowDynamicMenu”
id=”org.eclipse.ui.showDynamicMenu”>
<visibleWhen
checkEnabled=”false”>

Finally we should create the following class :

DynamicMenu extends ContributionItem {
private Action[] actions;
private IWorkbenchWindow window;
protected boolean dirty = true;

public static final String SHOW_QUICK_FIX_ID = “org.eclipse.ui.showDynamic”; ////$NON-NLS-N$

public DynamicMenu () {
this(SHOW_QUICK_FIX_ID);
}

private final class DynamicAction extends Action {
public DynamicAction() {
}
public void run(){
//
}
}

public DynamicMenu (String id) {
super(id);
for(){
actions.add(new DynamicAction());
}
}

private IMenuListener menuListener = new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
manager.markDirty();
dirty = true;
}
};

public void fill(Menu menu, int index) {
if (getParent() instanceof MenuManager) {
((MenuManager) getParent()).addMenuListener(menuListener);
}

if (!dirty) {
return;
}

MenuManager manager = new MenuManager();
fillMenu(manager);
IContributionItem items[] = manager.getItems();
if (items.length > 0 ) {
for (int i = 0; i < items.length; i++) {
items[i].fill(menu, index++);
}
}
dirty = false;
}

private void fillMenu(IMenuManager innerMgr) {
// Remove all.
innerMgr.removeAll();
// If no page disable all.
IWorkbenchPage page = window.getActivePage();
if (page == null) {
return;
}

for (int i = 0; i < actions.length; i++) {
innerMgr.add(actions[i]);
}
}

Advertisements