Eclipse RCP编辑器关闭按钮的屏蔽方法

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

通过设断点跟踪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,         

      site.supportsState(IStackPresentationSite.STATE_MINIMIZED),            

     site.supportsState(IStackPresentationSite.STATE_MAXIMIZED)); ... 

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

***就是定义我们自己的UnCloseableEditorFolder了

Java代码:

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代码:

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几年来一点小小的心得体会。

责任编辑:book05 来源: javaeye
相关推荐

2013-09-10 16:02:59

Elipse编辑器

2009-06-04 20:08:26

Eclipse RCPEclipse

2010-02-23 15:44:24

Python编辑器

2011-01-10 16:17:49

2009-05-20 14:48:07

ibmdwEclipse开发技巧

2010-03-24 09:20:07

CentOS vi编辑

2013-07-11 11:13:51

编辑器

2013-06-18 01:22:46

CocoStudio工Cocos2d-x

2011-03-22 13:54:57

UbuntuPHP编辑器

2017-03-09 11:45:16

LinuxVim编辑器

2018-09-25 09:25:11

Vim编辑器命令

2009-10-26 10:47:57

linux vi编辑器

2009-06-15 16:23:39

Eclipse中使用SEclipse RCP

2020-10-14 14:00:39

VIM编辑器

2022-05-31 14:46:02

Ruby编码线上编辑器

2009-12-04 17:07:49

SlickEdit

2022-01-04 08:16:49

编辑器在线编辑开发

2023-02-01 09:21:59

图形编辑器标尺

2023-01-18 08:30:40

图形编辑器元素

2020-09-18 06:00:51

开源Markdown编辑器
点赞
收藏

51CTO技术栈公众号