Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[pde-dev] Re: multiple dynamic popupMenus

Leen Toelen <toelen@...> writes:

> 
> Hi,
> 
> I have developed a TreeViewer and attached some objectContributions to
> the context menu. This works fine, I have added some markers to the
> menu, one of which MB_ADDITIONS. Now, between two separators, I have
> to dynamically add menu items based on the selection. Sort of how the
> "Open with>" menu works. The menu is shown, I do some database
> lookups, and add a menu for each row found in the database.
> Is this possible with the standard extension points?
> 
> Regards,
> Leen Toelen
> 


extend the view class in the following way:

public void createPartControl(Composite parent) {
.
.
.

createContextMenu();
}

private void createContextMenu()
{
// Create menu manager.
MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
       public void menuAboutToShow(IMenuManager mgr) {
               fillContextMenu(mgr);
       }
   });
       
// Create menu.
Menu menu = menuMgr.createContextMenu(viewer.getControl());
viewer.getControl().setMenu(menu);
      
// Register menu for extension.
getSite().registerContextMenu(menuMgr, viewer);
}

private void fillContextMenu(IMenuManager mgr)
{
	MenuManager contextAssignMenu = new MenuManager("assign");
		
	String selection = ((StructuredSelection)
viewer.getSelection()).getFirstElement().toString();

// get your stuff from the database
	String[] teams = Database.getStuff(selection);

	for (int i = 0; i < teams.length; i++)
	{
		Action contextAction = new Action(
				teams[i])
		{
			public void run()
			{
				super.run();
			}
		};
		contextAction.setActionDefinitionId(teams[i]);
			
		contextAssignMenu.add(contextAction);
	}
       
	contextAssignMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		
        mgr.add(contextAssignMenu);
	mgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
}


i hope this help. it won't fit to your code 100% but you should grasp the idea
behind it.





Back to the top