簡體   English   中英

Eclipse PDE,Navigator View,TreeSelection - 獲取文件類型和名稱

[英]Eclipse PDE, Navigator View, TreeSelection - Obtaining the file type and name

我正在嘗試在“導航器樹”視圖中獲取Eclipse用戶的結構化選擇的詳細信息。 目前我有以下基於org.eclipse.ui.popMenus擴展點的內容:

public void run(IAction action) {

Shell shell = new Shell();

ISelection selection = workbenchPart.getSite().getSelectionProvider().getSelection();

if (structuredSelection instanceof org.eclipse.jface.viewers.TreeSelection) {

   org.eclipse.jface.viewers.TreeSelection treeSelection = (org.eclipse.jface.viewers.TreeSelection) structuredSelection;
   IAdaptable firstElement = (IAdaptable) treeSelection.getFirstElement();

   // Relies on an internal API, bad juju
   if (firstElement instanceof org.eclipse.jdt.internal.core.CompilationUnit) {
    org.eclipse.jdt.internal.core.CompilationUnit compilationUnit = (org.eclipse.jdt.internal.core.CompilationUnit) firstElement;                                   
    String editorSelection = new String(compilationUnit.getContents());
   }            
}

這個問題是它目前已經加入了JDT編譯單元API,它是內部的,並且對我想要的東西太具體了。

理想情況下,我希望能夠獲得基礎文件名,類型和內容,而不必依賴:

  1. 內部API
  2. JDT編譯單元代碼。

這樣,當用戶右鍵單擊導航器視圖中的文件時,這將允許我獲取通用文件的屬性。

有人可以向我提供有關我如何做到這一點的任何指示嗎?

[編輯:我添加了以下替代方案 - 最初的答案是父親失望]

首先:如果您在Package Explorer中選擇了某些內容,則所選項目都是Java Model對象 - 您必須在某個級別處理它們。 有兩種方法可以解決這個問題:

  1. 直接使用ICompilationUnit(參見更遠的地方)
  2. 創建一個Eclipe適配器工廠以自動執行轉換

適配器工廠方法

你可以創建一個適配器工廠(它可以存在於你的主插件或不同的插件中),eclipse可以使用它來自動從ICompilationUnit轉換為IFile。

注意:如果您在其他插件中創建適配器工廠,則可能需要為其設置早期啟動以使適配器工廠加載。 否則,你需要讓你的插件適用於選擇取決於提供適配器的插件。

關於適配器的一些很好的細節在http://www.eclipse.org/resources/resource.php?id=407 ,但我將在這里討論這個問題的實現。

依賴

將托管適配器的插件需要以下依賴項

  • org.eclispe.core.resources
  • org.eclipse.jdt.core

適配器工廠類

在新插件中定義以下類

package com.javadude.foo;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.jdt.core.ICompilationUnit;

public class CompilationUnitToFileAdapter implements IAdapterFactory {
    @Override
    public Object getAdapter(Object adaptableObject, Class adapterType) {
        if (adaptableObject instanceof ICompilationUnit)
            // note: "adapting" it here just means returning the ref'd IFile
            return (IFile) ((ICompilationUnit)adaptableObject).getResource();
        return null;
    }
    @Override
    public Class[] getAdapterList() {
        return new Class[] {IFile.class};
    }
}

擴展名

在將托管適配器工廠的插件中,將以下內容添加到plugin.xml:

<extension point="org.eclipse.core.runtime.adapters">
    <factory
        adaptableType="org.eclipse.jdt.core.ICompilationUnit"
        class="com.javadude.foo.AdapterFactory1">
        <adapter type="org.eclipse.core.resources.IFile" />
    </factory>
</extension>

使用適配器

有了上述內容,您現在可以寫:

Object firstElement = ((ITreeSelection) selection).getFirstElement();
IFile file = (IFile) Platform.getAdapterManager().
                         getAdapter(firstElement, IFile.class);
if (file == null)
    // adapter not present; cannot use as IFile
else
    // adapter present - you can use it as an IFile

使用此方法,您可以添加其他適配器以將其他類型轉換為IFile,並且您的選擇代碼無關緊要。


直接ICompilationUnit方法

[編輯:我已經改變了答案,但是將以下內容作為參考信息b / c,這是探索在包瀏覽器中選擇的編譯單元內容的標准方法]

這實際上是獲取包瀏覽器中文件內容的首選方法...

您應該使用ICompilationUnit,而不是使用CompilationUnit。 大多數eclipse API使用接口進行公共消費,使用類來獲取內部細節。

如果您將代碼更改為

if (firstElement instanceof ICompilationUnit) {
    ICompilationUnit unit = (ICompilationUnit firstElement;
    String contents = new String(unit.getContents());
}

你會保持良好狀態。

要查看檢查/修改Java模型和源代碼的詳細信息:

(In Eclipse)
Help->
  Help Contents->
    JDT Plug-in Developer's Guide->
      Programmer's Guide->
        JDT Core

這顯示了如何適當地使用Java模型

要隔離引用java模型的位置,可以創建一個(eclipse)適配器,它將Java Model對象轉換為文件。 假設存在這樣的適配器,您可以請求AdapterManager將其轉換為java文件。 我會先看看是否存在。

那(將資源與JDT分離)是E4(Eclipse 4)的目標之一。
REsources插件列表不再提及JDT(同樣,僅限Eclipse 4.x):

  • org.eclipse.core.filesystem - 一個抽象的通用文件系統API,包括本地文件系統的此API實現。 這是資源插件通過其訪問底層文件系統的API。
  • org.eclipse.core.resources - 包含API和資源模型的實現
  • org.eclipse.core.resources.compatibility - 為在Eclipse 3.1或更高版本中打開舊工作區的用戶提供遷移支持的插件

e4photo這樣的演示項目不需要任何JDT來從選擇IContainer訪問IResource

void setSelection(@Named(IServiceConstants.ACTIVE_SELECTION)
        IResource selection) {
...
IResource[] members = input.members();
...
IResource resource = members[i];
if (resource.getType() == IResource.FILE) {
  InputStream contents = ((IFile) resource).getContents();

看起來JDT已經為所有Java元素定義了一個IAdapterFactory ,包括編譯單元( org.eclipse.jdt.internal.ui.JavaElementAdapterFactory )。 適配器是為IResource而不是IFile定義的,所以你應該能夠做到:

Object firstElement = treeSelection.getFirstElement();

IResource resource = (IResource)Platform.getAdapterManager().getAdapter(firstElement, IResource.class);

你想要實現什么目標? 你想要獲取文件內容嗎? 然后你可以嘗試:

IAdaptable firstElement = (IAdaptable) treeSelection.getFirstElement();

IFile file = (IFile) firstElement.getAdapter(IFile.class);
if (file != null && file.isAccessible()) {
    // Use getContents API
    ....
}

暫無
暫無

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

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