鉴于以下操作栏,我希望能够向其添加任何其他SWT窗口小部件(例如标签)。鉴于actual API,我不知道这是如何可能的,因为它只允许添加IAction对象。
答案 0 :(得分:2)
这是不可能的(我认为它很好,否则不会有来自不同来源的插件的一致外观。)
备选方案:
答案 1 :(得分:1)
您可以通过常规API向IActionBars
添加其他控件。下面是显示如何实现此目的的代码。我修改了由新的“Plug-in Project”向导创建的标准视图示例:
...
public void createPartControl(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setSorter(new NameSorter());
viewer.setInput(getViewSite());
makeActions();
hookContextMenu();
hookDoubleClickAction();
contributeToActionBars();
}
private void hookContextMenu() {
MenuManager menuMgr = new MenuManager("#PopupMenu");
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
SampleView.this.fillContextMenu(manager);
}
});
Menu menu = menuMgr.createContextMenu(viewer.getControl());
viewer.getControl().setMenu(menu);
getSite().registerContextMenu(menuMgr, viewer);
}
private void contributeToActionBars() {
IActionBars bars = getViewSite().getActionBars();
fillLocalPullDown(bars.getMenuManager());
fillLocalToolBar(bars.getToolBarManager());
}
private void fillLocalPullDown(IMenuManager manager) {
manager.add(action1);
manager.add(new Separator());
manager.add(action2);
}
private void fillContextMenu(IMenuManager manager) {
manager.add(action1);
manager.add(action2);
// Other plug-ins can contribute there actions here
manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
}
private void fillLocalToolBar(IToolBarManager manager) {
manager.add(action1);
manager.add(action2);
manager.add(new LabelContributionItem());
manager.add(new ComboContributionItem());
}
private class LabelContributionItem extends ControlContribution {
public LabelContributionItem() {
super("someLabelId");
}
@Override
protected Control createControl(Composite parent) {
Label label = new Label(parent, SWT.NONE);
label.setText("Hello");
return label;
}
}
private class ComboContributionItem extends ControlContribution {
public ComboContributionItem() {
super("someComboId");
}
@Override
protected Control createControl(Composite parent) {
Combo combo = new Combo(parent, SWT.NONE);
return combo;
}
}
...
希望这有帮助。