Custom Perspective Switcher Toolbar: How can I dynamically update it?

*爱你&永不变心* 提交于 2019-12-12 03:36:21

问题


I'm trying to implement a custom perspective switcher toolbar to replace eclipse's built-in one. I couldn't get the toolbar to display, and it was shown to me that due to a bug with the dynamic element in a menu contribution, I have to use a control element instead, as described in the workaround to the dynamic bug.

I have a toolbar displaying following that approach, but I cannot figure out how to update it dynamically. The workaround instruction is to call ContributionItem#fill(CoolBar, int) from my WorkbenchControlContributionItem's update method instead of doing the fill in the createControl method.

I don't know who is supposed to call update, but it never gets invoked no matter what I do. I have a perspective listener which knows when to update the toolbar, so from that listener's callback I call fill(CoolBar, int). But I wasn't sure how to get the CoolBar to pass to that method, so I created one on the current shell.

The end result of all this is that the toolbar displays the correct number of items initially, but when I need to add an item, it has no effect. I call fill(CoolBar, int) and it adds the new item to the toolbar, but everything I've tried to make the CoolBar and ToolBarupdate does not work. When I re-launch the app, the toolbar has the added item.

I'm sure I'm doing this wrong, but I can't figure out the right way. Here's an elided representation of my code (omitting methods, layout code, etc not related to the update problem).

public class PerspectiveSwitcherToolbar extends WorkbenchWindowControlContribution implements IPerspectiveListener {
...
@Override
    protected Control createControl(Composite parent) {
        this.parent = parent;
        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        page.getWorkbenchWindow().addPerspectiveListener(this);
        toolBarManager = (ToolBarManager)parent.getParent().getData();
        fTopControl = new Composite(parent, SWT.BORDER);
        fill(new CoolBar(page.getWorkbenchWindow().getShell(), SWT.HORIZONTAL), -1);
        return fTopControl;
}

@Override
    public void fill(CoolBar coolbar, int index) {
        IPerspectiveDescriptor[] openPerspectives = page.getOpenPerspectives();
        String activePerspective = getPerspectiveId();
        ToolBar toolbar = new ToolBar(fTopControl, SWT.NONE);
        for(IPerspectiveDescriptor descriptor : openPerspectives) {
            ToolItem item = new ToolItem(toolbar, SWT.RADIO);
        //overkill here, trying to find some way to upate the toolbar
        toolbar.update();
        parent.update();
        parent.layout(true);
        parent.getParent().update();
        parent.getParent().layout(true);
        coolbar.layout(true);
}

//PerspectiveListener callback
@Override
    public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
        fill(new CoolBar(page.getWorkbenchWindow().getShell(), SWT.HORIZONTAL), -1);
        if (page.getWorkbenchWindow() instanceof WorkbenchWindow){
            //this non-API call doesn't help either
            ((WorkbenchWindow) page.getWorkbenchWindow()).updateActionBars();
        }
}
...
}

来源:https://stackoverflow.com/questions/35835499/custom-perspective-switcher-toolbar-how-can-i-dynamically-update-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!