簡體   English   中英

從自定義IStorage實現獲取IProject

[英]Get IProject from a custom IStorage implementation

我目前正在開發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");
    }
  }

這里FileStorageIStorage接口的自己的實現。 看起來像這樣:

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 );
    }
  }
}

有什么辦法可以從此FileStorage獲取IProject嗎?

簡而言之:否FileStorage類用於表示位於工作區外部的文件的IStorage實例。 因此,它們不包含在任何工作區項目中,並且無法為其獲取IProject

您可以嘗試使用以下方法獲取文件的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();
  ...
}

但這僅適用於工作空間中的文件。 對於工作空間之外的任何內容,您都無法擁有項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM