我正在研究eclipse插件,我必须从项目资源管理器中打开一个文件。 假设我在项目资源管理器中有一个项目ABC。右键单击项目后,我有一个选项来运行我的插件工具。在处理之后我得到了一些结果,比如检查文件xyz.java。
现在我想通过代码
在IDE中打开此文件我正在使用这个
File absolute = new File("/Decider.java");
File file = new File("/Decider.java");
IFileStore fileOnLocalDisk = EFS.getLocalFileSystem().getStore(absolute.toURI() );
FileStoreEditorInput editorInput = new FileStoreEditorInput(fileOnLocalDisk);
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
try {
page.openEditor(editorInput, "org.eclipse.ui.DefaultTextEditor");
page.openEditor(editorInput, "MyEditor.editor");
IFileStore fileStore = EFS.getLocalFileSystem().getStore(absolute.toURI() );
IDE.openEditorOnFileStore( page, fileStore );
} catch (PartInitException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
System.out.println(file.getCanonicalPath());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
IPath path = new Path(" /DirectoryReader.java");
IFile sampleFile = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
IEditorInput editorInput1 = new FileEditorInput(sampleFile);
IWorkbenchWindow window1=PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page1 = window1.getActivePage();
try {
page1.openEditor(editorInput1, "org.eclipse.ui.DefaultTextEdtior");
} catch (PartInitException e1) {
e1.printStackTrace();
}
这里它在c盘中创建了一个名为decider的新文件,这意味着它的路径错误。
但是当我在一些独立的java文件中使用路径代码作为普通的 JAVA 项目时,它正在获得正确的路径。
答案 0 :(得分:8)
对于工作区中的文件,您应该使用IFile
。如果您从Project Explorer或其他视图中选择了IFile
或可以适应IFile
的视图。
如果您只有工作空间相对路径,请使用ResourcesPlugin.getWorkspace().getRoot().getFile(path)
(路径包含项目)。
要打开文件内容的默认编辑器,请使用
IDE.openEditor(page, file, true);
打开特定的编辑器使用
IDE.openEditor(page, file, "editor id");
IDE是org.eclipse.ui.ide.IDE。