[英]JFace/SWT: What is the best way to add a toolbar with Commands to a Section?
我有一个部分,想要添加一个工具栏。 我能够使用Actions以编程方式执行它,但要求是尽可能多地以声明方式(在plugin.xml中)执行它。 所以我想为每个工具栏按钮定义一个Command和一个Handler,但我不知道如何将它们添加到该部分的工具栏中。 有没有办法在plugin.xml中以声明方式执行此操作? 如果没有,我该如何以编程方式进行?
谢谢!
我认为你必须编写自己的扩展点来定义plugin.xml
,然后编写代码来访问扩展点注册表以获取声明的扩展并从信息中创建工具栏。
有关更多详细信息,请参阅Eclipse扩展点和扩展 。
您需要了解如何使用org.eclipse.ui.menus extension
点。 它支持向menu / popup / toolbar / trim添加命令/小部件。
//contributing to local toolbar
ToolBarManager localToolBarmanager = new ToolBarManager();
IMenuService menuService = (IMenuService) PlatformUI.getWorkbench().getService(IMenuService.class);
menuService.populateContributionManager(localToolBarmanager,
"toolbar:localtoolbar"); //id of your local toolbar
localToolBarmanager.createControl(control);
以下是如何为节创建工具栏的示例,确保在section.setClient()
之前创建工具栏。
protected void createToolbar(Section section) {
ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
toolBarManager.add(new Action("print") {
@Override
public void run() {
System.out.println("PRINT");
}
});
createSectionToolbar(section, toolBarManager);
}
/**
* create a toolbar in the passed section
*
* @param section
* @param toolBarManager
*/
protected void createSectionToolbar(Section section, ToolBarManager toolBarManager) {
Composite toolbarComposite = toolkit.createComposite(section);
toolbarComposite.setBackground(null);
toolBarManager.createControl(toolbarComposite);
section.clientVerticalSpacing = 0;
section.descriptionVerticalSpacing = 0;
section.setTextClient(toolbarComposite);
}
如果要将plugin.xml
中的声明命令添加到工具栏,请查看CommandContributionItem
。
toolBarManager.add(new CommandContributionItem(new CommandContributionItemParameter(getSite(), "id", "commandId", SWT.NONE)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.