2016-09-23 19 views
0

Я пишу плагин Eclipse, который требует, чтобы я получил полный путь к любому файлу, открытому в рабочей области.Eclipse PDE: Получить полный путь к внешнему файлу, открытому в Workbench

Я могу получить полный путь к любому файлу, который является частью любого проекта Eclipse. Код для открытия открытого/активного файла редактора из рабочей области.

public static String getActiveFilename(IWorkbenchWindow window) { 
     IWorkbenchPage activePage = window.getActivePage(); 
     IEditorInput input = activePage.getActiveEditor().getEditorInput(); 
     String name = activePage.getActiveEditor().getEditorInput().getName(); 
     PluginUtils.log(activePage.getActiveEditor().getClass() +" Editor."); 
     IPath path = input instanceof FileEditorInput ? ((FileEditorInput) input).getPath() : null; 
     if (path != null) { 
      return path.toPortableString(); 
     } 
     return name; 
    } 

Однако, если какой-либо файл drag-dropped in Workspace или открыт с помощью File -> Open File. Например, я открыл файл из /Users/mac/log.txt из File -> Open File. Мой плагин не может найти местоположение этого файла.

ответ

0

После нескольких дней поиска я нашел ответ, посмотрев исходный код Eclipse IDE.

В среде IDE.class Eclipse пытается найти подходящий ввод редактора в зависимости от файла рабочей области или внешнего файла. Eclipse обрабатывает файлы в рабочей области, используя FileEditorInput и внешние файлы, используя FileStoreEditorInput. Фрагмент кода ниже:

/** 
    * Create the Editor Input appropriate for the given <code>IFileStore</code>. 
    * The result is a normal file editor input if the file exists in the 
    * workspace and, if not, we create a wrapper capable of managing an 
    * 'external' file using its <code>IFileStore</code>. 
    * 
    * @param fileStore 
    *   The file store to provide the editor input for 
    * @return The editor input associated with the given file store 
    * @since 3.3 
    */ 
    private static IEditorInput getEditorInput(IFileStore fileStore) { 
     IFile workspaceFile = getWorkspaceFile(fileStore); 
     if (workspaceFile != null) 
      return new FileEditorInput(workspaceFile); 
     return new FileStoreEditorInput(fileStore); 
    } 

Я изменил код размещен в вопросе, чтобы обрабатывать оба файла в рабочей области и внешнем файле.

public static String getActiveEditorFilepath(IWorkbenchWindow window) { 
     IWorkbenchPage activePage = window.getActivePage(); 
     IEditorInput input = activePage.getActiveEditor().getEditorInput(); 


     String name = activePage.getActiveEditor().getEditorInput().getName(); 
     //Path of files in the workspace. 
     IPath path = input instanceof FileEditorInput ? ((FileEditorInput) input).getPath() : null; 
     if (path != null) { 
      return path.toPortableString(); 
     } 

     //Path of the externally opened files in Editor context. 
     try { 
      URI urlPath = input instanceof FileStoreEditorInput ? ((FileStoreEditorInput) input).getURI() : null; 
      if (urlPath != null) { 
       return new File(urlPath.toURL().getPath()).getAbsolutePath(); 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 

     //Fallback option to get at least name 
     return name; 
    }