List of usage examples for org.eclipse.jface.action ToolBarContributionItem setUseChevron
@Override public void setUseChevron(boolean value)
From source file:org.nightlabs.base.ui.action.registry.AbstractActionRegistry.java
License:Open Source License
/** * @param coolBarManager//from w w w .ja v a 2s. c om * @return Returns the number of visible items (i.e. actions) that have been added (because some * might be invisible and therefore not added). */ public Map<Class<? extends ItemDescriptor>, List<ItemDescriptor>> contributeToCoolBar( ICoolBarManager coolBarManager) { if (coolBarManager instanceof SubContributionManager) ((SubCoolBarManager) coolBarManager).setVisible(true); 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(); Map<Class<? extends ItemDescriptor>, List<ItemDescriptor>> res = contribute(tmpMenu, ContributionManagerKind.coolBar); // all contributionItems with these IDs must not be added to the orphanage menu but directly // to the coolbar // TODO this should be somehow cached Set<String> additions_contributionItemIDs = new HashSet<String>(); additions_contributionItemIDs.add("additions"); // additions itself must not end up in orphanage menu //$NON-NLS-1$ for (ActionDescriptor actionDescriptor : getActionDescriptors()) { if ("additions".equals(actionDescriptor.getToolbarPath())) //$NON-NLS-1$ additions_contributionItemIDs.add(actionDescriptor.getID()); } // 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 = null; if (coolBarManager instanceof SubCoolBarManager) { coolBarItems = ((SubCoolBarManager) coolBarManager).getParent().getItems(); } else if (coolBarManager instanceof IContributionManager) { coolBarItems = ((IContributionManager) coolBarManager).getItems(); } // IContributionItem[] coolBarItems = ((SubCoolBarManager)coolBarManager).getParent().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); } // Clean the "orphanage" toolbar which holds all those actions, that are not wrapped by a menu. // This toolbar does only exist, if it is needed. ToolBarContributionItem orphanageToolBarContributionItem = getToolBarContributionItem( coolBarItemMap.get(orphanageToolbarID)); if (orphanageToolBarContributionItem != null) orphanageToolBarContributionItem.getToolBarManager().removeAll(); // We need to collect all the "orphaned" actions in a menu first and at them after all the other menus. MenuManager orphanageMenu = new MenuManager(); List<IContributionItem> additionsToBeAddedDirectlyToCoolBar = new ArrayList<IContributionItem>(); // 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]; // Either, we've hit a menu or an "orphaned" item if (tmpItem instanceof IMenuManager) { IMenuManager tmpSubMenu = (IMenuManager) tmpItem; String tmpSubMenuID = baseID + '.' + tmpSubMenu.getId(); // find the previously existing ToolBarManager or create a new one. IToolBarManager toolBarManager; ToolBarContributionItem toolBarContributionItem = getToolBarContributionItem( coolBarItemMap.get(tmpSubMenuID)); if (toolBarContributionItem != null) toolBarManager = toolBarContributionItem.getToolBarManager(); else { toolBarManager = new ToolBarManager(); toolBarContributionItem = new ToolBarContributionItem(toolBarManager, tmpSubMenuID); toolBarContributionItem.setUseChevron(false); coolBarManager.add(toolBarContributionItem); } toolBarManager.removeAll(); addFlattenedMenu(toolBarManager, tmpSubMenuID + ".separator", tmpSubMenu); //$NON-NLS-1$ } else { if (additions_contributionItemIDs.contains(tmpItem.getId())) additionsToBeAddedDirectlyToCoolBar.add(tmpItem); else orphanageMenu.add(tmpItem); } } // for (int i = 0; i < tmpItems.length; ++i) { // If we have actions without menus, populate a toolbar with them. if (!orphanageMenu.isEmpty()) { IToolBarManager toolBarManager; orphanageToolBarContributionItem = getToolBarContributionItem(coolBarItemMap.get(orphanageToolbarID)); if (orphanageToolBarContributionItem != null) toolBarManager = orphanageToolBarContributionItem.getToolBarManager(); else { toolBarManager = new ToolBarManager(); orphanageToolBarContributionItem = new ToolBarContributionItem(toolBarManager, orphanageToolbarID); orphanageToolBarContributionItem.setUseChevron(false); coolBarManager.add(orphanageToolBarContributionItem); } toolBarManager.removeAll(); addFlattenedMenu(toolBarManager, orphanageToolbarID + ".separator", orphanageMenu); //$NON-NLS-1$ } // contribute this stuff directly into the CoolBar (not a nested ToolBar). for (IContributionItem contributionItem : additionsToBeAddedDirectlyToCoolBar) { coolBarManager.add(contributionItem); } try { coolBarManager.update(true); } catch (Exception e) { // TODO WORKAROUND // sometimes at first startup an ArrayIndexOutOfBounds occurs, because // CoolBarManager.adjustContributionList removes first an separator and // afterwards also the first element, of an now empty list // this is an workaround for this bug logger.warn("Eclipse bug in coolBarManager.update(true) occured", e); //$NON-NLS-1$ } // if (coolBarManager instanceof CoolBarManager) // ((CoolBarManager)coolBarManager).refresh(); return res; }