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);
}
}
}
有办法获得主操作栏吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,无法访问主工具栏。
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 callsetBaseEnabled(boolean state)
to make sure it fires the correct event.Declaratively, when contributed via
org.eclipse.ui.handlers
it has support for anenabledWhen
element as well. That has access to the application state listed inorg.eclipse.ui.ISources
如果您想访问主工具栏上的项目,一旦 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/
我刚刚发现以下活动模式删除了外部工具菜单的贡献。这一点是很难弄清楚的。
I just found that the following activity pattern removes the External tools menu contribution. This one was quite difficult to figure out.