Eclipse RCP 插件开发中经常调用的几个方法

原文:http://blog.chinaunix.net/uid-200142-id-4607484.html

在插件中访问StatusLine

  1. WorkbenchWindow window = PlatfromUI.getWorkbench().getActiveWorkbenchWindow(); IStatusLineManager status = window.getStatusLineManager();

退出RCP程序

  1. PlatformUI.getWorkbench().restart();
重启RCP应用

  1. PlatformUI.getWorkbench().restart();
在插件资源库中查找Perspective(其它extend point资源的获得类似)

  1. IPerspectiveDescriptor ipd = PlatformUI.getWorkbench().getPerspectiveRegistry().findPerspectiveWithId(FavoritePerspective.ID);
获得当前RCP应用的shell

  1. Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
显示帮助信息

  1. PlatformUI.getWorkbench().getHelpSystem().displayHelp();
显示内存

  1. PlatformUI.getPreferenceStore().setValue( IWorkbenchPreferenceConstants.SHOW_MEMORY_MONITOR, true);
eclipse平台图片资源共享的获取方法 

  1. PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TASK));
rcp(插件开发) 获取插件版本信息 

  1. /**
  2.   * 获取插件版本
  3.   * @return
  4.   */
  5.  public String getVersion(){
  6.         return ((BundleHost)getBundle()).getVersion().toString();
  7.     }
在其他view中获取某个view的引用

  1. PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("viewId");
如何使用代码重启RCP程序?
  1. PlatformUI.getWorkbench().restart();

获得父Shell的几种方法
IViewPart 或者IEditorPart : 

  1. IShellProvider shellProvider = viewOrEditor.getSite();
PlatformUI:

  1. Shell parentShell =
  2.    PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
Display :

  1. Shell parentShell = Display.getDefault().getActiveShell();
在实现了 IWorkbenchWindowActionDelegate接口的类中:

  1. private IWorkbenchWindow window;

  2. public void init(IWorkbenchWindow window) {
  3.    this.window = window;
  4. }
  5. public void run(IAction action) {
  6.    Shell parentShell = window.getShell();
  7.    MyDialog dialog = new MyDialog(parentShell, );
  8.     etc 
  9. }
获取程序启动参数
  1. String[] args = Platform.getCommandLineArgs();


关闭一个当前的perspective打开一个新的perspective

  1. IWorkbench w=PlatformUI.getWorkbench();

  2. ActionFactory.IWorkbenchAction closePerspectiveAction
  3. = ActionFactory.CLOSE_PERSPECTIVE.create(w.getActiveWorkbenchWindow());
  4. closePerspectiveAction.run();

  5. try ...{
  6. PlatformUI.getWorkbench().showPerspective("com.ibm.demo.saic.ui.views.NextPerspective",
  7. w.getActiveWorkbenchWindow());
  8. } catch (WorkbenchException e) ...{
  9. e.printStackTrace();
  10. }


定制Console View的ToolBar:
去掉RCP Console View 中的Open Console和Select Console按钮。

  1. WorkbenchPage page = PlatformUI.getWorkbench().getWorkbenchWindows()[0].getPages()[0]; 
  2. IViewPart viewpart = page.findView(IConsoleConstants.ID_CONSOLE_VIEW); 
  3. IActionBars actionBar = viewpart.getViewSite().getActionBars(); 
  4. IToolBarManager toolbarMgr = actionBar.getToolBarManager(); 
  5. IContributionItem[] items = toolbarMgr.getItems(); 
  6. for (IContributionItem item : items) { 
  7.     if (item instanceof ActionContributionItem) { 
  8.         IAction action = ((ActionContributionItem) item).getAction(); 
  9.         String text = action.getText(); 
  10.         if (text.equals("Open Console") || text.equals("Select Console")) { 
  11.             toolbarMgr.remove(item); 
  12.         } 
  13.     } 
  14. } 
  15. actionBar.updateActionBars();
如果不需要toobar中的任何东西,可以直接

  1. toolbarMgr.removeAll();
可以看到现在的rcp上有很多menubar,这些是我们所不需要的,可以通过在代码中添加下列代码来简化menubar

  1. IWorkbenchPage page = PlatformUI.getWorkbench().getWorkbenchWindows()[0] 
  2.                 .getPages()[0]; 
  3.         IViewPart viewpart = page.findView(IConsoleConstants.ID_CONSOLE_VIEW); 
  4.         // IActionBars actionBar = viewpart.getViewSite().getActionBars(); 
  5.         IToolBarManager toolbarMgr = viewpart.getViewSite().getActionBars() 
  6.                 .getToolBarManager(); 
  7.         // IToolBarManager toolbarMgr = actionBar.getToolBarManager(); 
  8.         IContributionItem[] items = toolbarMgr.getItems(); 
  9.         for (IContributionItem item : items) { 
  10.             if (item instanceof ActionContributionItem) { 
  11.                 IAction action = ((ActionContributionItem) item).getAction(); 
  12.                 String text = action.getText(); 
  13.                 if (text.equals("Open Console") 
  14.                         || text.equals("Select Console")) { 
  15.                     toolbarMgr.remove(item); 
  16.                 } 
  17.             } 
  18.         } 
  19.         viewpart.getViewSite().getActionBars().updateActionBars();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值