2012-01-09 205 views
3

我在Eclipse中打开一个带菜单打开的编辑器,但我无法获取当前选定文件的路径。有时,它会给出正确的路径,但有时会抛出空指针例外。 我正在编写下面的代码来获取选定的文件路径。如何在Eclipse插件开发中获取当前所选文件的路径

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; 


     } 
+0

您是否试图在编辑器中获取该文件路径? – 2012-01-09 10:07:25

+0

其实我想通过浏览器中的路径来设置URL。 – Eshika 2012-01-09 10:24:54

回答

0

您可以向活动编辑器询问底层文件的路径。只需将IPartListener注册到活动的IWorkbenchPage,并在激活零件时询问该听众。这里是一个片段

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 

      } 
     }); 
+0

如果我尝试访问这个路径之外的方法,它给了我这个错误“最后的局部变量getnewpath不能被分配,因为它是在一个封闭的类型定义”我保存file.getLocation字符串getnewpath – Eshika 2012-01-09 10:27:58

+0

请参阅http:// stackoverflow.com/questions/5977735/setting-outer-variable-from-anonymous-inner-class – 2012-01-09 10:50:51

4

我找到了答案在这似乎更容易和我的作品迄今Eclipse forum

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. 
} 

其中一些类需要新的项目引用,所以这里列出了我所有的该类导入。当然,并不是所有这些都与这个片段有关。

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; 
相关问题