通過設斷點跟蹤Eclipse RCP的代碼 發現編輯器上的關閉按鈕其實並不屬於Editor控件的一部分而是editor所屬容器的具體層次結構沒有深入去研究總之按鈕是加在AbstractTabFolder這樣一個控件上的RCP在啟動時會通過默認的WorkbenchPresentationFactory在生成GUI上的DefaultTabFolder並且默認具有關閉按鈕因此屏蔽關閉按鈕就從此入手
首先在ApplicationWorkbenchWindowAdvisor類的preWindowOpen()方法中注冊我們自己定制的PresentationFactory
Java代碼
configurersetPresentationFactory(new UnCloseableEditorPresentationFactory())
UnCloseableEditorPresentationFactory類繼承WorkbenchPresentationFactory類為了不影響別的GUI功能我們只需要重寫public StackPresentation createEditorPresentation(Composite parent IStackPresentationSite site)方法中的關於設置TableFolder的部分具體如下
Java代碼
DefaultTabFolder folder = new UnCloseableEditorFolder(parent editorTabPosition | SWTBORDER
sitesupportsState(IStackPresentationSiteSTATE_MINIMIZED)
sitesupportsState(IStackPresentationSiteSTATE_MAXIMIZED))
…
該方法中其余部分代碼把父類的復制過來即可
最後就是定義我們自己的UnCloseableEditorFolder了
Java代碼
public UnCloseableEditorFolder(Composite parent int flagsboolean allowMin boolean allowMax){
super(parent flags allowMin allowMax)
}
@SuppressWarnings(restriction) public AbstractTabItem add(int index int flags) {
return superadd(index flags ^ SWTCLOSE)
}
以上就是需要定制的代碼另外UnCloseableEditorPresentationFactory類中我們還可以public StackPresentation createEditorPresentation(Composite parent IStackPresentationSite site)中定制StandardViewSystemMenu從而去掉RCP中編輯器folder上的菜單中的closecloseallnew editor等菜單
Java代碼
class StandardEditorSystemMenu extends StandardViewSystemMenu {
/**
* @param site
*/
public StandardEditorSystemMenu(IStackPresentationSite site) {
super(site)
}
String getMoveMenuText() {
return WorkbenchMessagesEditorPane_moveEditor;
}
/* (nonJavadoc)
* @see orgeclipseuiinternalpresentationsutilISystemMenu#show(orgeclipseswtwidgetsControl orgeclipseswtgraphicsPoint orgeclipseuipresentationsIPresentablePart) */
public void show(Control parent Point displayCoordinates IPresentablePart currentSelection) {
supershow(parent displayCoordinates currentSelection)
} }
以上就是個人從事RCP幾年來一點小小的心得體會
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28813.html