【发布时间】:2012-06-16 12:45:18
【问题描述】:
我在 Eclipse 中使用 Open with menu 打开一个编辑器。但我无法获取当前选定文件的路径。有时它会提供正确的路径,但有时会抛出空指针异常。 我正在编写以下代码来获取选定的文件路径。
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;
}
【问题讨论】:
-
您是否要在编辑器中获取该文件路径?
-
其实我想在浏览器中传递那个路径来设置 URL。
标签: eclipse-plugin