Example usage for org.eclipse.jface.internal.provisional.action IToolBarContributionItem getToolBarManager

List of usage examples for org.eclipse.jface.internal.provisional.action IToolBarContributionItem getToolBarManager

Introduction

In this page you can find the example usage for org.eclipse.jface.internal.provisional.action IToolBarContributionItem getToolBarManager.

Prototype

public IToolBarManager getToolBarManager();

Source Link

Document

Returns the internal tool bar manager of the contribution item.

Usage

From source file:com.aptana.ide.core.ui.actions.AbstractWorkbenchWindowPulldownDelegate.java

License:Open Source License

/**
 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
 *///from   ww w.  ja va2  s.  co m
public void run(IAction action) {
    String id = action.getId();
    ToolItem widget = null;

    WorkbenchWindow window = (WorkbenchWindow) PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    CoolBarManager manager = window.getCoolBarManager();
    CoolBar parent = manager.getControl();
    // this returns the list of actionSets groups
    IContributionItem[] items = manager.getItems();
    for (IContributionItem item : items) {
        if (item instanceof IToolBarContributionItem) {
            IToolBarContributionItem toolbarItem = (IToolBarContributionItem) item;
            // this returns the list of actual items for the actions
            IContributionItem[] children = toolbarItem.getToolBarManager().getItems();
            for (IContributionItem child : children) {
                if (child.getId().equals(id)) {
                    // found the toolbar item that corresponds to the action
                    ActionContributionItem actionItem = (ActionContributionItem) child;
                    if (CoreUIUtils.inEclipse34orHigher) {
                        // uses the 3.4 API
                        widget = (ToolItem) actionItem.getWidget();
                    }
                    break;
                }
            }
        }
    }
    Menu menu = getMenu(parent);
    if (widget != null) {
        // sets the location of where the menu is displayed to be the same
        // as when the dropdown arrow is clicked
        Rectangle bounds = widget.getBounds();
        Point point = widget.getParent().toDisplay(bounds.x, bounds.y + bounds.height);
        menu.setLocation(point);
    }
    menu.setVisible(true);
}

From source file:com.astra.ses.spell.dev.advisor.WorkbenchActionBuilder.java

License:Open Source License

/**
 * Update the build actions on the toolbar and menu bar based on the current
 * state of autobuild. This method can be called from any thread.
 * /*  ww w  .  j  a v  a  2s  .  c o  m*/
 * @param immediately
 *            <code>true</code> to update the actions immediately,
 *            <code>false</code> to queue the update to be run in the
 *            event loop
 */
void updateBuildActions(boolean immediately) {
    // this can be triggered by property or resource change notifications
    Runnable update = new Runnable() {
        public void run() {
            if (isDisposed) {
                return;
            }
            IWorkspace workspace = ResourcesPlugin.getWorkspace();
            IProject[] projects = workspace.getRoot().getProjects();
            boolean enabled = BuildUtilities.isEnabled(projects, IncrementalProjectBuilder.INCREMENTAL_BUILD);
            //update menu bar actions in project menu
            buildAllAction.setEnabled(enabled);
            buildProjectAction.setEnabled(enabled);
            toggleAutoBuildAction.setChecked(workspace.isAutoBuilding());
            cleanAction.setEnabled(BuildUtilities.isEnabled(projects, IncrementalProjectBuilder.CLEAN_BUILD));

            //update the cool bar build button
            ICoolBarManager coolBarManager = getActionBarConfigurer().getCoolBarManager();
            IContributionItem cbItem = coolBarManager.find(IWorkbenchActionConstants.TOOLBAR_FILE);
            if (!(cbItem instanceof IToolBarContributionItem)) {
                // This should not happen
                System.err.println("File toolbar contribution item is missing"); //$NON-NLS-1$
                return;
            }
            IToolBarContributionItem toolBarItem = (IToolBarContributionItem) cbItem;
            IToolBarManager toolBarManager = toolBarItem.getToolBarManager();
            if (toolBarManager == null) {
                // error if this happens, file toolbar assumed to always exist
                System.err.println("File toolbar is missing"); //$NON-NLS-1$
                return;
            }
        }
    };
    if (immediately) {
        update.run();
    } else {
        // Dispatch the update to be run later in the UI thread.
        // This helps to reduce flicker if autobuild is being temporarily disabled programmatically.
        Shell shell = window.getShell();
        if (shell != null && !shell.isDisposed()) {
            shell.getDisplay().asyncExec(update);
        }
    }
}

From source file:com.astra.ses.spell.dev.advisor.WorkbenchActionBuilder.java

License:Open Source License

/**
  * Update the pin action's tool bar/*from ww  w  .  j  a v  a2s .co m*/
  */
void updatePinActionToolbar() {

    ICoolBarManager coolBarManager = getActionBarConfigurer().getCoolBarManager();
    IContributionItem cbItem = coolBarManager.find(IWorkbenchActionConstants.TOOLBAR_NAVIGATE);
    if (!(cbItem instanceof IToolBarContributionItem)) {
        // This should not happen
        System.err.println("Navigation toolbar contribution item is missing"); //$NON-NLS-1$
        return;
    }
    IToolBarContributionItem toolBarItem = (IToolBarContributionItem) cbItem;
    IToolBarManager toolBarManager = toolBarItem.getToolBarManager();
    if (toolBarManager == null) {
        // error if this happens, navigation toolbar assumed to always exist
        System.err.println("Navigate toolbar is missing"); //$NON-NLS-1$
        return;
    }

    toolBarManager.update(false);
    toolBarItem.update(ICoolBarManager.SIZE);
}

From source file:com.google.dart.tools.deploy.ApplicationActionBarAdvisor.java

License:Open Source License

/**
 * Update the build actions on the toolbar and menu bar based on the current state of autobuild.
 * This method can be called from any thread.
 * /*  w ww . j a  v a 2  s  . c o m*/
 * @param immediately <code>true</code> to update the actions immediately, <code>false</code> to
 *          queue the update to be run in the event loop
 */
void updateBuildActions(boolean immediately) {
    // this can be triggered by property or resource change notifications
    Runnable update = new Runnable() {
        @Override
        public void run() {
            if (isDisposed) {
                return;
            }
            //update the cool bar build button
            ICoolBarManager coolBarManager = getActionBarConfigurer().getCoolBarManager();
            IContributionItem cbItem = coolBarManager.find(IWorkbenchActionConstants.TOOLBAR_FILE);
            if (!(cbItem instanceof IToolBarContributionItem)) {
                // This should not happen
                IDEWorkbenchPlugin.log("File toolbar contribution item is missing"); //$NON-NLS-1$
                return;
            }
            IToolBarContributionItem toolBarItem = (IToolBarContributionItem) cbItem;
            IToolBarManager toolBarManager = toolBarItem.getToolBarManager();
            if (toolBarManager == null) {
                // error if this happens, file toolbar assumed to always exist
                IDEWorkbenchPlugin.log("File toolbar is missing"); //$NON-NLS-1$
                return;
            }
        }
    };
    if (immediately) {
        update.run();
    } else {
        // Dispatch the update to be run later in the UI thread.
        // This helps to reduce flicker if autobuild is being temporarily disabled programmatically.
        Shell shell = window.getShell();
        if (shell != null && !shell.isDisposed()) {
            shell.getDisplay().asyncExec(update);
        }
    }
}

From source file:com.google.dart.tools.deploy.ApplicationActionBarAdvisor.java

License:Open Source License

/**
 * Update the pin action's tool bar//w  w  w.j  a  va 2 s .c  o m
 */
void updatePinActionToolbar() {

    ICoolBarManager coolBarManager = getActionBarConfigurer().getCoolBarManager();
    IContributionItem cbItem = coolBarManager.find(IWorkbenchActionConstants.TOOLBAR_NAVIGATE);
    if (!(cbItem instanceof IToolBarContributionItem)) {
        // This should not happen
        IDEWorkbenchPlugin.log("Navigation toolbar contribution item is missing"); //$NON-NLS-1$
        return;
    }
    IToolBarContributionItem toolBarItem = (IToolBarContributionItem) cbItem;
    IToolBarManager toolBarManager = toolBarItem.getToolBarManager();
    if (toolBarManager == null) {
        // error if this happens, navigation toolbar assumed to always exist
        IDEWorkbenchPlugin.log("Navigate toolbar is missing"); //$NON-NLS-1$
        return;
    }

    toolBarManager.update(false);
    toolBarItem.update(ICoolBarManager.SIZE);
}

From source file:com.sap.dirigible.ide.ui.rap.managers.CoolBarManager.java

License:Open Source License

public void update(final boolean force) {
    if ((isDirty() || force) && getControl2() != null) {
        refresh();// ww  w  .ja  va 2 s  .  c  o m
        boolean changed = false;

        /*
         * Make a list of items including only those items that are visible.
         * Separators are being removed. Because we use only one Toolbar all
         * ToolBarContributionItems will be extracted in their IContribution
         * Items.
         */
        final IContributionItem[] items = getItems();
        final List<IContributionItem> visibleItems = new ArrayList<IContributionItem>(items.length);
        for (int i = 0; i < items.length; i++) {
            final IContributionItem item = items[i];
            if (item.isVisible()) {
                if (item instanceof IToolBarContributionItem) {
                    IToolBarContributionItem toolbarItem = (IToolBarContributionItem) item;
                    IToolBarManager toolBarManager = toolbarItem.getToolBarManager();
                    IContributionItem[] toolbarItems = toolBarManager.getItems();
                    for (int j = 0; j < toolbarItems.length; j++) {
                        final IContributionItem toolItem = toolbarItems[j];
                        if (toolItem.isVisible() && !toolItem.isSeparator()) {
                            visibleItems.add(toolItem);
                        }
                    }
                }
            }
        }

        /*
         * Make a list of ToolItem widgets in the tool bar for which there
         * is no current visible contribution item. These are the widgets to
         * be disposed. Dynamic items are also removed.
         */
        ToolItem[] toolItems = toolbar.getItems();
        final ArrayList<ToolItem> toolItemsToRemove = new ArrayList<ToolItem>(toolItems.length);
        for (int i = 0; i < toolItems.length; i++) {
            final Object data = toolItems[i].getData();
            if ((data == null) || (!visibleItems.contains(data))
                    || ((data instanceof IContributionItem) && ((IContributionItem) data).isDynamic())) {
                toolItemsToRemove.add(toolItems[i]);
            }
        }

        // Dispose of any items in the list to be removed.
        for (int i = toolItemsToRemove.size() - 1; i >= 0; i--) {
            ToolItem toolItem = (ToolItem) toolItemsToRemove.get(i);
            if (!toolItem.isDisposed()) {
                Control control = toolItem.getControl();
                if (control != null) {
                    toolItem.setControl(null);
                    control.dispose();
                }
                toolItem.dispose();
            }
        }

        // Add any new items by telling them to fill.
        toolItems = toolbar.getItems();
        IContributionItem sourceItem;
        IContributionItem destinationItem;
        int sourceIndex = 0;
        int destinationIndex = 0;
        final Iterator<IContributionItem> visibleItemItr = visibleItems.iterator();
        while (visibleItemItr.hasNext()) {
            sourceItem = visibleItemItr.next();

            // Retrieve the corresponding contribution item from SWT's
            // data.
            if (sourceIndex < toolItems.length) {
                destinationItem = (IContributionItem) toolItems[sourceIndex].getData();
            } else {
                destinationItem = null;
            }

            // The items match if they are equal or both separators.
            if (destinationItem != null) {
                if (sourceItem.equals(destinationItem)) {
                    sourceIndex++;
                    destinationIndex++;
                    sourceItem.update();
                    continue;
                } else if ((destinationItem.isSeparator()) && (sourceItem.isSeparator())) {
                    toolItems[sourceIndex].setData(sourceItem);
                    sourceIndex++;
                    destinationIndex++;
                    sourceItem.update();
                    continue;
                }
            }

            // Otherwise, a new item has to be added.
            final int start = toolbar.getItemCount();
            sourceItem.fill(toolbar, destinationIndex);
            final int newItems = toolbar.getItemCount() - start;
            // add the selection listener for the styling
            StylingSelectionAdapter listener = new StylingSelectionAdapter(HEADER_TOOLBAR_VARIANT);
            for (int i = 0; i < newItems; i++) {
                ToolItem item = toolbar.getItem(destinationIndex++);
                item.setData(sourceItem);
                item.addSelectionListener(listener);
            }
            changed = true;
        }

        // Remove any old widgets not accounted for.
        for (int i = toolItems.length - 1; i >= sourceIndex; i--) {
            final ToolItem item = toolItems[i];
            if (!item.isDisposed()) {
                Control control = item.getControl();
                if (control != null) {
                    item.setControl(null);
                    control.dispose();
                }
                item.dispose();
                changed = true;
            }
        }

        // Update wrap indices. only needed by a coolbar
        // updateWrapIndices();

        // Update the sizes.
        for (int i = 0; i < items.length; i++) {
            IContributionItem item = items[i];
            item.update(SIZE);
        }

        if (changed) {
            updateToolbarTabOrder();
        }

        // We are no longer dirty.
        setDirty(false);
        styleToolItems();
        toolbar.pack();
        toolbar.layout(true, true);
        manageOverflow();
    }
}

From source file:org.eclipse.datatools.enablement.rcp.ApplicationActionBarAdvisor.java

License:Open Source License

/**
 * Update the build actions on the toolbar and menu bar based on the current
 * state of autobuild. This method can be called from any thread.
 * //ww w . j a v  a2  s . c  o  m
 * @param immediately
 *            <code>true</code> to update the actions immediately,
 *            <code>false</code> to queue the update to be run in the
 *            event loop
 */
void updateBuildActions(boolean immediately) {
    // this can be triggered by property or resource change notifications
    Runnable update = new Runnable() {
        public void run() {
            if (isDisposed) {
                return;
            }
            IWorkspace workspace = ResourcesPlugin.getWorkspace();
            IProject[] projects = workspace.getRoot().getProjects();
            boolean enabled = BuildUtilities.isEnabled(projects, IncrementalProjectBuilder.INCREMENTAL_BUILD);
            //update menu bar actions in project menu
            buildAllAction.setEnabled(enabled);
            buildProjectAction.setEnabled(enabled);
            toggleAutoBuildAction.setChecked(workspace.isAutoBuilding());
            cleanAction.setEnabled(BuildUtilities.isEnabled(projects, IncrementalProjectBuilder.CLEAN_BUILD));

            //update the cool bar build button
            ICoolBarManager coolBarManager = getActionBarConfigurer().getCoolBarManager();
            IContributionItem cbItem = coolBarManager.find(IWorkbenchActionConstants.TOOLBAR_FILE);
            if (!(cbItem instanceof IToolBarContributionItem)) {
                // This should not happen
                IDEWorkbenchPlugin.log("File toolbar contribution item is missing"); //$NON-NLS-1$
                return;
            }
            IToolBarContributionItem toolBarItem = (IToolBarContributionItem) cbItem;
            IToolBarManager toolBarManager = toolBarItem.getToolBarManager();
            if (toolBarManager == null) {
                // error if this happens, file toolbar assumed to always exist
                IDEWorkbenchPlugin.log("File toolbar is missing"); //$NON-NLS-1$
                return;
            }
            //add the build button if build actions are enabled, and remove it otherwise
            boolean found = toolBarManager.find(buildAllAction.getId()) != null;
            if (enabled && !found) {
                toolBarManager.appendToGroup(IWorkbenchActionConstants.BUILD_GROUP, buildAllAction);
                toolBarManager.update(false);
                toolBarItem.update(ICoolBarManager.SIZE);
            } else if (buildAllAction != null && found && !enabled) {
                toolBarManager.remove(buildAllAction.getId());
                toolBarManager.update(false);
                toolBarItem.update(ICoolBarManager.SIZE);
            }
        }
    };
    if (immediately) {
        update.run();
    } else {
        // Dispatch the update to be run later in the UI thread.
        // This helps to reduce flicker if autobuild is being temporarily disabled programmatically.
        Shell shell = window.getShell();
        if (shell != null && !shell.isDisposed()) {
            shell.getDisplay().asyncExec(update);
        }
    }
}

From source file:org.eclipse.rap.internal.design.example.managers.CoolBarManager.java

License:Open Source License

@Override
public void update(final boolean force) {
    if ((isDirty() || force) && getControl2() != null) {
        refresh();// w w  w  .  jav a  2 s .c o m
        boolean changed = false;

        /*
         * Make a list of items including only those items that are
         * visible. Separators are being removed. Because we use only one Toolbar
         * all ToolBarContributionItems will be extracted in their IContribution
         * Items.
         */
        final IContributionItem[] items = getItems();
        final List visibleItems = new ArrayList(items.length);
        for (int i = 0; i < items.length; i++) {
            final IContributionItem item = items[i];
            if (item.isVisible()) {
                if (item instanceof IToolBarContributionItem) {
                    IToolBarContributionItem toolbarItem = (IToolBarContributionItem) item;
                    IToolBarManager toolBarManager = toolbarItem.getToolBarManager();
                    IContributionItem[] toolbarItems = toolBarManager.getItems();
                    for (int j = 0; j < toolbarItems.length; j++) {
                        final IContributionItem toolItem = toolbarItems[j];
                        if (toolItem.isVisible() && !toolItem.isSeparator()) {
                            visibleItems.add(toolItem);
                        }
                    }
                }
            }
        }

        /*
         * Make a list of ToolItem widgets in the tool bar for which there
         * is no current visible contribution item. These are the widgets
         * to be disposed. Dynamic items are also removed.
         */
        ToolItem[] toolItems = toolbar.getItems();
        final ArrayList toolItemsToRemove = new ArrayList(toolItems.length);
        for (int i = 0; i < toolItems.length; i++) {
            final Object data = toolItems[i].getData();
            if ((data == null) || (!visibleItems.contains(data))
                    || ((data instanceof IContributionItem) && ((IContributionItem) data).isDynamic())) {
                toolItemsToRemove.add(toolItems[i]);
            }
        }

        // Dispose of any items in the list to be removed.
        for (int i = toolItemsToRemove.size() - 1; i >= 0; i--) {
            ToolItem toolItem = (ToolItem) toolItemsToRemove.get(i);
            if (!toolItem.isDisposed()) {
                Control control = toolItem.getControl();
                if (control != null) {
                    toolItem.setControl(null);
                    control.dispose();
                }
                toolItem.dispose();
            }
        }

        // Add any new items by telling them to fill.
        toolItems = toolbar.getItems();
        IContributionItem sourceItem;
        IContributionItem destinationItem;
        int sourceIndex = 0;
        int destinationIndex = 0;
        final Iterator visibleItemItr = visibleItems.iterator();
        while (visibleItemItr.hasNext()) {
            sourceItem = (IContributionItem) visibleItemItr.next();

            // Retrieve the corresponding contribution item from SWT's
            // data.
            if (sourceIndex < toolItems.length) {
                destinationItem = (IContributionItem) toolItems[sourceIndex].getData();
            } else {
                destinationItem = null;
            }

            // The items match if they are equal or both separators.
            if (destinationItem != null) {
                if (sourceItem.equals(destinationItem)) {
                    sourceIndex++;
                    destinationIndex++;
                    sourceItem.update();
                    continue;
                } else if ((destinationItem.isSeparator()) && (sourceItem.isSeparator())) {
                    toolItems[sourceIndex].setData(sourceItem);
                    sourceIndex++;
                    destinationIndex++;
                    sourceItem.update();
                    continue;
                }
            }

            // Otherwise, a new item has to be added.
            final int start = toolbar.getItemCount();
            sourceItem.fill(toolbar, destinationIndex);
            final int newItems = toolbar.getItemCount() - start;
            // add the selection listener for the styling
            StylingSelectionAdapter listener = new StylingSelectionAdapter(HEADER_TOOLBAR_VARIANT);
            for (int i = 0; i < newItems; i++) {
                ToolItem item = toolbar.getItem(destinationIndex++);
                item.setData(sourceItem);
                item.addSelectionListener(listener);
            }
            changed = true;
        }

        // Remove any old widgets not accounted for.
        for (int i = toolItems.length - 1; i >= sourceIndex; i--) {
            final ToolItem item = toolItems[i];
            if (!item.isDisposed()) {
                Control control = item.getControl();
                if (control != null) {
                    item.setControl(null);
                    control.dispose();
                }
                item.dispose();
                changed = true;
            }
        }

        // Update wrap indices. only needed by a coolbar
        //updateWrapIndices();

        // Update the sizes.
        for (int i = 0; i < items.length; i++) {
            IContributionItem item = items[i];
            item.update(SIZE);
        }

        if (changed) {
            updateToolbarTabOrder();
        }

        // We are no longer dirty.
        setDirty(false);
        styleToolItems();
        toolbar.pack();
        toolbar.layout(true, true);
        manageOverflow();
    }
}

From source file:org.eclipse.ui.internal.ActionSetActionBars.java

License:Open Source License

/**
 * Returns the correct tool bar for the given action id. If this action is
 * an adjunct type the it returns the toolbar manager from the cool bar
 * manager./*from w  w  w. j  a  va  2s  .  c o m*/
 * 
 * @param id
 *            the id of the action
 * @return the tool bar manager
 */
public IToolBarManager getToolBarManager(String actionId) {
    // Check if a tool bar manager for an adjunct type is being requested
    String toolBarId = actionSetId;
    boolean isAdjunctType = false;
    if (!actionId.equals(actionSetId)) {
        // Adjunct type
        toolBarId = actionId;
        isAdjunctType = true;
    }

    // Rereive the cool bar manager
    ICoolBarManager coolBarManager = getCastedParent().getCoolBarManager();
    if (coolBarManager == null) {
        return null;
    }

    // Check to see that there isn't already a tool bar created
    // and the tool bar being requested is not for an adjunct action
    if ((coolItemToolBarMgr != null) && (!isAdjunctType)) {
        return coolItemToolBarMgr;
    }

    // Search for toolBarId in the cool Bar manager
    IContributionItem cbItem = coolBarManager.find(toolBarId);
    // If there hasn't been a tool bar contribution item created for this
    // tool bar
    // id then create one. Otherwise retrieve the tool bar contribution
    // item
    if (cbItem instanceof IToolBarContributionItem) {

        IToolBarContributionItem tbcbItem = (IToolBarContributionItem) cbItem;
        coolItemToolBarMgr = tbcbItem.getToolBarManager();
        // If this not an adjuct type then we can cashe the tool bar
        // contribution type
        if (!isAdjunctType) {
            toolBarContributionItem = tbcbItem;
        }
    } else {

        coolItemToolBarMgr = actionBarConfigurer.createToolBarManager();

        // If this is not an adjunct type then create a tool bar
        // contribution item
        // we don't create one for an adjunct type because another action
        // set action bars contains one

        IContributionItem toolBarContributionItem = actionBarConfigurer
                .createToolBarContributionItem(coolItemToolBarMgr, toolBarId);

        toolBarContributionItem.setParent(coolItemToolBarMgr);
        toolBarContributionItem.setVisible(getActive());
        coolItemToolBarMgr.markDirty();

        // Now add the tool bar contribution Item to the cool bar manager
        IContributionItem refItem = findAlphabeticalOrder(IWorkbenchActionConstants.MB_ADDITIONS, toolBarId,
                coolBarManager);
        if (refItem != null) {
            coolBarManager.insertAfter(refItem.getId(), toolBarContributionItem);
        } else {
            coolBarManager.add(toolBarContributionItem);
        }
    }
    return coolItemToolBarMgr;
}

From source file:org.eclipse.ui.internal.ide.WorkbenchActionBuilder.java

License:Open Source License

/**
 * Update the build actions on the toolbar and menu bar based on the current
 * state of autobuild. This method can be called from any thread.
 * /*from   ww w .  j  a  va  2 s. c o m*/
 * @param immediately
 *            <code>true</code> to update the actions immediately,
 *            <code>false</code> to queue the update to be run in the
 *            event loop
 */
void updateBuildActions(boolean immediately) {
    // this can be triggered by property or resource change notifications
    Runnable update = new Runnable() {
        public void run() {
            if (isDisposed) {
                return;
            }
            IWorkspace workspace = ResourcesPlugin.getWorkspace();
            IProject[] projects = workspace.getRoot().getProjects();
            boolean enabled = BuildUtilities.isEnabled(projects, IncrementalProjectBuilder.INCREMENTAL_BUILD);
            //update menu bar actions in project menu
            updateCommandEnablement(buildAllAction.getActionDefinitionId());
            buildProjectAction.setEnabled(enabled);
            toggleAutoBuildAction.setChecked(workspace.isAutoBuilding());
            cleanAction.setEnabled(BuildUtilities.isEnabled(projects, IncrementalProjectBuilder.CLEAN_BUILD));

            //update the cool bar build button
            ICoolBarManager coolBarManager = getActionBarConfigurer().getCoolBarManager();
            IContributionItem cbItem = coolBarManager.find(IWorkbenchActionConstants.TOOLBAR_FILE);
            if (!(cbItem instanceof IToolBarContributionItem)) {
                // This should not happen
                IDEWorkbenchPlugin.log("File toolbar contribution item is missing"); //$NON-NLS-1$
                return;
            }
            IToolBarContributionItem toolBarItem = (IToolBarContributionItem) cbItem;
            IToolBarManager toolBarManager = toolBarItem.getToolBarManager();
            if (toolBarManager == null) {
                // error if this happens, file toolbar assumed to always exist
                IDEWorkbenchPlugin.log("File toolbar is missing"); //$NON-NLS-1$
                return;
            }
            //add the build button if build actions are enabled, and remove it otherwise
            boolean found = toolBarManager.find(buildAllAction.getId()) != null;
            if (enabled && !found) {
                toolBarManager.appendToGroup(IWorkbenchActionConstants.BUILD_GROUP, buildAllAction);
                toolBarManager.update(false);
                toolBarItem.update(ICoolBarManager.SIZE);
            } else if (buildAllAction != null && found && !enabled) {
                toolBarManager.remove(buildAllAction.getId());
                toolBarManager.update(false);
                toolBarItem.update(ICoolBarManager.SIZE);
            }
        }

        private void updateCommandEnablement(String commandId) {
            IHandlerService handlerService = (IHandlerService) window.getService(IHandlerService.class);
            ICommandService commandService = (ICommandService) window.getService(ICommandService.class);
            if (handlerService != null && commandService != null) {
                Command buildAllCmd = commandService.getCommand(commandId);
                buildAllCmd.setEnabled(handlerService.getCurrentState());
            }
        }
    };
    if (immediately) {
        update.run();
    } else {
        // Dispatch the update to be run later in the UI thread.
        // This helps to reduce flicker if autobuild is being temporarily disabled programmatically.
        Shell shell = window.getShell();
        if (shell != null && !shell.isDisposed()) {
            shell.getDisplay().asyncExec(update);
        }
    }
}