hengxin
hengxin

Reputation: 1999

How to contribute a context menu to a custom view in an eclipse plug-in project?

In an eclipse plug-in project, I contributed a view named Favorites with id com.qualityeclipse.favorites.views.FavoritesView.

Then I want to contribute a context menu to the Favorites view by using popup:com.qualityeclipse.favorites.views.FavoritesView?after=additions.
However, no context menu shows up upon a right-click within the Favorites view.

I changed it to popup:org.eclipse.ui.popup.any?after=additions for test. This time a context menu appears as expected in other views (such as Problems, Console, and Declaration) than my own Favorites view.

How to contribute a context menu to a custom view?

Upvotes: 0

Views: 454

Answers (2)

greg-449
greg-449

Reputation: 111142

You must create a context menu in your view code and register it with the view site. Something like:

ISelectionProvider provider = ... a selection provider such as a TreeViewer or TableViewer

Control control = control to own the menu - usually the TreeViewer or TableViewer control

MenuManager menuMgr = new MenuManager("#PopUp");
menuMgr.setRemoveAllWhenShown(true);

menuMgr.addMenuListener(new IMenuListener() {
    @Override
    public void menuAboutToShow(IMenuManager manager) {
       // Additions placeholder
       manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));

       // Note: you can add other menu items directly here
    }
});

Menu menu = menuMgr.createContextMenu(control);
control.setMenu(menu);

// register the context menu such that other plug-ins may contribute to it
getSite().registerContextMenu(menuMgr, provider);

Upvotes: 1

flafoux
flafoux

Reputation: 2110

You need to use :

popup:org.eclipse.ui.popup.any?after=additions

But then, add some conditions to each command (with right click > New > Visible When :

This one make it visible when the active part is your view

     <visibleWhen
        checkEnabled="false">
     <with
           variable="activePartId">
        <equals
              value="com.qualityeclipse.favorites.views.FavoritesView">
        </equals>
     </with>
  </visibleWhen>

Upvotes: 0

Related Questions