|
Re: More context menus for editor [message #765748 is a reply to message #765728] |
Wed, 14 December 2011 15:51 |
Daniel Krügler Messages: 853 Registered: July 2009 |
Senior Member |
|
|
On 2011-12-14 16:32, Behnil wrote:
> Hi,
>
> how can I have more context menus in my editor? I have a TableViewer and
> a ListViewer side by side in the editor part and I would like to
> contribute the theirs context menus independently. Both viewers show
> same model elements.
>
>
> getSite().setSelectionProvider(tableViewer);
>
> MenuManager menuMgr = new MenuManager();
> menuMgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
> Menu menu = menuMgr.createContextMenu(tableViewer.getControl());
> tableViewer.getControl().setMenu(menu);
> getSite().registerContextMenu(menuMgr, tableViewer);
>
>
> menuMgr = new MenuManager();
> menuMgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
> Menu menu = menuMgr.createContextMenu(listViewer.getControl());
> listViewer.getControl().setMenu(menu);
> getSite().registerContextMenu("secondMenu", menuMgr, listViewer);
>
>
> when I contribute a menu like this:
>
> <menuContribution
> locationURI="popup:org.eclipse.ui.popup.any?after=additions">
> <command commandId="myCommandId" style="push">
> <visibleWhen checkEnabled="false">
> <and>
> <count value="1"></count>
> <iterate>
> <instanceof value="myModelObject"></instanceof>
> </iterate>
> </and>
> </visibleWhen>
> </command> </menuContribution>
>
> in the table's context menu the contribution shows up correctly. But the
> list's menu is empty. I guess its because of the line
>
> getSite().setSelectionProvider(tableViewer);
>
> Which selectionProvider to register with the site when I have more of
> them by the way?
If you have more than one context menu per view you should assign
different menu ids to *both*, i.e. use
MenuManager menuMgr = new MenuManager(null, "firstMenuId");
[..]
getSite().registerContextMenu("firstMenuId", menuMgr, tableViewer);
and
MenuManager menuMgr = new MenuManager(null, "secondMenuId");
[..]
getSite().registerContextMenu("secondMenuId", menuMgr, listViewer);
HTH & Greetings from Bremen,
Daniel Krügler
|
|
|
Re: More context menus for editor [message #765768 is a reply to message #765748] |
Wed, 14 December 2011 16:40 |
Jan Krakora Messages: 477 Registered: December 2009 Location: Prague |
Senior Member |
|
|
And what about the site's selection provider? When I have more of them, which one to set?
I suppose that menu service deal with actual selection of the editor part when composes context menu from declarative menu contributions. Or does it deal with selection from the selection provider registered by getSite().registerContextMenu("menuId", menuManager, selectionProvider)?
[Updated on: Wed, 14 December 2011 17:02] Report message to a moderator
|
|
|
|
|
Re: More context menus for editor [message #766763 is a reply to message #766104] |
Fri, 16 December 2011 11:34 |
Jan Krakora Messages: 477 Registered: December 2009 Location: Prague |
Senior Member |
|
|
Well, I have still a problem with this.
I have this menu contribution:
<menuContribution locationURI="popup:org.eclipse.ui.popup.any?after=additions">
<command commandId="myCommand" style="push">
<visibleWhen checkEnabled="false">
<with variable="activeMenuSelection">
<and>
<count value="1"></count>
<iterate>
<instanceof value="myModel"></instanceof>
</iterate>
</and>
</with>
</visibleWhen>
</command>
</menuContribution>
Let's say my editor contains a TableViewer. This TableViewer is registered as selection provider to the editor's site.
It has also a context menu which is registered to the site so it can be contributed by other plugins.
getSite().setSelectionProvider(tableViewer);
MenuManager menuMgr = new MenuManager(null, "editor.mainContextMenu");
menuMgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
Menu menu = menuMgr.createContextMenu(tableViewer.getControl());
tableViewer.getControl().setMenu(menu);
getSite().registerContextMenu("editor.mainContextMenu", menuMgr, tableViewer);
Context menu is shown properly.
Now let's say that I can open a non modal dialog from this editor. In the dialog is a ListViewer containing the same model as the editor's TableViewer.
And would like to get context menu on that ListViewer also extended by other plugins. So I pass the editor's IWorkbenchPartSite to the dialog when is opened
and register ListViewer's context menu:
MenuManager menuMgr = new MenuManager(null, "editor.dialogContextMenu");
menuMgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
Menu menu = menuMgr.createContextMenu(listViewer.getControl());
listViewer.getControl().setMenu(menu);
editorSite.registerContextMenu("editor.dialogContextMenu", menuMgr, listViewer);
Now when the dialog is open, context menu show properly. That's amazing.
But, when I left click on the editor and then I click back to the dialog and pres right click on the listViewer, listViewer's context menu isn't shown at all.
When I after that right click on the tableViewer and then again right click on the listViewer, context menu shows up properly.
I really don't understand what is happening here.
After some debugging I found the MenuManager on the listViewer handleAboutToShow() and immediately after that handleAboutToHide() called.
So the context menu can't be shown I guess. but why is the handleAboutToHide() called and why right click on the tableViewer fixes that, is a mystery.
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02807 seconds