Eclipse RCP編輯器關閉按鈕的屏蔽方法
通過設斷點跟蹤Eclipse RCP的代碼, 發現編輯器上的關閉按鈕其實并不屬于Editor控件的一部分,而是editor所屬容器的,具體層次結構沒有深入去研究,總之按鈕是加在AbstractTabFolder這樣一個控件上的。RCP在啟動時,會通過默認的WorkbenchPresentationFactory在生成GUI上的DefaultTabFolder,并且默認具有關閉按鈕。因此屏蔽關閉按鈕就從此入手。
首先,在ApplicationWorkbenchWindowAdvisor類的preWindowOpen()方法中注冊我們自己定制的PresentationFactory。
Java代碼:
configurer.setPresentationFactory(new UnCloseableEditorPresentationFactory()); |
UnCloseableEditorPresentationFactory類繼承WorkbenchPresentationFactory類,為了不影響別的GUI功能,我們只需要重寫public StackPresentation createEditorPresentation(Composite parent, IStackPresentationSite site)方法中的關于設置TableFolder的部分,具體如下:
Java代碼:
DefaultTabFolder folder = new UnCloseableEditorFolder(parent, editorTabPosition | SWT.BORDER, |
該方法中其余部分代碼,把父類的復制過來即可。
***就是定義我們自己的UnCloseableEditorFolder了
Java代碼:
public UnCloseableEditorFolder(Composite parent, int flags,boolean allowMin, boolean allowMax) |
以上就是需要定制的代碼,另外,UnCloseableEditorPresentationFactory類中,我們還可以public StackPresentation createEditorPresentation(Composite parent, IStackPresentationSite site)中定制StandardViewSystemMenu,從而去掉RCP中編輯器folder上的菜單中的close,closeall,new editor等菜單
Java代碼:
class StandardEditorSystemMenu extends StandardViewSystemMenu { /** * @param site */ public StandardEditorSystemMenu(IStackPresentationSite site) { super(site); } String getMoveMenuText() { return WorkbenchMessages.EditorPane_moveEditor; } /* (non-Javadoc) * @see org.eclipse.ui.internal.presentations.util. ISystemMenu#show(org.eclipse.swt.widgets.Control, org.eclipse.swt.graphics.Point, org.eclipse.ui.presentations.IPresentablePart) */ public void show(Control parent, Point displayCoordinates, IPresentablePart currentSelection) { super.show(parent, displayCoordinates, currentSelection); } } |
以上就是個人從事RCP幾年來一點小小的心得體會。






