2009-02-21 20 views
2

我正在爲Java編寫Eclipse插件,並且存在以下問題:編寫Eclipse插件時,檢查IEditorPart是否爲Java編輯器的正確方法是什麼?

給定一個IEditorPart,我需要檢查它是否是java編輯器。

我可以做(IEditor的instanceof JavaEditor), 但JavaEditor是org.eclipse.jdt.internal.ui.javaeditor.JavaEditor, 這下JDT的 「內部」 類下降。

有沒有更智能,更安全的方法來做到這一點?我不確定爲什麼沒有非內部接口。

+0

檢查它是什麼原因? – nitind 2013-02-01 06:21:52

回答

4

應測試IEditorPart的ID:

private boolean isJavaEditor(IWorkbenchPartReference ref) { 
    if (ref == null) { 
     return false; } 

    String JavaDoc id= ref.getId(); 
    return JavaUI.ID_CF_EDITOR.equals(id) || JavaUI.ID_CU_EDITOR.equals(id); 
} 

測試實例是only needed in eclipse3.1

alt text http://blogs.zdnet.com/images/Burnette_DSCN0599.JPG

JavaUI是主要的接入點到Java用戶界面組件。它允許您以編程方式在Java元素上打開編輯器,打開Java或Java瀏覽視角,打開包並鍵入提示器對話框。

JavaUI是Java UI插件(ID爲「org.eclipse.jdt.ui‘)

你可以看到那種在ASTProvider用於例如效用函數(’isJavaEditor()」)的中心接入點。

這裏的識別機制確實是簡單的字符串比較。

無論如何,你是明智的,以避免與內部類比較:它被列爲one of the 10 common errors in plugins development;)。

+0

似乎可靠,但我不得不說這是一個非常奇怪的機制...字符串比較? 我希望JDT暴露更多材料。 – Uri 2009-02-21 22:46:56

0

一個策略可能是使用JavaUI.getEditorInputJavaElement(IEditorPart)

// given IEditorPart editor 
IJavaElement elt = JavaUI.getEditorInputJavaElement(editor.getEditorInput()); 
if (elt != null) { 
    // editor is a Java editor 
} 

如果編輯器輸入其實也不是Java元素的方法返回null

相關問題