Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » multiple context menu's in one editor
multiple context menu's in one editor [message #544602] Sun, 04 July 2010 20:07 Go to next message
Henno Vermeulen is currently offline Henno VermeulenFriend
Messages: 126
Registered: July 2009
Senior Member
I have one (Form)editor with multiple TableViewers. I would like each TableViewer to have it's own context menu.

My menu's can contain commands such as "insert new row", "delete selection", etc...

I think it's not hard to do this in pure SWT with "low level" selection listeners, but I would like to do it the clean RCP way with commands and structured selections. (Actually I am not sure if I ever need another plugin to contribute commands, but it would be nice to have this possibility).

As described on this blog, I found out how to add a context menu to one TableViewer(s) and register it and it's structured selection with the editor so that plugins can contribute commands. Their handlers can then access the selection using
HandlerUtil.getCurrentSelection(event)
inside their execute method.

But in my case I have multiple TableViewers that should have their own commands and have their own selection. So I have these questions:

1. How can I contribute commands to one specific TableViewer but not to the others? Can I somehow change the
<menuContribution locationURI="popup:some.package.someditor">

to
<menuContribution locationURI="popup:some.package.someditor.someviewer">
?

2. When I write a handler for a command such as "insert new row" or "delete selected rows" or "call selected person" I will need access to the structured selection of the specific TableViewer and I need access to the WritableList in it's content provider to remove/add the new items. And what do I do when I need access to other data/methods that is only known by my specific editor? What is a clean way to get access to this information in the handler code?
Re: multiple context menu's in one editor [message #544629 is a reply to message #544602] Mon, 05 July 2010 06:34 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 04.07.2010 22:07, SlowStrider wrote:
> I have one (Form)editor with multiple TableViewers. I would like each
> TableViewer to have it's own context menu.
>
> My menu's can contain commands such as "insert new row", "delete
> selection", etc...
>
> I think it's not hard to do this in pure SWT with "low level" selection
> listeners, but I would like to do it the clean RCP way with commands and
> structured selections. (Actually I am not sure if I ever need another
> plugin to contribute commands, but it would be nice to have this
> possibility).
>
> As described
> http://www.ralfebert.de/blog/eclipsercp/commands_context_men u/, I found
> out how to add a context menu to one TableViewer(s) and register it and
> it's structured selection with the editor so that plugins can contribute
> commands. Their handlers can then access the selection using
> HandlerUtil.getCurrentSelection(event) inside their execute method.
>
> But in my case I have multiple TableViewers that should have their own
> commands and have their own selection. So I have these questions:
>
> 1. How can I contribute commands to one specific TableViewer but not to
> the others? Can I somehow change the
> <menuContribution locationURI="popup:some.package.someditor">
> to
> <menuContribution locationURI="popup:some.package.someditor.someviewer">?

Yes, in fact the latter assumption is correct. You have to create a
MenuManager for each Table control (or whatever control you need a
context menu for) as described in the Link. You also need to invoke
the call to registerContextMenu, but this time you need the three-
parameter version:

void registerContextMenu(String menuId, MenuManager menuManager,
ISelectionProvider selectionProvider);

For the first parameter you need to provide a unique ID for each
viewer. This i9d needs to be used within the locationURI as you
assumed.
You don't need to invoke

getSite().setSelectionProvider(tableViewer);

because this would not only register one of the viewers as selection
provider.

> 2. When I write a handler for a command such as "insert new row" or
> "delete selected rows" or "call selected person" I will need access to
> the structured selection of the specific TableViewer and I need access
> to the WritableList in it's content provider to remove/add the new
> items. And what do I do when I need access to other data/methods that is
> only known by my specific editor? What is a clean way to get access to
> this information in the handler code?

Use HandlerUtil.getActiveEditor or HandlerUtil.getActivePart and
cast to your concrete Editor class - or even better: Let your editor
implement some specific interface that provides the functionality you
need and cast to the interface.

HTH & Greetings from Bremen,

Daniel Krügler
Re: multiple context menu's in one editor [message #544675 is a reply to message #544629] Mon, 05 July 2010 08:47 Go to previous messageGo to next message
Henno Vermeulen is currently offline Henno VermeulenFriend
Messages: 126
Registered: July 2009
Senior Member
Thank you, this seems to be exactly what I need, I will try it out. By the way, can you provide some tips on how I can find what you told me in the documentation so that I don't need to post?

I also like to keep our business-specific code as much decoupled from Eclipse RCP classes as I can so casting to an interface that provides what I need is a good idea.
Re: multiple context menu's in one editor [message #544708 is a reply to message #544675] Mon, 05 July 2010 10:01 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 05.07.2010 10:47, SlowStrider wrote:
> Thank you, this seems to be exactly what I need, I will try it out. By
> the way, can you provide some tips on how I can find what you told me in
> the documentation so that I don't need to post?

In fact it is documented in the Eclipse help, but sometimes not very
clear. E.g. by combining the information from

http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/reference/extension-points/org_eclipse_ui_ popupMenus.html

and of

http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench_cmd_menus.htm

You also find some descriptions in

http://www.eclipse.org/articles/viewArticle/ViewArticle2.htm l

(search for registerContextMenu)

HTH & Greetings from Bremen,

Daniel Krügler
Re: multiple context menu's in one editor [message #544754 is a reply to message #544675] Mon, 05 July 2010 12:35 Go to previous message
Henno Vermeulen is currently offline Henno VermeulenFriend
Messages: 126
Registered: July 2009
Senior Member
Ok it works! Without setting the selection provider on the entire site,
HandlerUtil.getCurrentSelection(event)

returns null.

But I found out that
HandlerUtil.getActiveMenuSelection(event)


Returns the selection of the ISelectionProvider (TableViewer) I associated with the popup menu.

But how can I properly re-use this command from a button inside my editor? Then the popup menu will not be active. I think I'll just solve this by making a "removeSelectedConcreteObject" method in my editor...

[Updated on: Mon, 05 July 2010 12:45]

Report message to a moderator

Previous Topic:hyperlinkDetectors and hyperlinkDetectorTargets
Next Topic:Adding new icon to Title Bar
Goto Forum:
  


Current Time: Fri Mar 29 11:01:41 GMT 2024

Powered by FUDForum. Page generated in 0.04329 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top