Reputation: 25872
Given the following action bar, I want to be able to add any other SWT widget to it (such as a label). Given the actual API, I don't see how this is possible as it only allows adding IAction objects.
Upvotes: 0
Views: 877
Reputation: 3241
You can add other controls to the IActionBars
through the regular API. Below is the code that shows how to achieve this. I have modified the standard view sample that is created by the new "Plug-in Project" wizard:
...
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;
}
}
...
Hope this helps.
Upvotes: 1
Reputation: 22070
That's not possible (and I think it is good, otherwise there wouldn't be a consistent look for plugins from different sources).
Alternatives:
Upvotes: 2