Gangmax Blog

Get current project reference in Eclipse

我今天需要一个功能:在Eclipse的form page中得到当前Eclipse project的引用,找了半天在这里找到的答案:

  1. 从编辑器获得当前编辑的文件:

    1
    2
    3
    4
    5
    IEditorPart editor = ((DefaultEditDomain)(parent.getViewer().getEditDomain())).getEditorPart();
    IEditorInput input = editor.getEditorInput();
    if(input instanceof IFileEditorInput){
    IFile file = ((IFileEditorInput)input).getFile();
    }
  2. 通过文件得到工程Project:

    1
    IProject project = ((IFile)o).getProject(); 

Comments