Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Adding programmatically a MenuItem to a pop-up ContextMenu(I want to mix menu items defined via commands with programmatically defined menu items)
Adding programmatically a MenuItem to a pop-up ContextMenu [message #723423] Thu, 08 September 2011 14:13 Go to next message
christian is currently offline christianFriend
Messages: 3
Registered: September 2011
Junior Member
Hi!

I was hoping somebody could help me with a question regarding popup menus and commands.
I have a view with multiple table viewers. Each table has a popup menu with several menu items defined via Eclipse commands.
For some of the commands I need to know which viewer actually sent the command.
This is quite simple of course when you add the popup menus directly to the viewer without using the command framework. But so far I found no way how I could find out in the command handler which of my viewer sent the command.

I tried a trick by using the current cursor position and checking the bounds of my viewers, but this is a rather unreliable solution.

My idea is now to add some menu items programmatically to the popup menus along with the command defined menu items. But somehow the additional menu items never show up?

The relevant code in the view looks like this:
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
...
MenuManager menuManager = new MenuManager();
Menu menu = menuManager.createContextMenu(viewer.getTable());
// the additional menu items
MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
menuItem.setText("Test");
// Set the MenuManager
viewer.getTable().setMenu(menu);
getSite().registerContextMenu(menuManager, viewer);


I would expect that the menu item will be added, but it doesn't!
It would only show up when I create the menu via:
Menu menu = new Menu(getSite().getShell(), SWT.POP_UP);

But in this case I will only see the 'Test' menu item and not my command defined items, which doesn't help either.

I would really appreciate a good tip!

Thanks
Re: Adding programmatically a MenuItem to a pop-up ContextMenu [message #723432 is a reply to message #723423] Thu, 08 September 2011 14:31 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by:

On 2011-09-08 16:13, christian wrote:
> Hi!
>
> I was hoping somebody could help me with a question regarding popup
> menus and commands.
> I have a view with multiple table viewers. Each table has a popup menu
> with several menu items defined via Eclipse commands. For some of the
> commands I need to know which viewer actually sent the command. This is
> quite simple of course when you add the popup menus directly to the
> viewer without using the command framework. But so far I found no way
> how I could find out in the command handler which of my viewer sent the
> command.
>
> I tried a trick by using the current cursor position and checking the
> bounds of my viewers, but this is a rather unreliable solution.

Arrgh, don't do this!

> My idea is now to add some menu items programmatically to the popup
> menus along with the command defined menu items. But somehow the
> additional menu items never show up?
>
> The relevant code in the view looks like this:
>
> viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
> ..
> MenuManager menuManager = new MenuManager();
> Menu menu = menuManager.createContextMenu(viewer.getTable());
> // the additional menu items
> MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
> menuItem.setText("Test");
> // Set the MenuManager
> viewer.getTable().setMenu(menu);
> getSite().registerContextMenu(menuManager, viewer);

If you have multiple context menus per view, you need to add them with
an explicit menu id. Unless you do this, the view id will be used and
this does not help you. Create the menu manager via

MenuManager menuManager = new MenuManager(null, your_id);

where your_id is a unique identifier for the given context menu.

Also, use

getSite().registerContextMenu(your_id, menuManager, viewer);

to register the context menu via this very same id.

> I would expect that the menu item will be added, but it doesn't!
> It would only show up when I create the menu via:
>
> Menu menu = new Menu(getSite().getShell(), SWT.POP_UP);

You should not invoke the popup manually - let the framework do this for
you. You should not even call

viewer.getTable().setMenu(menu);

because this is automatically done. I also recommend to use the
menu-listener idiom, like so:

menuManager.setRemoveAllWhenShown(true);
menuManager.addMenuListener(new IMenuListener() {
@Override
public void menuAboutToShow(IMenuManager manager) {
// Fill manager here;
}
});

instead of creating static menus like:

MenuItem menuItem = new MenuItem(menu, SWT.PUSH);

> But in this case I will only see the 'Test' menu item and not my command
> defined items, which doesn't help either.
>
> I would really appreciate a good tip!

If you are within the command to can discriminate the different viewers,
when you have registered the underlying viewer control via
IFocusService.addFocusTracker with some unique id (don't forget to
remove the tracker later via removeFocusTracker). Then (in the command
handler use HandlerUtil.getVariable via the constant
ISources.ACTIVE_FOCUS_CONTROL_ID_NAME to verify the id. You can use
ISources.ACTIVE_FOCUS_CONTROL_NAME to get the control itself.

HTH & Greetings from Bremen,

Daniel Krügler
Re: Adding programmatically a MenuItem to a pop-up ContextMenu [message #723456 is a reply to message #723432] Thu, 08 September 2011 15:30 Go to previous message
christian is currently offline christianFriend
Messages: 3
Registered: September 2011
Junior Member
Wow, amazing reply! Thank's a lot! I will try your suggestion and will give feedback.

Gruesse aus Singapur von einem Oesterreicher

Christian
Previous Topic:'Workbench has not yet been created' issue
Next Topic:Java KeyListener problem
Goto Forum:
  


Current Time: Sat Apr 20 03:35:21 GMT 2024

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

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

Back to the top