2012-09-25 55 views
3

我正在編寫一個Eclipse插件,可以更輕鬆地爲Android進行資源編輯。當用戶點擊項目中的任何XML資源文件時,會打開一個編輯器,允許一次編輯項目中的所有資源。如何強制爲同一輸入打開第二個編輯器?

我想補充能力在一個單獨的,默認的Android資源編輯器打開相同的文件。我知道該編輯的身份,但我無法訪問其課程。

調用IDE.openEditor什麼也不做,因爲編輯是該文件已經打開,即使我指定其他的Android編輯器的ID。

如何強制Eclipse中打開相同的輸入其他編輯器?

在另一方面,它可以嵌入在MultiPageEditorPart其他編輯器,如果我只能訪問它的ID,而不是它的課嗎?

回答

2

IDE.openEditor方法調用結束時相應IWorkbenchPage方法來打開編輯器。

,可能是你的情況非常有用的方法是 org.eclipse.ui.IWorkbenchPage.openEditor(IEditorInput, String, boolean, int)

/** 
    * Opens an editor on the given input. 
    * <p> 
    * If this page already has an editor open that matches the given input 
    * and/or editor id (as specified by the matchFlags argument), that editor 
    * is brought to the front; otherwise, a new editor is opened. Two editor 
    * inputs are considered the same if they equal. See 
    * <code>Object.equals(Object)<code> 
    * and <code>IEditorInput</code>. If <code>activate == true</code> the editor 
    * will be activated. 
    * </p><p> 
    * The editor type is determined by mapping <code>editorId</code> to an editor 
    * extension registered with the workbench. An editor id is passed rather than 
    * an editor object to prevent the accidental creation of more than one editor 
    * for the same input. It also guarantees a consistent lifecycle for editors, 
    * regardless of whether they are created by the user or restored from saved 
    * data. 
    * </p> 
    * 
    * @param input the editor input 
    * @param editorId the id of the editor extension to use 
    * @param activate if <code>true</code> the editor will be activated 
    * @param matchFlags a bit mask consisting of zero or more of the MATCH_* constants OR-ed together 
    * @return an open editor, or <code>null</code> if an external editor was opened 
    * @exception PartInitException if the editor could not be created or initialized 
    * 
    * @see #MATCH_NONE 
    * @see #MATCH_INPUT 
    * @see #MATCH_ID 
    * @since 3.2 
    */ 
    public IEditorPart openEditor(final IEditorInput input, 
     final String editorId, final boolean activate, final int matchFlags) 
     throws PartInitException; 

你需要調用它,並將它傳遞MATCH_ID | MATCH_INPUT因此,它需要編輯的ID考慮試圖確定什麼時候是否現有的編輯器應該被重用或者應該創建一個新的。

2

編輯器擴展點org.eclipse.ui.editors允許一個matchingStrategy添加到擴展。這使您可以影響Eclipse嘗試確定給定ID和給定編輯器輸入的編輯器是否已經打開時的行爲。

實施是相當容易的。您只需提供接口org.eclipse.ui.IEditorMatchingStrategy的實現。它只有一個方法

boolean matches(IEditorReference editorRef, IEditorInput input); 

如果返回false這裏,Eclipse會每次打開一個新的編輯器,即使編輯ID和編輯輸入相等。

相關問題