.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

Create an command for opening editor from navigator


As you know, navigator view of an eclipse RCP application default allows you open an editor such a text editor. That is one of default editors, how about your own editor. Using command is one of the ways. By every step, we have:
I assume that I have an editor from the class FormEditor, at here we have its ID_EDITOR, besides I have also an Navigator with ID_NAVIGATOR
First, declare a command in extension points
In plugin.xml we declare:

<extension
         point="org.eclipse.ui.commands">
      <command
            defaultHandler="com.my.example.OpenEditorHandler"
            id="com.my.example.OpenEditor"
            name="Open Editor">
      </command>
   </extension>


As you can see, we need an class handler of this command named OpenEditorHandler. So,
Second, create the handler
it is simply a extension of AbstractHandler as other command. Something need to be done in this class is, we have to get out what file is selected by the SeclectionService, then we open the editor with the opened as an editor input.

public class OpenConnectionEditor extends AbstractHandler
{

    /**
     * {@inheritDoc}
     *
     * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
     */
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException
    {
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
        IWorkbenchPage page = window.getActivePage();
        IViewPart view = page.findView(ID_NAVIGATOR);
        ISelection selection = view.getSite().getSelectionProvider().getSelection();
        if (selection != null && selection instanceof IStructuredSelection)
        {
            Object obj = ((IStructuredSelection) selection).getFirstElement();
            if (obj != null)
            {
                IFile file = (IFile) obj;
                FileEditorInput input = new FileEditorInput(file);
                try
                {
                    page.openEditor(input, ID_EDITOR);

                }
                catch (PartInitException e)
                {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

}

That’s all, the rest of the job is in the navigator view, we use this command in any event handle. In the navigator which extends from CommonNavigator supports handler for double click event

@Override
    protected void handleDoubleClick(DoubleClickEvent event)
    {

        if (event.getSelection() instanceof StructuredSelection)
        {
            StructuredSelection structuredSelection = (StructuredSelection) event.getSelection();
            if (structuredSelection.getFirstElement() instanceof IFile)
            {
                IHandlerService handlerService = (IHandlerService) GEMAWorkspace.this.getSite().getService(
                    IHandlerService.class);
                try
                {
                    handlerService.executeCommand("com.my.example.OpenEditor", null);
                }
                catch (Exception ex)
                {
                    throw new RuntimeException("com.my.example.OpenEditor not found");
                }
            }
            else
            {
                super.handleDoubleClick(event);
            }
        }
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: