3

I am opening an Editor with Open with menu in Eclipse.But i am not able to get path of current selected file.Sometimes it gives proper path but sometimes throws null pointer Exception. I am writing following code to get selected file path.

IWorkbenchPage iwPage=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
     System.err.println("iwpage::"+iwPage);
     ISelection selection=iwPage.getSelection();
        System.err.println("selection::::testtttt"+selection.toString());
        if(selection!=null && selection instanceof IStructuredSelection)
        {
            IStructuredSelection selectedFileSelection = (IStructuredSelection) selection;
            System.out.println(selection.toString());
            Object obj = selectedFileSelection.getFirstElement();

            selectedFile=(IResource)obj;
            System.err.println("selection::::"+selectedFile.getLocation().toString());
            String html=selectedFile.getLocation().toString().replace(" ","%20");
            String html_file="file:///"+html;
            return html_file;


        }
skaffman
  • 398,947
  • 96
  • 818
  • 769
Eshika
  • 321
  • 1
  • 8
  • 20

2 Answers2

4

I found an answer in the Eclipse forum that seems easier and works for me so far.

IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow window = 
        workbench == null ? null : workbench.getActiveWorkbenchWindow();
IWorkbenchPage activePage = 
        window == null ? null : window.getActivePage();

IEditorPart editor = 
        activePage == null ? null : activePage.getActiveEditor();
IEditorInput input = 
        editor == null ? null : editor.getEditorInput();
IPath path = input instanceof FileEditorInput 
        ? ((FileEditorInput)input).getPath()
        : null;
if (path != null)
{
    // Do something with path.
}

Some of those classes required new project references, so here's a list of all my imports for that class. Not all of them are related to this snippet, of course.

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.source.CompositeRuler;
import org.eclipse.jface.text.source.LineNumberRulerColumn;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.osgi.framework.Bundle;
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
0

You can ask the active editor for the path of the underlying file. Just register an IPartListener to your active IWorkbenchPage and ask withing this listener when activating a part. Here is a snippet

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
        .addPartListener(new IPartListener() {

            @Override
            public void partOpened(IWorkbenchPart part) {
                // TODO Auto-generated method stub

            }

            @Override
            public void partDeactivated(IWorkbenchPart part) {
                // TODO Auto-generated method stub

            }

            @Override
            public void partClosed(IWorkbenchPart part) {
                // TODO Auto-generated method stub

            }

            @Override
            public void partBroughtToTop(IWorkbenchPart part) {
                if (part instanceof IEditorPart) {
                    if (((IEditorPart) part).getEditorInput() instanceof IFileEditorInput) {
                        IFile file = ((IFileEditorInput) ((EditorPart) part)
                                .getEditorInput()).getFile();
                        System.out.println(file.getLocation());
                    }
                }

            }

            @Override
            public void partActivated(IWorkbenchPart part) {
                // TODO Auto-generated method stub

            }
        });
Tom Seidel
  • 9,525
  • 1
  • 26
  • 38
  • If i try to access this path outside of method it gives me this error "The final local variable getnewpath cannot be assigned, since it is defined in an enclosing type" I am saving file.getLocation to string getnewpath – Eshika Jan 09 '12 at 10:27
  • see http://stackoverflow.com/questions/5977735/setting-outer-variable-from-anonymous-inner-class – Tom Seidel Jan 09 '12 at 10:50