Example usage for org.eclipse.jface.action ToolBarContributionItem setVisible

List of usage examples for org.eclipse.jface.action ToolBarContributionItem setVisible

Introduction

In this page you can find the example usage for org.eclipse.jface.action ToolBarContributionItem setVisible.

Prototype

@Override
public void setVisible(boolean visible) 

Source Link

Document

The default implementation of this IContributionItem method stores the value in an internal state variable, which is true by default.

Usage

From source file:org.nightlabs.base.ui.action.registry.AbstractActionRegistry.java

License:Open Source License

/**
 * Removes all contributions of this registry from the CoolBar of the
 * given coolBarManager. Ideally this is done by making the contributions
 * invisible, so Eclipse can remember their positions. However
 * this is currently not possible and thats why this method
 * removes the affected contributions from the CoolBar.
 * This behaviour can is configured via {@link #useRemoveInsteadOfUnvisibleWorkaround}.
 *
 * @param coolBarManager The {@link ICoolBarManager} where the contributions of this registry should be removed from.
 *///from  w w  w. ja  v a 2 s  . c  o m
public void removeAllFromCoolBar(ICoolBarManager coolBarManager) {
    if (Display.getCurrent() == null)
        throw new IllegalStateException("This method must be called on the UI thread!"); //$NON-NLS-1$

    IContributionManager coolBarContributionManager = coolBarManager;
    if (coolBarManager instanceof SubCoolBarManager)
        coolBarContributionManager = ((SubCoolBarManager) coolBarManager).getParent();

    if (!useRemoveInsteadOfUnvisibleWorkaround)
        ((SubCoolBarManager) coolBarManager).setVisible(false);

    String baseID = this.getClass().getName();
    String orphanageToolbarID = baseID + '.' + ORPHANAGE_TOOLBAR_ID;
    // We use a temporary MenuManager which will be translated into the real
    // coolbar afterwards.
    MenuManager tmpMenu = new MenuManager();
    ActionVisibilityDecider backupActionVisibilityDecider = this.actionVisibilityDecider;
    try {
        this.actionVisibilityDecider = actionVisibilityDeciderAlwaysVisible;
        contribute(tmpMenu, ContributionManagerKind.coolBar);
    } finally {
        this.actionVisibilityDecider = backupActionVisibilityDecider;
    }

    // convert the existing items of the real coolbar-manager into a Map - the new items might
    // already exist because of Eclipse's workspace memory (and then the old ones need to be
    // manipulated - new ones would be ignored because of a bug/feature in the EclipseRCP)
    //      IContributionItem[] coolBarItems = ((SubCoolBarManager)coolBarManager).getParent().getItems();
    IContributionItem[] coolBarItems = coolBarContributionManager.getItems();

    // key: String itemId
    // value: IXContributionItem
    Map<String, IContributionItem> coolBarItemMap = new HashMap<String, IContributionItem>(coolBarItems.length);
    for (int i = 0; i < coolBarItems.length; ++i) {
        IContributionItem coolBarItem = coolBarItems[i];
        coolBarItemMap.put(coolBarItem.getId(), coolBarItem);
        logger.debug("Having " + coolBarItem.getId() + " in CoolBar"); //$NON-NLS-1$ //$NON-NLS-2$
    }

    ToolBarContributionItem orphanageToolBarContributionItem = getToolBarContributionItem(
            coolBarItemMap.get(orphanageToolbarID));
    if (orphanageToolBarContributionItem != null) {
        IContributionItem item = coolBarContributionManager.find(orphanageToolBarContributionItem.getId());
        if (item != null) {
            if (useRemoveInsteadOfUnvisibleWorkaround) {
                coolBarContributionManager.remove(orphanageToolBarContributionItem.getId());
            } else {
                orphanageToolBarContributionItem.setVisible(false);
                item.setVisible(false);
            }
        }
    }

    // Now, we iterate all the "precompiled" items and contribute them to the coolbar
    IContributionItem[] tmpItems = tmpMenu.getItems();
    for (int i = 0; i < tmpItems.length; ++i) {
        IContributionItem tmpItem = tmpItems[i];

        // Test for items that are already in the parent
        if (tmpItem instanceof IMenuManager) {
            IMenuManager tmpSubMenu = (IMenuManager) tmpItem;
            String tmpSubMenuID = baseID + '.' + tmpSubMenu.getId();

            ToolBarContributionItem toolBarContributionItem = getToolBarContributionItem(
                    coolBarItemMap.get(tmpSubMenuID));
            if (toolBarContributionItem != null) {
                IContributionItem item = coolBarContributionManager.find(toolBarContributionItem.getId());
                if (item != null) {
                    if (useRemoveInsteadOfUnvisibleWorkaround) {
                        coolBarContributionManager.remove(tmpSubMenuID);
                    } else {
                        toolBarContributionItem.setVisible(false);
                        item.setVisible(false);
                    }
                }
            }
        }
    }

    try {
        coolBarContributionManager.update(true);
    } catch (Exception x) {
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=239945
        logger.error("CoolBarManager.update failed: " + x.getLocalizedMessage(), x); //$NON-NLS-1$
    }
}