Eclipse RCP 中的惯用小技巧

Eclipse RCP 中的常用小技巧

1.实现窗口的透明功能

通过函数Shell.setAlpha(int alpha)设置窗口的透明度,alpha取值0到255,0为全透明。透明窗口的实现需要所在系统的支持,在不支持的系统下setAlpha会被忽略。

 

2.实现全屏模式

 

在Ecipse 3.4M3以后的版本中要设置全屏可以调用 Shell.setFullScreen(true)即可;


在之前版本的RCP平台中实现全屏,需要显式设置Shell的样式为SWT.NO_TRIM (表示Shell无边框和标题栏)和SWT.ON_TOP (表示Shell始终在最前端显示),然后把Shell的大小设置为覆盖全屏幕即可达到目的;


顺便介绍一下样式 SWT.TOOL ,在API中是这样解释的:


      A tool window is a window intended to be used as a floating toolbar. It typically has a title bar that is shorter than a normal title bar,and the window title is typically drawn using a smaller font.


意思是,标识为TOOL样式的窗口建议作为一个浮动工具条使用,它的边框和标题栏比普通的Shell要小,而且边框上的字体也小。

 

 

3.使SWT的Table根据TableItem显示Tooltip

默认情况下,SWT的Table只会给整个Table设置Tooltip,要对每一个TableItem设置Tooltip的话,就要监听鼠标事件了,代码如下:

 

table.addMouseTrackListener(new MouseTrackAdapter() {     
      public void mouseHover(MouseEvent event) {     
          Point pt = new Point(event.x, event.y);     
          int index = table.getTopIndex();     
          while (index < table.getItemCount()) {     
              TableItem item = table.getItem(index);     
              for (int i = 0; i < table.getColumnCount(); i++) {     
                  Rectangle rect = item.getBounds(i);     
                  if (rect.contains(pt)) {     
                      Object object= item.getData();     
                      //TODO 此处可以进行类型的转化了   
                  }     
              }     
              index++;     
          }     
      }     
  });   

 

 

4.指定JFace中Dialog初始化的位置

打开一个新的 对话框 时,如何设定它和父 对话框 的相对位置?比如在登录 对话框 有一个“创建新帐号”的按钮,用户点击以后,就出现新的对话框用于注册,请问如何能让新的 对话框 和旧 对话框 排列的整齐一些?应该是能设定二者的相对位置吧? 
最开始,以为要用Shell.setLocation来设置,但是对于一个Dialog而言,它的Shell在什么时候才能初始化呢?
我 在构造函数里面,configureShell(Shell newShell)方法里面,Control createDialogArea(Composite parent)方法里面都调用过了this.getShell方法想得到当前的Shell,结果都抛出空指针异常....
后来看书发现,应该重写protected Point getInitialLocation(Point initialSize)方法
比如,在最开始的例子中,在第二个对话框中我重写了该方法,代码如下:

 

 

protected Point getInitialLocation(Point initialSize) {     
        Point location = new Point(this.getParentShell().getLocation().x     
                + this.getParentShell().getBounds().width, this     
                .getParentShell().getLocation().y     
                + this.getParentShell().getBounds().height     
                - this.getInitialSize().y);     
        return location;     
    }    

 

 

5.在RCP中使界面适合桌面大小

final int screenWidth = Display.getCurrent().getBounds().width;  
final int screenHeight = Display.getCurrent().getBounds().height;  

  

6.在透视图快捷方式栏中同时显示多个透视图快捷方式

 

如果在程序中用到了多个透视图,默认只显示初始透视图快捷方式,每次都要 打开透视图——other,特麻烦,所以用户可能希望以更加快捷的方式来打开透视图,下面提供了2种方式:

第一种是在切换栏中显示:

 

 

 

public class PIMWorkbenchAdvisor extends WorkbenchAdvisor {  
    @Override  
    public void postStartup() {  
        super.postStartup();  
        IWorkbenchWindow activeWorkbenchWindow PlatformUI.getWorkbench().getActiveWorkbenchWindow();  
        设置同时显示多个透视图标  
        PerspectiveBarManager barManager =((WorkbenchWindow)activeWorkbenchWindow).getPerspectiveBar();  
        if(barManager != null){  
            IPerspectiveDescriptor mailPerspective = WorkbenchPlugin.getDefault().getPerspectiveRegistry   ().findPerspectiveWithId("MyWork_mail.perspective");  
            PerspectiveBarContributionItem item=new PerspectiveBarContributionItem(mailPerspective,activeWorkbenchWindow.getActivePage());  
            barManager.addItem(item);  
        }  
}  
 

 

第二种是在下拉框中显示:

 

 

public class UiPerspective implements IPerspectiveFactory  
{  
    public void createInitialLayout(IPageLayout layout){  
        //增加透视图  
        layout.addPerspectiveShortcut("net.sf.pim.plugin.UiPerspective");  
        layout.addPerspectiveShortcut("MyWork_mail.perspective");  
       }  
}  

 

 

7.控制文件菜单中“最近打开文档”的个数

RCP中在继承ActionBarAdvisor的类中定义:

 

private IContributionItem reOpenAction = ContributionItemFactory.REOPEN_EDITORS.create(window);

然后在fillMenuBar(IMenuManager menuBar)方法中添加上面的aciton

 

运行时“最近打开的文档”只有4个,如果想自己控制“最近打开的文档”的数量,则设置一下Workbench中的初始化首选项时RECENT_FILES的默认参数值如:

WorkbenchPlugin.getDefault().getPreferenceStore().setDefault(IPreferenceConstants.RECENT_FILES,10);

 

8.设置透视图工具栏的位置

在 Eclipse 中可以通过在首选项Window->Preferences-> Workbench->Appearance中来设置它显示的位置,那么如何用程序来实现这已功能呢,需要在ApplicationWorkbenchAdvisor 类的 方法 initialize(IWorkbenchConfigurer configurer)中通过设置首选项的值来实现:

// 保存当前窗口状态  
configurer.setSaveAndRestore(true);  
// 设备界面标题风格  
PlatformUI.getPreferenceStore().setValue(IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS,false);  
PlatformUI.getPreferenceStore().setValue(IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR, IWorkbenchPreferenceConstants.TOP_RIGHT);