Eclipse RCP Tutorial: Add popup menu to editor context

1. What the popup menu looks like in an eclipse editor?
This tutorial shows how to add a popup menu item to an editor context. In this example, a menu item called “ShowIFile” is added like the following:

add-popup-menu-editor-context

2. Steps for adding the popup menu in editor context
The following are snapshots of every step involved when creating a popup menu by using PDE.

rcp-popup-menu-1

rcp-popup-menu-2

rcp-popup-menu-3

rcp-popup-menu-4

The most important part is the following xml file. The plug-in.xml file should be changed to the following:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.ui.popupMenus">
      <objectContribution
            objectClass="org.eclipse.ui.IEditorInput"
            nameFilter="*.*"
            id="SimplePopupMenu.contribution1">
         <menu
               label="ShowIFile"
               path="additions"
               id="SimplePopupMenu.menu1">
            <separator
                  name="group1">
            </separator>
         </menu>
         <action
               label="SimpleMessageAction"
               class="simplepopupmenu.popup.actions.SimpleMessageAction"
               menubarPath="SimplePopupMenu.menu1/group1"
               enablesFor="1"
               id="SimplePopupMenu.newAction">
         </action>
      </objectContribution>
   </extension>
</plugin>

The objectContribution make the menu appear in a popup menu for views or editors. In this example, objectClass=”org.eclipse.ui.IEditorInput” make the popup menu appear in an editor.

When you run the plug-in now, the snapshot shown at the beginning is shown up.

As it is configured in the xml file above, when the menu item is clicked, the workbench will run the specified class – SimpleMessageAction. Because the popup is declared as an objectContribution, the supplied class must implement IObjectActionDelegate. In this example plug-in, it should shows a simple message.

3. Get the selection from editor

Instead of doing nothing useful, let’s change the SimpleMessageAction.java class and make it do something useful.

Let’s get the selection information of text.

package simplepopupmenu.popup.actions;
 
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.texteditor.AbstractTextEditor;
 
 
import simplepopupmenu.Activator;
 
 
public class SimpleMessageAction implements IObjectActionDelegate {
	private Shell shell;
 
	public SimpleMessageAction() {
		super();
	}
 
	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}
 
 
	@Override
	public void run(IAction action) {
		try {
			//get editor
			IEditorPart editorPart = Activator.getDefault().getWorkbench()
					.getActiveWorkbenchWindow().getActivePage()
					.getActiveEditor();
 
			if (editorPart instanceof AbstractTextEditor) {
				int offset = 0;
				int length = 0;
				String selectedText = null;
				IEditorSite iEditorSite = editorPart.getEditorSite();
				if (iEditorSite != null) {
					//get selection provider
					ISelectionProvider selectionProvider = iEditorSite
							.getSelectionProvider();
					if (selectionProvider != null) {
						ISelection iSelection = selectionProvider
								.getSelection();
						//offset
						offset = ((ITextSelection) iSelection).getOffset();
						if (!iSelection.isEmpty()) {
							selectedText = ((ITextSelection) iSelection)
									.getText();
							//length
							length = ((ITextSelection) iSelection).getLength();
							System.out.println("length: " + length);
							 MessageDialog.openInformation(
							         shell,
							         "Do Something Menu",
							         "Length: " + length + "    Offset: " + offset);
						}
					}
				}
 
			}
		} catch (Exception e) {		}
	}
 
 
	@Override
	public void selectionChanged(IAction action, ISelection selection) {
	}
 
}

If you run the plug-in now, the following will show up.

text-selection-in-editor-eclipse

3 thoughts on “Eclipse RCP Tutorial: Add popup menu to editor context”

  1. The org.eclipse.ui.editors and org.eclipse.jface.text plugins contain the AbstractTextEditor and ITextSelection classes; be sure to add those plugins in the Dependencies tab of your manifest.

  2. I am not getting the following dependencies for the same code. I have followed the same procedure and steps as mentioned in the blog. Kindly help on this

Leave a Comment