通过设断点跟蹤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几年来一点小小的心得体会