📄 coolbarmanager.java
字号:
/******************************************************************************* * Copyright (c) 2003, 2006 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.jface.action;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.ListIterator;import org.eclipse.jface.util.Assert;import org.eclipse.jface.util.Policy;import org.eclipse.swt.SWT;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.CoolBar;import org.eclipse.swt.widgets.CoolItem;import org.eclipse.swt.widgets.Menu;/** * A cool bar manager is a contribution manager which realizes itself and its * items in a cool bar control. * <p> * This class may be instantiated; it may also be subclassed. * </p> * * @since 3.0 */public class CoolBarManager extends ContributionManager implements ICoolBarManager { /** * A separator created by the end user. */ public final static String USER_SEPARATOR = "UserSeparator"; //$NON-NLS-1$ /** * The original creation order of the contribution items. */ private ArrayList cbItemsCreationOrder = new ArrayList(); /** * MenuManager for cool bar pop-up menu, or null if none. */ private MenuManager contextMenuManager = null; /** * The cool bar control; <code>null</code> before creation and after * disposal. */ private CoolBar coolBar = null; /** * The cool bar items style; <code>SWT.NONE</code> by default. */ private int itemStyle = SWT.NONE; /** * Creates a new cool bar manager with the default style. Equivalent to * <code>CoolBarManager(SWT.NONE)</code>. */ public CoolBarManager() { // do nothing } /** * Creates a cool bar manager for an existing cool bar control. This * manager becomes responsible for the control, and will dispose of it when * the manager is disposed. * * @param coolBar * the cool bar control */ public CoolBarManager(CoolBar coolBar) { this(); Assert.isNotNull(coolBar); this.coolBar = coolBar; itemStyle = coolBar.getStyle(); } /** * Creates a cool bar manager with the given SWT style. Calling <code>createControl</code> * will create the cool bar control. * * @param style * the cool bar item style; see * {@link org.eclipse.swt.widgets.CoolBar CoolBar}for for valid * style bits */ public CoolBarManager(int style) { itemStyle = style; } /* * (non-Javadoc) * * @see org.eclipse.jface.action.ICoolBarManager#add(org.eclipse.jface.action.IToolBarManager) */ public void add(IToolBarManager toolBarManager) { Assert.isNotNull(toolBarManager); super.add(new ToolBarContributionItem(toolBarManager)); } /** * Collapses consecutive separators and removes a separator from the * beginning and end of the list. * * @param contributionList * the list of contributions; must not be <code>null</code>. * @return The contribution list provided with extraneous separators * removed; this value is never <code>null</code>, but may be * empty. */ private ArrayList adjustContributionList(ArrayList contributionList) { IContributionItem item; // Fist remove a separator if it is the first element of the list if (contributionList.size() != 0) { item = (IContributionItem) contributionList.get(0); if (item.isSeparator()) { contributionList.remove(0); } ListIterator iterator = contributionList.listIterator(); // collapse consecutive separators while (iterator.hasNext()) { item = (IContributionItem) iterator.next(); if (item.isSeparator()) { while (iterator.hasNext()) { item = (IContributionItem) iterator.next(); if (item.isSeparator()) { iterator.remove(); } else { break; } } } } // Now check last element to see if there is a separator item = (IContributionItem) contributionList.get(contributionList .size() - 1); if (item.isSeparator()) { contributionList.remove(contributionList.size() - 1); } } return contributionList; } /* (non-Javadoc) * @see org.eclipse.jface.action.ContributionManager#checkDuplication(org.eclipse.jface.action.IContributionItem) */ protected boolean allowItem(IContributionItem itemToAdd) { /* We will allow as many null entries as they like, though there should * be none. */ if (itemToAdd == null) { return true; } /* Null identifiers can be expected in generic contribution items. */ String firstId = itemToAdd.getId(); if (firstId == null) { return true; } // Cycle through the current list looking for duplicates. IContributionItem[] currentItems = getItems(); for (int i = 0; i < currentItems.length; i++) { IContributionItem currentItem = currentItems[i]; // We ignore null entries. if (currentItem == null) { continue; } String secondId = currentItem.getId(); if (firstId.equals(secondId)) { if (Policy.TRACE_TOOLBAR) { System.out.println("Trying to add a duplicate item."); //$NON-NLS-1$ new Exception().printStackTrace(System.out); System.out.println("DONE --------------------------"); //$NON-NLS-1$ } return false; } } return true; } /** * Positions the list iterator to the end of all the separators. Calling * <code>next()</code> the iterator should return the immediate object * following the last separator. * * @param iterator * the list iterator. */ private void collapseSeparators(ListIterator iterator) { while (iterator.hasNext()) { IContributionItem item = (IContributionItem) iterator.next(); if (!item.isSeparator()) { iterator.previous(); return; } } } /** * Returns whether the cool bar control has been created and not yet * disposed. * * @return <code>true</code> if the control has been created and not yet * disposed, <code>false</code> otherwise */ private boolean coolBarExist() { return coolBar != null && !coolBar.isDisposed(); } /** * Creates and returns this manager's cool bar control. Does not create a * new control if one already exists. * * @param parent * the parent control * @return the cool bar control */ public CoolBar createControl(Composite parent) { Assert.isNotNull(parent); if (!coolBarExist()) { coolBar = new CoolBar(parent, itemStyle); coolBar.setMenu(getContextMenuControl()); coolBar.setLocked(false); update(false); } return coolBar; } /** * Disposes of this cool bar manager and frees all allocated SWT resources. * Notifies all contribution items of the dispose. Note that this method * does not clean up references between this cool bar manager and its * associated contribution items. Use <code>removeAll</code> for that * purpose. */ public void dispose() { if (coolBarExist()) { IContributionItem[] items = getItems(); for (int i = 0; i < items.length; i++) { // Disposes of the contribution item. // If Contribution Item is a toolbar then it will dispose of // all the nested // contribution items. items[i].dispose(); } coolBar.dispose(); coolBar = null; } // If a context menu existed then dispose of it. if (contextMenuManager != null) { contextMenuManager.dispose(); contextMenuManager = null; } } /** * Disposes the given cool item. * * @param item * the cool item to dispose */ private void dispose(CoolItem item) { if ((item != null) && !item.isDisposed()) { item.setData(null); Control control = item.getControl(); // if the control is already disposed, setting the coolitem // control to null will cause an SWT exception, workaround // for 19630 if ((control != null) && !control.isDisposed()) { item.setControl(null); } item.dispose(); } } /** * Finds the cool item associated with the given contribution item. * * @param item * the contribution item * @return the associated cool item, or <code>null</code> if not found */ private CoolItem findCoolItem(IContributionItem item) { CoolItem[] coolItems = (coolBar == null) ? null : coolBar.getItems(); return findCoolItem(coolItems, item); } private CoolItem findCoolItem(CoolItem[] items, IContributionItem item) { if (items == null) { return null; } for (int i = 0; i < items.length; i++) { CoolItem coolItem = items[i]; IContributionItem data = (IContributionItem) coolItem.getData(); if (data != null && data.equals(item)) { return coolItem; } } return null; } /** * Return a consistent set of wrap indices. The return value will always * include at least one entry and the first entry will always be zero. * CoolBar.getWrapIndices() is inconsistent in whether or not it returns an * index for the first row. * * @param wraps * the wrap indicies from the cool bar widget * @return the adjusted wrap indicies. */ private int[] getAdjustedWrapIndices(int[] wraps) { int[] adjustedWrapIndices; if (wraps.length == 0) { adjustedWrapIndices = new int[] { 0 }; } else { if (wraps[0] != 0) { adjustedWrapIndices = new int[wraps.length + 1]; adjustedWrapIndices[0] = 0; for (int i = 0; i < wraps.length; i++) { adjustedWrapIndices[i + 1] = wraps[i]; } } else { adjustedWrapIndices = wraps; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -