Eclipse RCP中编辑器(editpart)关闭按钮的屏蔽方法

注:原文转自http://blog.csdn.net/davyxie/article/details/2905901


Eclipse RCP开发中,屏蔽视图的关闭按钮很容易,在IPerspectiveFactory接口的createInitialLayout方法中,调用IViewLayout的setCloseable(boolean)方法即可,而屏蔽编辑器的关闭按钮则有点麻烦,需要RCP中的WorkbenchPresentationFactory进行定制.

通过设断点跟踪Eclipse RCP的代码, 发现编辑器上的关闭按钮其实并不属于Editor控件的一部分,而是editor所属容器的,具体层次结构没有深入去研究,总之按钮是加在AbstractTabFolder这样一个控件上的。RCP在启动时,会通过默认的WorkbenchPresentationFactory在生成GUI上的DefaultTabFolder,并且默认具有关闭按钮。因此屏蔽关闭按钮就从此入手。

首先,在ApplicationWorkbenchWindowAdvisor类的preWindowOpen()方法中注册我们自己定制的PresentationFactory。

Java代码 复制代码
  1. configurer.setPresentationFactory(new UnCloseableEditorPresentationFactory());  
  1. configurer.setPresentationFactory(new UnCloseableEditorPresentationFactory());  

 

UnCloseableEditorPresentationFactory类继承WorkbenchPresentationFactory类,为了不影响别的GUI功能,我们只需要重写public StackPresentation createEditorPresentation(Composite parent, IStackPresentationSite site)方法中的关于设置TableFolder的部分,具体如下:

Java代码 复制代码
  1. DefaultTabFolder folder = new UnCloseableEditorFolder(parent, editorTabPosition | SWT.BORDER,               site.supportsState(IStackPresentationSite.STATE_MINIMIZED),                 site.supportsState(IStackPresentationSite.STATE_MAXIMIZED)); ...  
  1. DefaultTabFolder folder = new UnCloseableEditorFolder(parent, editorTabPosition | SWT.BORDER,               site.supportsState(IStackPresentationSite.STATE_MINIMIZED),                 site.supportsState(IStackPresentationSite.STATE_MAXIMIZED)); ...  

 

该方法中其余部分代码,把父类的复制过来即可。

最后就是定义我们自己的UnCloseableEditorFolder了

Java代码 复制代码
  1. public UnCloseableEditorFolder(Composite parent, int flags,boolean allowMin, boolean allowMax) {        super(parent, flags, allowMin, allowMax);   }   @SuppressWarnings("restriction")    public AbstractTabItem add(int index, int flags) {      return super.add(index, flags ^ SWT.CLOSE);     }  
  1. public UnCloseableEditorFolder(Composite parent, int flags,boolean allowMin, boolean allowMax) {        super(parent, flags, allowMin, allowMax);   }   @SuppressWarnings("restriction")    public AbstractTabItem add(int index, int flags) {      return super.add(index, flags ^ SWT.CLOSE);     }  

 以上就是需要定制的代码,另外,UnCloseableEditorPresentationFactory类中,我们还可以public StackPresentation createEditorPresentation(Composite parent, IStackPresentationSite site)中定制StandardViewSystemMenu,从而去掉RCP中编辑器folder上的菜单中的close,closeall,new editor等菜单

Java代码 复制代码
  1. 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);     } }  
  1. 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);     } }  




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值