Eclipse RCP 中的常用小技巧

来源:互联网 发布:手机淘宝怎么收藏店铺 编辑:程序博客网 时间:2024/05/22 01:52
 
  • 实现窗口的透明功能(限于Eclipse 3.4M3以上版本)

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

    • 实现全屏模式

      在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要小,而且边框上的字体也小。

      • 使SWT的Table根据TableItem显示Tooltip

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

      view plaincopy to clipboardprint?
      1. table.addMouseTrackListener(new MouseTrackAdapter() {     
      2.       public void mouseHover(MouseEvent event) {     
      3.           Point pt = new Point(event.x, event.y);     
      4.           int index = table.getTopIndex();     
      5.           while (index < table.getItemCount()) {     
      6.               TableItem item = table.getItem(index);     
      7.               for (int i = 0; i < table.getColumnCount(); i++) {     
      8.                   Rectangle rect = item.getBounds(i);     
      9.                   if (rect.contains(pt)) {     
      10.                       Object object= item.getData();     
      11.                       //TODO 此处可以进行类型的转化了   
      12.                   }     
      13.               }     
      14.               index++;     
      15.           }     
      16.       }     
      17.   });     

       

      • 指定JFace中Dialog初始化的位置

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

      view plaincopy to clipboardprint?
      1. protected Point getInitialLocation(Point initialSize) {     
      2.         Point location = new Point(this.getParentShell().getLocation().x     
      3.                 + this.getParentShell().getBounds().width, this     
      4.                 .getParentShell().getLocation().y     
      5.                 + this.getParentShell().getBounds().height     
      6.                 - this.getInitialSize().y);     
      7.         return location;     
      8.     }     

      • 在RCP中使界面适合桌面大小

      view plaincopy to clipboardprint?
      1. final int screenWidth = Display.getCurrent().getBounds().width;  
      2. final int screenHeight = Display.getCurrent().getBounds().height;  

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

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

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

      view plaincopy to clipboardprint?
      1. public class PIMWorkbenchAdvisor extends WorkbenchAdvisor {  
      2.     @Override  
      3.     public void postStartup() {  
      4.         super.postStartup();  
      5.         IWorkbenchWindow activeWorkbenchWindow PlatformUI.getWorkbench().getActiveWorkbenchWindow();  
      6.         设置同时显示多个透视图标  
      7.         PerspectiveBarManager barManager =((WorkbenchWindow)activeWorkbenchWindow).getPerspectiveBar();  
      8.         if(barManager != null){  
      9.             IPerspectiveDescriptor mailPerspective = WorkbenchPlugin.getDefault().getPerspectiveRegistry   ().findPerspectiveWithId("MyWork_mail.perspective");  
      10.             PerspectiveBarContributionItem item=new PerspectiveBarContributionItem(mailPerspective,activeWorkbenchWindow.getActivePage());  
      11.             barManager.addItem(item);  
      12.         }  
      13. }  

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

      view plaincopy to clipboardprint?
      1. public class UiPerspective implements IPerspectiveFactory  
      2. {  
      3.     public void createInitialLayout(IPageLayout layout){  
      4.         //增加透视图   
      5.         layout.addPerspectiveShortcut("net.sf.pim.plugin.UiPerspective");  
      6.         layout.addPerspectiveShortcut("MyWork_mail.perspective");  
      7.        }  
      8. }  

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

      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);

      • 设置透视图工具栏的位置

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


      view plaincopy to clipboardprint?
      1. // 保存当前窗口状态   
      2. configurer.setSaveAndRestore(true);  
      3. // 设备界面标题风格   
      4. PlatformUI.getPreferenceStore().setValue(IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS,false);  
      5. PlatformUI.getPreferenceStore().setValue(IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR, IWorkbenchPreferenceConstants.TOP_RIGHT);