2016-09-23 3 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からファイルを開いたとします。私のプラグインはこのファイルの場所を見つけることができません。

答えて

0

検索の数日後、私はEclipse IDEのソースコードを見て答えを見つけました。

IDE.classでは、Eclipseはワークスペースファイルまたは外部ファイルに応じて適切なエディタ入力を探します。 EclipseはFileEditorInputを使用してワークスペース内のファイルを処理し、FileStoreEditorInputを使用する外部ファイルを処理します。以下のコードスニペット:

私はWorkspaceと外部ファイルの両方のファイルを処理するために質問に投稿されたコードを変更しました。

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; 
    } 
関連する問題