📄 coolbarmanager.java
字号:
} return adjustedWrapIndices; } /** * Returns the control of the Menu Manager. If the menu manager does not * have a control then one is created. * * @return menu control associated with manager, or null if none */ private Menu getContextMenuControl() { if ((contextMenuManager != null) && (coolBar != null)) { Menu menuWidget = contextMenuManager.getMenu(); if ((menuWidget == null) || (menuWidget.isDisposed())) { menuWidget = contextMenuManager.createContextMenu(coolBar); } return menuWidget; } return null; } /* * (non-Javadoc) * * @see org.eclipse.jface.action.ICoolBarManager#isLayoutLocked() */ public IMenuManager getContextMenuManager() { return contextMenuManager; } /** * Returns the cool bar control for this manager. * * @return the cool bar control, or <code>null</code> if none */ public CoolBar getControl() { return coolBar; } /** * Returns an array list of all the contribution items in the manager. * * @return an array list of contribution items. */ private ArrayList getItemList() { IContributionItem[] cbItems = getItems(); ArrayList list = new ArrayList(cbItems.length); for (int i = 0; i < cbItems.length; i++) { list.add(cbItems[i]); } return list; } /* * (non-Javadoc) * * @see org.eclipse.jface.action.ICoolBarManager#isLayoutLocked() */ public boolean getLockLayout() { if (!coolBarExist()) { return false; } return coolBar.getLocked(); } /** * Returns the number of rows that should be displayed visually. * * @param items * the array of contributin items * @return the number of rows */ private int getNumRows(IContributionItem[] items) { int numRows = 1; boolean separatorFound = false; for (int i = 0; i < items.length; i++) { if (items[i].isSeparator()) { separatorFound = true; } if ((separatorFound) && (items[i].isVisible()) && (!items[i].isGroupMarker()) && (!items[i].isSeparator())) { numRows++; separatorFound = false; } } return numRows; } /* * (non-Javadoc) * * @see org.eclipse.jface.action.ICoolBarManager#getStyle() */ public int getStyle() { return itemStyle; } /** * Subclasses may extend this <code>ContributionManager</code> method, * but must call <code>super.itemAdded</code>. * * @see org.eclipse.jface.action.ContributionManager#itemAdded(org.eclipse.jface.action.IContributionItem) */ protected void itemAdded(IContributionItem item) { Assert.isNotNull(item); super.itemAdded(item); int insertedAt = indexOf(item); boolean replaced = false; final int size = cbItemsCreationOrder.size(); for (int i = 0; i < size; i++) { IContributionItem created = (IContributionItem) cbItemsCreationOrder .get(i); if (created.getId() != null && created.getId().equals(item.getId())) { cbItemsCreationOrder.set(i, item); replaced = true; break; } } if (!replaced) { cbItemsCreationOrder.add(Math.min(Math.max(insertedAt, 0), cbItemsCreationOrder.size()), item); } } /** * Subclasses may extend this <code>ContributionManager</code> method, * but must call <code>super.itemRemoved</code>. * * @see org.eclipse.jface.action.ContributionManager#itemRemoved(org.eclipse.jface.action.IContributionItem) */ protected void itemRemoved(IContributionItem item) { Assert.isNotNull(item); super.itemRemoved(item); CoolItem coolItem = findCoolItem(item); if (coolItem != null) { coolItem.setData(null); } } /** * Positions the list iterator to the starting of the next row. By calling * next on the returned iterator, it will return the first element of the * next row. * * @param iterator * the list iterator of contribution items * @param ignoreCurrentItem * Whether the current item in the iterator should be considered * (as well as subsequent items). */ private void nextRow(ListIterator iterator, boolean ignoreCurrentItem) { IContributionItem currentElement = null; if (!ignoreCurrentItem && iterator.hasPrevious()) { currentElement = (IContributionItem) iterator.previous(); iterator.next(); } if ((currentElement != null) && (currentElement.isSeparator())) { collapseSeparators(iterator); return; } //Find next separator while (iterator.hasNext()) { IContributionItem item = (IContributionItem) iterator.next(); if (item.isSeparator()) { // we we find a separator, collapse any consecutive // separators // and return collapseSeparators(iterator); return; } } } /* * Used for debuging. Prints all the items in the internal structures. */ // private void printContributions(ArrayList contributionList) { // int index = 0; // System.out.println("----------------------------------\n"); //$NON-NLS-1$ // for (Iterator i = contributionList.iterator(); i.hasNext(); index++) { // IContributionItem item = (IContributionItem) i.next(); // if (item.isSeparator()) { // System.out.println("Separator"); //$NON-NLS-1$ // } else { // System.out.println(index + ". Item id: " + item.getId() //$NON-NLS-1$ // + " - is Visible: " //$NON-NLS-1$ // + item.isVisible()); // } // } // } /** * Synchronizes the visual order of the cool items in the control with this * manager's internal data structures. This method should be called before * requesting the order of the contribution items to ensure that the order * is accurate. * <p> * Note that <code>update()</code> and <code>refresh()</code> are * converses: <code>update()</code> changes the visual order to match the * internal structures, and <code>refresh</code> changes the internal * structures to match the visual order. * </p> */ public void refresh() { if (!coolBarExist()) { return; } // Retreives the list of contribution items as an array list ArrayList contributionList = getItemList(); // Check the size of the list if (contributionList.size() == 0) { return; } // The list of all the cool items in their visual order CoolItem[] coolItems = coolBar.getItems(); // The wrap indicies of the coolbar int[] wrapIndicies = getAdjustedWrapIndices(coolBar.getWrapIndices()); int row = 0; int coolItemIndex = 0; // Traverse through all cool items in the coolbar add them to a new // data structure // in the correct order ArrayList displayedItems = new ArrayList(coolBar.getItemCount()); for (int i = 0; i < coolItems.length; i++) { CoolItem coolItem = coolItems[i]; if (coolItem.getData() instanceof IContributionItem) { IContributionItem cbItem = (IContributionItem) coolItem .getData(); displayedItems.add(Math.min(i, displayedItems.size()), cbItem); } } // Add separators to the displayed Items data structure int offset = 0; for (int i = 1; i < wrapIndicies.length; i++) { int insertAt = wrapIndicies[i] + offset; displayedItems.add(insertAt, new Separator(USER_SEPARATOR)); offset++; } // Determine which rows are invisible ArrayList existingVisibleRows = new ArrayList(4); ListIterator rowIterator = contributionList.listIterator(); collapseSeparators(rowIterator); int numRow = 0; while (rowIterator.hasNext()) { // Scan row while (rowIterator.hasNext()) { IContributionItem cbItem = (IContributionItem) rowIterator .next(); if (displayedItems.contains(cbItem)) { existingVisibleRows.add(new Integer(numRow)); break; } if (cbItem.isSeparator()) { break; } } nextRow(rowIterator, false); numRow++; } Iterator existingRows = existingVisibleRows.iterator(); // Adjust row number to the first visible if (existingRows.hasNext()) { row = ((Integer) existingRows.next()).intValue(); } HashMap itemLocation = new HashMap(); for (ListIterator locationIterator = displayedItems.listIterator(); locationIterator .hasNext();) { IContributionItem item = (IContributionItem) locationIterator .next(); if (item.isSeparator()) { if (existingRows.hasNext()) { Integer value = (Integer) existingRows.next(); row = value.intValue(); } else { row++; } } else { itemLocation.put(item, new Integer(row)); } } // Insert the contribution items in their correct location for (ListIterator iterator = displayedItems.listIterator(); iterator .hasNext();) { IContributionItem cbItem = (IContributionItem) iterator.next(); if (cbItem.isSeparator()) { coolItemIndex = 0; } else { relocate(cbItem, coolItemIndex, contributionList, itemLocation); cbItem.saveWidgetState(); coolItemIndex++; } } if (contributionList.size() != 0) { contributionList = adjustContributionList(contributionList); IContributionItem[] array = new IContributionItem[contributionList .size() - 1]; array = (IContributionItem[]) contributionList.toArray(array); internalSetItems(array); } } /** * Relocates the given contribution item to the specified index. * * @param cbItem * the conribution item to relocate * @param index * the index to locate this item * @param contributionList * the current list of conrtributions * @param itemLocation */ private void relocate(IContributionItem cbItem, int index, ArrayList contributionList, HashMap itemLocation) { if (!(itemLocation.get(cbItem) instanceof Integer)) { return; } int targetRow = ((Integer) itemLocation.get(cbItem)).intValue(); int cbInternalIndex = contributionList.indexOf(cbItem); // by default add to end of list int insertAt = contributionList.size(); // Find the row to place this item in. ListIterator iterator = contributionList.listIterator(); // bypass any separators at the begining collapseSeparators(iterator); int currentRow = -1; while (iterator.hasNext()) { currentRow++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -