創建Eclipse Editor

原文鏈接:http://www.vogella.com/articles/EclipseEditors/article.html

配置要求:Eclipse 3.7 Indigo

Eclipse使用editor和view來處理數據。editor一般需要用戶點擊“save”來保存數據改動,而view一般直接執行改動。

在RCP系統中通常採用下列步驟來創建和使用editor:

  • 在perspective中把editor區域設置爲可見
  • 創建IEditorInput類
  • 定義一個“org.eclipse.ui.editors” extension point
  • 創建editor類,這個類必須實現IEditorPart接口
IEditorInput是editor模型的輕量級展現,Eclipse會緩衝IEditorInput對象,因此這個對象應該相對較小。Eclipse根據IEditorInput中的equals()方法來判斷對應的editor是否已經打開,以及是否需要打開一個新的editor(這個方法很重要)。
editor通過“org.eclipse.ui.editors” extension point定義,editor類必須實現IEditorPart接口,通常會繼承EditorPart類。editor會通過init()方法獲取IEditorSite和IEditorInput對象,必須調用setInput()和setSite()分別進行設置。init()方法在createPartControl()之前被調用,因此在創建UI的時候,IEditorInput就已經可以被用戶使用了。
editor類中的setPartName()方法可以用來設置editor的title,override getTitleToolTip()可以設置tooltip。
editor類中的isDirty()方法可以用來告訴workbench editor的內容是否有改動,我們可以fire一個event來告訴workbench editor的dirty屬性已經改動:
firePropertyChange(IEditorPart.PROP.DIRTY);
我們可以通過當前活動頁面來打開editor,顯然我們需要editor的ID(來告訴Eclipse使用哪一個editor),以及一個EditorInput對象(告訴Eclipse的editor使用哪個model):
page.openEditor(new YourEditorInput(), ID_OF_THE_EDITOR);
如果獲取當前活動頁面,有這麼幾種方法:
// If you are in a view
getViewSite().getPage();
// If you are in an command
HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
// Somewhere else
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

原文中有一個詳細的例子和源代碼,有興趣的可以實踐一下。








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章