Zombies
Zombies

Reputation: 25872

Add an SWT Widget onto an eclipse plug-in's view's

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.

just an ordinary one, but I decided to upload an image to better articulate what I am talking about

Upvotes: 0

Views: 877

Answers (2)

Waqas Ilyas
Waqas Ilyas

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

Bananeweizen
Bananeweizen

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:

  • The action bar is only visible, if the view is active. And then also the view content area is visible, where you can place any number of widgets that you may need. So you could add that label at the very top, similar to how the Search view shows the current search term at the top.
  • You could modify the name of the view itself (what you see as tab above). But this is not recommended if it happens often, because it leads to an irritating re-layout of the tabs in that tab bar.
  • If you want to show some kind of state information in that label, maybe you can use a dropdown button with checkboxes or radiobuttons to do that. E.g. like the search history in the search view or the active console selection dropdown in the console view.

Upvotes: 2

Related Questions