[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [pde-dev] Re: multiple dynamic popupMenus
|
Hi,
Thanks for your reply. I have done the following:
- add an ObjectContribution, with an Action implementing
IObjectActionDelegate and IMenuCreator
- add a menuListener to the parent action.
This works fine when I stay in the same plugin as the plugin
registering the context menu. But when I add an objectContribution
from within another plugin the menu is not dynamically added. The
IObjectActionDelegate is only instanciated when I click on it (so I
cannot add a menuListener to it) Anyone knows how this is possible?
This is the code registering the context menu:
private void initializePopUpMenu() {
MenuManager manager = new MenuManager();
manager.setRemoveAllWhenShown(true);
manager.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager mgr) {
fillContextMenu(mgr);
}
});
Menu fTextContextMenu = manager.createContextMenu(treeViewer
.getControl());
treeViewer.getControl().setMenu(fTextContextMenu);
// register the menu to the site so that we can allow
// actions to be plugged in
getSite().registerContextMenu(manager, this.treeViewer);
System.out.println(manager.getId());
}
The code adding the dynamic menu:
public class MenuadderAction implements IObjectActionDelegate,IMenuCreator {
...
public Menu getMenu(Menu parent) {
fMenu = new Menu(parent);
// Dynamically update menu when it shows (selection might have changed)
fMenu.addMenuListener(new MenuListener() {
public void menuShown(MenuEvent e) {
updateMenu(fMenu);
}
public void menuHidden(MenuEvent e) {
}
});
return fMenu;
}
public Menu getMenu(Control parent) {
// NEVER USED
return null;
}
/**
* When a menu is shown, remove all existing items on it,
* add the needed ones
*/
private void updateMenu(Menu m) {...}
public void setActivePart(IAction action, IWorkbenchPart workbenchpart) {
action.setMenuCreator(this);
}
public void setActiveEditor(IAction action, IEditorPart editorpart) {
}
public void selectionChanged(IAction action, ISelection myselection) {
this.selection = (StructuredSelection) myselection;
action.setMenuCreator(this);
}
This works fine in one plugin (the same as the one registering the
context menu). When the popup menu is shown, all actions are
immediately instanciated. When I do the same in another plugin, the
action is only instanciated when I click on it.
Anyone has an idea how this is possible?
Regards,
Leen
On 10/14/05, Mihael Knezevic <m.knezevic@xxxxxxxx> wrote:
> 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.
>
>
>
> _______________________________________________
> pde-dev mailing list
> pde-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/pde-dev
>