capint
10/23/2016 - 3:10 PM

Eclipse >> Register popup menu programmatically to a view

Eclipse >> Register popup menu programmatically to a view

<menuContribution locationURI="popup:menuID?after=mySeperator">
	<command commandId="xxx" style="push" icon="icons/full/obj16/xxx.gif">
    ...
	</command>
</menuContribution>

<!-- Seperators can be contributed to all popup menus as follows: -->
<menuContribution locationURI="popup:org.eclipse.ui.popup.any?after=additions">
	<separator name="mySeparator" visible="true">
	</separator>
</menuContribution>
//Source: http://www.vogella.com/tutorials/EclipseCommands/article.html
public class View extends ViewPart {
  private TableViewer viewer;

  @Override
  public void createPartControl(Composite parent) {
    viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    
    //***NOTE: In Eclipse 4.5, cannot associate 1 MenuManager to different Viewer
    // Create a menu manager and create context menu
    MenuManager menuManager = new MenuManager();
    Menu menu = menuManager.createContextMenu(viewer.getTable());
    // set the menu on the SWT widget
    viewer.getTable().setMenu(menu);
    // register the menu with the framework
    //*** If we have many menus on the same viewer, use getSite().registerContextMenu(menuID, menuManager, viewer);
    getSite().registerContextMenu(menuManager, viewer);

    // make the viewer selection available
    getSite().setSelectionProvider(viewer);
  }
}