Eclipse RCP 以编程方式获取工具栏贡献

发布于 2024-12-22 17:22:02 字数 1219 浏览 0 评论 0原文

我有一个 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);
         }
      }
}

有办法获得主操作栏吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

残疾 2024-12-29 17:22:02

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

但是您可以根据活动处理程序的启用来启用/禁用命令。您的处理程序负责确定其启用状态。

以编程方式,如果您子类化 org.eclipse.core.commands.AbstractHandler ,您将调用 setBaseEnabled(boolean state) 来确保它触发正确的事件。

以声明方式,当通过 org.eclipse.ui.handlers 贡献时,它也支持 enabledWhen 元素。可以访问 org.eclipse.ui.ISources 中列出的应用程序状态

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

夜声 2024-12-29 17:22:02

如果您想访问主工具栏上的项目,一旦 IHandler 实现了 IElementUpdater 接口,Eclipse 的命令框架将使用该类来更新命令的标签、工具提示甚至图像。有关更多详细信息,请参阅此:

http:// www.robertwloch.net/2011/01/eclipse-tips-tricks-label-updating-command-handler/

If you want to access your items on the main toolbar, once an IHandler implements the interface IElementUpdater Eclipse’s command framework will use that class to update the label, tooltip, or even images of a command. See this for more details :

http://www.robertwloch.net/2011/01/eclipse-tips-tricks-label-updating-command-handler/

ζ澈沫 2024-12-29 17:22:02

我刚刚发现以下活动模式删除了外部工具菜单的贡献。这一点是很难弄清楚的。

       <activity id="org.eclipse.ui.navigator.resources.unwanted" name="unwanted"/> 

       <activityPatternBinding
       activityId="org.eclipse.ui.navigator.resources.unwanted"
       pattern=".*ExternalTool.*">
       </activityPatternBinding>

I just found that the following activity pattern removes the External tools menu contribution. This one was quite difficult to figure out.

       <activity id="org.eclipse.ui.navigator.resources.unwanted" name="unwanted"/> 

       <activityPatternBinding
       activityId="org.eclipse.ui.navigator.resources.unwanted"
       pattern=".*ExternalTool.*">
       </activityPatternBinding>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文