@PostConstruct public void init() { for (final String filename : Constants.recentlyUsedFilesList) { DynamicMenuHelper.addMenuItemToMenu(application, "recentfiles.menu", filename, "bundleclass://handlers.file.RecentFilesHandler"); } } @PreDestroy public void cleanup() { for (final String filename : Constants.recentlyUsedFilesList) { DynamicMenuHelper.removeMenuItemFromMenu(application, "recentfiles.menu", filename); } }
I need to create a submenu which is filled with elements at runtime.
//creation MDirectMenuItem dynamicItem = MMenuFactory.INSTANCE.createDirectMenuItem(); dynamicItem.setLabel("Testing"); //addition menu.getChildren().add(dynamicItem);
MMenu target = (MMenu) findMenuElement(application, menuElementId); List<MMenuElement> elements = target.getChildren(); List<MMenuElement> found = new ArrayList<MMenuElement>(); for (MMenuElement element: elements) { if (element.getLabel() != null && element.getLabel().equals(menuItemLabel)) { found.add(element); } } target.getChildren().removeAll(found);
On your run configuration -> main tab -> check 'Clear' and select 'workspace' -> apply
However it looks like when you remove the items, they don't get removed from the model and when the app runs again it loads the model which the previous launch left with the items in the model.
maybe you could remove the menu items on @PersistState instead of @PreDestroy?
... Checking or and removing the old menu on startup is likely your best option for now.
target.getChildren().clear();
java.lang.NullPointerException at org.eclipse.e4.ui.workbench.renderers.swt.DirectContributionItem.getExecutionContext(DirectContributionItem.java:476) at org.eclipse.e4.ui.workbench.renderers.swt.DirectContributionItem.executeItem(DirectContributionItem.java:450) at org.eclipse.e4.ui.workbench.renderers.swt.DirectContributionItem.handleWidgetSelection(DirectContributionItem.java:360) at org.eclipse.e4.ui.workbench.renderers.swt.DirectContributionItem.access$1(DirectContributionItem.java:344) at org.eclipse.e4.ui.workbench.renderers.swt.DirectContributionItem$2.handleEvent(DirectContributionItem.java:301) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053) at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1021) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:915) at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86) at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:150) at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629) at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584) at org.eclipse.equinox.launcher.Main.run(Main.java:1438) at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
((EObject)application).eResource().save(null);
DynamicMenuHelper.clearMenu(application, "menudemo.recentfiles.menu");