原文鏈接: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接口
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();
原文中有一個詳細的例子和源代碼,有興趣的可以實踐一下。