Eclipse RCP以编程方式获取工具栏贡献 [英] Eclipse RCP obtain toolbar contributions programmatically

查看:369
本文介绍了Eclipse RCP以编程方式获取工具栏贡献的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RCP应用程序,当我执行一些操作时,我想禁用/启用工具栏的某些元素。我的扩展名:

 < extension point =org.eclipse.ui.menus> 
< menuContribution locationURI =toolbar:org.eclipse.ui.main.toolbar>
< toolbar id =vendor.toolbar1h>
< command commandId =vendor.commands.MyCommand
icon =icon.png
id =MyButtonID1
style =toggle>
< / command>
< / toolbar>
< / menuContribution>
< / extension>

我尝试枚举所有的工具栏贡献与这段代码,但它不起作用,它显示

  IViewReference [] refs = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow()。 getActivePage()getViewReferences();
for(IViewReference ref:refs){
System.err.println(ID:+ ref.getId());
IViewPart viewPart = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow()。getActivePage()。findView(ref.getId());
IActionBars bars = viewPart.getViewSite()。getActionBars();
if(bars!= null){
IToolBarManager tbm = bars.getToolBarManager();
if(tbm!= null){
IContributionItem [] items = tbm.getItems();
for(IContributionItem item:items)
System.err.println(\t+ item);
}
}
}

存在一种获取主动作栏?

解决方案

不,无法访问主工具栏。 IActionBars 工具栏返回视图工具栏(右侧视图选项卡)。



但是您启用/禁用基于启用活动处理程序的命令。您的处理程序负责确定其启用状态。



程序化地,如果您继承 org.eclipse.core.commands.AbstractHandler 您将调用 setBaseEnabled(boolean state)以确保它触发正确的事件。



声明性地,当通过 org.eclipse.ui.handlers 贡献,它支持 enabledWhen 元素。可以访问 org.eclipse.ui.ISources


中列出的应用程序状态

I have an RCP application and I want disable/enable some elements of the toolbar when I perform some actions. My extension:

<extension point="org.eclipse.ui.menus">
   <menuContribution locationURI="toolbar:org.eclipse.ui.main.toolbar">
      <toolbar id="vendor.toolbar1h">
         <command commandId="vendor.commands.MyCommand"
          icon="icon.png"
          id="MyButtonID1"
          style="toggle">
         </command>
      </toolbar>
   </menuContribution>
</extension>

I try to enumerate all the toolbar contributions with this code, but it doesn't work, it show only the contributions of the views.

IViewReference[] refs = PlatformUI.getWorkbench()
   .getActiveWorkbenchWindow().getActivePage().getViewReferences();
for (IViewReference ref : refs) {
   System.err.println("ID: "+ref.getId());
   IViewPart viewPart = PlatformUI.getWorkbench()
      .getActiveWorkbenchWindow().getActivePage().findView(ref.getId());
   IActionBars bars = viewPart.getViewSite().getActionBars();
   if (bars != null) {
      IToolBarManager tbm = bars.getToolBarManager();
      if (tbm != null) {
         IContributionItem[] items = tbm.getItems();
         for (IContributionItem item : items)
            System.err.println("\t" + item);
         }
      }
}

Exists a way to get the main action bar?

解决方案

No, there's no way to get access to the main toolbar. The IActionBars toolbar returns the view toolbar (right next to the view tab).

But you enable/disable a command based on the enablement of the active handler. Your handler is responsible for determining its enabled state.

Programmaticly, if you subclass org.eclipse.core.commands.AbstractHandler you would call setBaseEnabled(boolean state) to make sure it fires the correct event.

Declaratively, when contributed via org.eclipse.ui.handlers it has support for an enabledWhen element as well. That has access to the application state listed in org.eclipse.ui.ISources

这篇关于Eclipse RCP以编程方式获取工具栏贡献的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆