Add menu item to menu at runtime [message #881071] |
Sun, 03 June 2012 16:46  |
Eclipse User |
|
|
|
Hello,
I need to create a submenu which is filled with elements at runtime.
I didn't find any examples or any easy way to do it, so I've implemented it via a crude "iterate over menus to find the correctly named menu, and then add MMenuElements to it".
Corresponding code in the handler:
@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);
}
}
This correctly adds the MMenuElements to the menu and seemingly also removes them, but obviously the state gets saved before the @PreDestroy method is called (I mean the "runtime" folder in the Eclipse workspace), so the menu elements don't get removed and multiply each time the application starts.
Can anyone point me in the right direction to do this?
|
|
|
|
|
|
|
|
Re: Add menu item to menu at runtime [message #881503 is a reply to message #881346] |
Mon, 04 June 2012 13:39   |
Eclipse User |
|
|
|
First of all, thanks for your input so far!
Sopot Cela wrote on Sun, 03 June 2012 18:58On 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.
Clearing the workspace works, but my impression is exactly what you wrote in your last sentence - the model stays the same, and that's unacceptable for the release application.
Christoph Keimel wrote on Mon, 04 June 2012 03:54maybe you could remove the menu items on @PersistState instead of @PreDestroy?
I tried that, but the method then does not get called at exiting the application.
Brian de Alwis wrote on Mon, 04 June 2012 07:34... Checking or and removing the old menu on startup is likely your best option for now.
Do you mean the menu where I want to put the dynamically generated menu items into or the menu items?
When I simply remove the menu items from the menu target.getChildren().clear(); , on first click on one of the menu items I get a fat stacktrace with no functions of my own involved - after that, the menu items in the menu are unlabeled and throw further exceptions or do nothing when being clicked.
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)
I really thought that implementing a "recent files" menu would be a very simple feature to implement - maybe I'm overlooking something terribly obvious (this framework is new to me)?
|
|
|
|
|
|
|
Re: Add menu item to menu at runtime [message #985880 is a reply to message #881879] |
Fri, 16 November 2012 09:43  |
Eclipse User |
|
|
|
Hi,
thank you for solution above, it was very helpful. However, I have problem with finding which element of the Menu was clicked in execute method of handler. I would be grateful for any hint.
[EDIT] Solution is of course commands parameter..
[Updated on: Fri, 16 November 2012 12:20] by Moderator
|
|
|
Powered by
FUDForum. Page generated in 0.28696 seconds