Eclipse RCP obtain toolbar contributions programmatically

岁酱吖の 提交于 2019-12-20 03:12:47

问题


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?


回答1:


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




回答2:


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/




回答3:


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>


来源:https://stackoverflow.com/questions/8603405/eclipse-rcp-obtain-toolbar-contributions-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!