I'm currently developing an Eclipse Neon editor plug-in. At the moment I'm trying to be able to open files from the filesystem, which weren't created inside of Eclipse. To accomplish that, I need to get an IProject
in the following method:
我目前正在开发Eclipse Neon编辑器插件。目前我正在尝试从文件系统中打开文件,这些文件不是在Eclipse内部创建的。为此,我需要通过以下方法获取IProject:
public static IProject getProject(IStorage storage) {
if (storage instanceof IFile) {
return ((IFile) storage).getProject();
}
else if (storage instanceof IJarEntryResource) {
return ((IJarEntryResource) storage).getPackageFragmentRoot().getJavaProject().getProject();
}
else if (storage instanceof FileStorage) {
// ????
}
else {
throw new IllegalArgumentException("Unknown IStorage implementation");
}
}
Here FileStorage
is an own implementation of the IStorage
interface. And looks like that:
这里FileStorage是IStorage接口的自己实现。看起来像那样:
public class FileStorage implements IStorage {
private final FileStoreEditorInput editorInput;
FileStorage( FileStoreEditorInput editorInput ) {
this.editorInput = editorInput;
}
@Override
public <T> T getAdapter( Class<T> adapter ) {
return Platform.getAdapterManager().getAdapter( this, adapter );
}
@Override
public boolean isReadOnly() {
return false;
}
@Override
public String getName() {
return editorInput.getName();
}
@Override
public IPath getFullPath() {
return new Path( URIUtil.toFile( editorInput.getURI() ).getAbsolutePath() );
}
@Override
public InputStream getContents() {
try {
return editorInput.getURI().toURL().openStream();
} catch( IOException e ) {
throw new UncheckedIOException( e );
}
}
}
Is there any way to get an IProject
from this FileStorage
?
有没有办法从这个FileStorage获取IProject?
2 个解决方案
#1
1
You can try and get an IFile
for a file using:
您可以尝试使用以下方法获取文件的IFile:
IPath path = ... absolute path to the file
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IFile file = root.getFileForLocation(path);
if (file != null) {
IProject project = file.getProject();
...
}
But this will only work for a file inside the workspace. For anything outside of the workspace you can't have a project.
但这只适用于工作区内的文件。对于工作区以外的任何内容,您都无法拥有项目。
#2
1
In short: No. The FileStorage
class is meant to represent IStorage
instances for files that are located outside of the workspace. Therefore they are not contained in any workspace project and it is not possible to obtain an IProject
for them.
简而言之:No。FileStorage类用于表示位于工作区外部的文件的IStorage实例。因此,它们不包含在任何工作空间项目中,并且无法为它们获取IP项目。
#1
1
You can try and get an IFile
for a file using:
您可以尝试使用以下方法获取文件的IFile:
IPath path = ... absolute path to the file
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IFile file = root.getFileForLocation(path);
if (file != null) {
IProject project = file.getProject();
...
}
But this will only work for a file inside the workspace. For anything outside of the workspace you can't have a project.
但这只适用于工作区内的文件。对于工作区以外的任何内容,您都无法拥有项目。
#2
1
In short: No. The FileStorage
class is meant to represent IStorage
instances for files that are located outside of the workspace. Therefore they are not contained in any workspace project and it is not possible to obtain an IProject
for them.
简而言之:No。FileStorage类用于表示位于工作区外部的文件的IStorage实例。因此,它们不包含在任何工作空间项目中,并且无法为它们获取IP项目。