📄 coolbarmanager.java
字号:
if (currentRow == targetRow) { // We found the row to insert the item int virtualIndex = 0; insertAt = iterator.nextIndex(); // first check the position of the current element (item) // then get the next element while (iterator.hasNext()) { IContributionItem item = (IContributionItem) iterator .next(); Integer itemRow = (Integer) itemLocation.get(item); if (item.isSeparator()) { break; } // if the item has an associate widget if ((itemRow != null) && (itemRow.intValue() == targetRow)) { // if the next element is the index we are looking for // then break if (virtualIndex >= index) { break; } virtualIndex++; } insertAt++; } // If we don't need to move it then we return if (cbInternalIndex == insertAt) { return; } break; } nextRow(iterator, true); } contributionList.remove(cbItem); // Adjust insertAt index if (cbInternalIndex < insertAt) { insertAt--; } // if we didn't find the row then add a new row if (currentRow != targetRow) { contributionList.add(new Separator(USER_SEPARATOR)); insertAt = contributionList.size(); } insertAt = Math.min(insertAt, contributionList.size()); contributionList.add(insertAt, cbItem); } /** * Restores the canonical order of this cool bar manager. The canonical * order is the order in which the contribution items where added. */ public void resetItemOrder() { for (ListIterator iterator = cbItemsCreationOrder.listIterator(); iterator .hasNext();) { IContributionItem item = (IContributionItem) iterator.next(); // if its a user separator then do not include in original order. if ((item.getId() != null) && (item.getId().equals(USER_SEPARATOR))) { iterator.remove(); } } IContributionItem[] itemsToSet = new IContributionItem[cbItemsCreationOrder .size()]; cbItemsCreationOrder.toArray(itemsToSet); setItems(itemsToSet); } /* * (non-Javadoc) * * @see org.eclipse.jface.action.ICoolBarManager#setContextMenuManager(org.eclipse.jface.action.IMenuManager) */ public void setContextMenuManager(IMenuManager contextMenuManager) { this.contextMenuManager = (MenuManager) contextMenuManager; if (coolBar != null) { coolBar.setMenu(getContextMenuControl()); } } /** * Replaces the current items with the given items. * Forces an update. * * @param newItems the items with which to replace the current items */ public void setItems(IContributionItem[] newItems) { // dispose of all the cool items on the cool bar manager if (coolBar != null) { CoolItem[] coolItems = coolBar.getItems(); for (int i = 0; i < coolItems.length; i++) { dispose(coolItems[i]); } } // Set the internal structure to this order internalSetItems(newItems); // Force and update update(true); } /* * (non-Javadoc) * * @see org.eclipse.jface.action.ICoolBarManager#lockLayout(boolean) */ public void setLockLayout(boolean value) { if (!coolBarExist()) { return; } coolBar.setLocked(value); } /** * Subclasses may extend this <code>IContributionManager</code> method, * but must call <code>super.update</code>. * * @see org.eclipse.jface.action.IContributionManager#update(boolean) */ public void update(boolean force) { if ((!isDirty() && !force) || (!coolBarExist())) { return; } boolean relock = false; boolean changed = false; try { coolBar.setRedraw(false); // Refresh the widget data with the internal data structure. refresh(); if (coolBar.getLocked()) { coolBar.setLocked(false); relock = true; } /* * Make a list of items including only those items that are * visible. Separators should stay because they mark line breaks in * a cool bar. */ 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()) { visibleItems.add(item); } } /* * Make a list of CoolItem widgets in the cool bar for which there * is no current visible contribution item. These are the widgets * to be disposed. Dynamic items are also removed. */ CoolItem[] coolItems = coolBar.getItems(); final ArrayList coolItemsToRemove = new ArrayList(coolItems.length); for (int i = 0; i < coolItems.length; i++) { final Object data = coolItems[i].getData(); if ((data == null) || (!visibleItems.contains(data)) || ((data instanceof IContributionItem) && ((IContributionItem) data) .isDynamic())) { coolItemsToRemove.add(coolItems[i]); } } // Dispose of any items in the list to be removed. for (int i = coolItemsToRemove.size() - 1; i >= 0; i--) { CoolItem coolItem = (CoolItem) coolItemsToRemove.get(i); if (!coolItem.isDisposed()) { Control control = coolItem.getControl(); if (control != null) { coolItem.setControl(null); control.dispose(); } coolItem.dispose(); } } // Add any new items by telling them to fill. coolItems = coolBar.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 < coolItems.length) { destinationItem = (IContributionItem) coolItems[sourceIndex] .getData(); } else { destinationItem = null; } // The items match is they are equal or both separators. if (destinationItem != null) { if (sourceItem.equals(destinationItem)) { sourceIndex++; destinationIndex++; sourceItem.update(); continue; } else if ((destinationItem.isSeparator()) && (sourceItem.isSeparator())) { coolItems[sourceIndex].setData(sourceItem); sourceIndex++; destinationIndex++; sourceItem.update(); continue; } } // Otherwise, a new item has to be added. final int start = coolBar.getItemCount(); sourceItem.fill(coolBar, destinationIndex); final int newItems = coolBar.getItemCount() - start; for (int i = 0; i < newItems; i++) { coolBar.getItem(destinationIndex++).setData(sourceItem); } changed = true; } // Remove any old widgets not accounted for. for (int i = coolItems.length - 1; i >= sourceIndex; i--) { final CoolItem item = coolItems[i]; if (!item.isDisposed()) { Control control = item.getControl(); if (control != null) { item.setControl(null); control.dispose(); } item.dispose(); changed = true; } } // Update wrap indices. updateWrapIndices(); // Update the sizes. for (int i = 0; i < items.length; i++) { IContributionItem item = items[i]; item.update(SIZE); } // if the coolBar was previously locked then lock it if (relock) { coolBar.setLocked(true); } if (changed) { updateTabOrder(); } // We are no longer dirty. setDirty(false); } finally { coolBar.setRedraw(true); } } /** * Sets the tab order of the coolbar to the visual order of its items. */ /* package */void updateTabOrder() { if (coolBar != null) { CoolItem[] items = coolBar.getItems(); if (items != null) { ArrayList children = new ArrayList(items.length); for (int i = 0; i < items.length; i++) { if ((items[i].getControl() != null) && (!items[i].getControl().isDisposed())) { children.add(items[i].getControl()); } } // Convert array Control[] childrenArray = new Control[0]; childrenArray = (Control[]) children.toArray(childrenArray); if (childrenArray != null) { coolBar.setTabList(childrenArray); } } } } /** * Updates the indices at which the cool bar should wrap. */ private void updateWrapIndices() { final IContributionItem[] items = getItems(); final int numRows = getNumRows(items) - 1; // Generate the list of wrap indices. final int[] wrapIndices = new int[numRows]; boolean foundSeparator = false; int j = 0; CoolItem[] coolItems = (coolBar == null) ? null : coolBar.getItems(); for (int i = 0; i < items.length; i++) { IContributionItem item = items[i]; CoolItem coolItem = findCoolItem(coolItems, item); if (item.isSeparator()) { foundSeparator = true; } if ((!item.isSeparator()) && (!item.isGroupMarker()) && (item.isVisible()) && (coolItem != null) && (foundSeparator)) { wrapIndices[j] = coolBar.indexOf(coolItem); j++; foundSeparator = false; } } /* * Check to see if these new wrap indices are different than the old * ones. */ final int[] oldIndices = coolBar.getWrapIndices(); boolean shouldUpdate = false; if (oldIndices.length == wrapIndices.length) { for (int i = 0; i < oldIndices.length; i++) { if (oldIndices[i] != wrapIndices[i]) { shouldUpdate = true; break; } } } else { shouldUpdate = true; } if (shouldUpdate) { coolBar.setWrapIndices(wrapIndices); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -