📄 toolbarcontributionitem.java
字号:
/******************************************************************************* * Copyright (c) 2000, 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.Iterator;import org.eclipse.jface.util.Assert;import org.eclipse.jface.util.Policy;import org.eclipse.swt.SWT;import org.eclipse.swt.events.DisposeEvent;import org.eclipse.swt.events.DisposeListener;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.CoolBar;import org.eclipse.swt.widgets.CoolItem;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Menu;import org.eclipse.swt.widgets.ToolBar;import org.eclipse.swt.widgets.ToolItem;/** * The <code>ToolBarContributionItem</code> class provides a wrapper for tool * bar managers when used in cool bar managers. It extends <code>ContributionItem</code> * but and provides some additional methods to customize the size of the cool * item and to retrieve the underlying tool bar manager. * <p> * This class may be instantiated; it is not intended to be subclassed. * </p> * * @since 3.0 */public class ToolBarContributionItem extends ContributionItem { /** * A constant used by <code>setMinimumItemsToShow</code> and <code>getMinimumItemsToShow</code> * to indicate that all tool items should be shown in the cool item. */ public static final int SHOW_ALL_ITEMS = -1; /** * The pull down menu used to list all hidden tool items if the current * size is less than the preffered size. */ private MenuManager chevronMenuManager = null; /** * The widget created for this item; <code>null</code> before creation * and after disposal. */ private CoolItem coolItem = null; /** * Current height of cool item */ private int currentHeight = -1; /** * Current width of cool item. */ private int currentWidth = -1; /** * A flag indicating that this item has been disposed. This prevents future * method invocations from doing things they shouldn't. */ private boolean disposed = false; /** * Mininum number of tool items to show in the cool item widget. */ private int minimumItemsToShow = SHOW_ALL_ITEMS; /** * The tool bar manager used to manage the tool items contained in the cool * item widget. */ private ToolBarManager toolBarManager = null; /** * Enable/disable chevron support. */ private boolean useChevron = true; /** * Convenience method equivalent to <code>ToolBarContributionItem(new ToolBarManager(), null)</code>. */ public ToolBarContributionItem() { this(new ToolBarManager(), null); } /** * Convenience method equivalent to <code>ToolBarContributionItem(toolBarManager, null)</code>. * * @param toolBarManager * the tool bar manager */ public ToolBarContributionItem(IToolBarManager toolBarManager) { this(toolBarManager, null); } /** * Creates a tool bar contribution item. * * @param toolBarManager * the tool bar manager to wrap * @param id * the contribution item id, or <code>null</code> if none */ public ToolBarContributionItem(IToolBarManager toolBarManager, String id) { super(id); Assert.isTrue(toolBarManager instanceof ToolBarManager); this.toolBarManager = (ToolBarManager) toolBarManager; } /** * Checks whether this contribution item has been disposed. If it has, and * the tracing options are active, then it prints some debugging * information. * * @return <code>true</code> if the item is disposed; <code>false</code> * otherwise. * */ private final boolean checkDisposed() { if (disposed) { if (Policy.TRACE_TOOLBAR) { System.out .println("Method invocation on a disposed tool bar contribution item."); //$NON-NLS-1$ new Exception().printStackTrace(System.out); } return true; } return false; } /* * (non-Javadoc) * * @see org.eclipse.jface.action.IContributionItem#dispose() */ public void dispose() { // Dispose of the ToolBar and all its contributions if (toolBarManager != null) { toolBarManager.dispose(); toolBarManager = null; } /* * We need to dispose the cool item or we might be left holding a cool * item with a disposed control. */ if ((coolItem != null) && (!coolItem.isDisposed())) { coolItem.dispose(); coolItem = null; } // Mark this item as disposed. disposed = true; } /* * (non-Javadoc) * * @see org.eclipse.jface.action.IContributionItem#fill(org.eclipse.swt.widgets.CoolBar, * int) */ public void fill(CoolBar coolBar, int index) { if (checkDisposed()) { return; } if (coolItem == null && coolBar != null) { ToolBar oldToolBar = toolBarManager.getControl(); ToolBar toolBar = toolBarManager.createControl(coolBar); if ((oldToolBar != null) && (oldToolBar.equals(toolBar))) { // We are using an old tool bar, so we need to update. toolBarManager.update(true); } // Do not create a coolItem if the toolbar is empty if (toolBar.getItemCount() < 1) { return; } int flags = SWT.DROP_DOWN; if (index >= 0) { coolItem = new CoolItem(coolBar, flags, index); } else { coolItem = new CoolItem(coolBar, flags); } // sets the back reference coolItem.setData(this); // Add the toolbar to the CoolItem widget coolItem.setControl(toolBar); // Handle Context Menu // ToolBarManager.createControl can actually return a pre-existing control. // Only add the listener if the toolbar was newly created (bug 62097). if (oldToolBar != toolBar) { toolBar.addListener(SWT.MenuDetect, new Listener() { public void handleEvent(Event event) { // if the toolbar does not have its own context menu then // handle the event if (toolBarManager.getContextMenuManager() == null) { handleContextMenu(event); } } }); } // Handle for chevron clicking if (getUseChevron()) { // Chevron Support coolItem.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { if (event.detail == SWT.ARROW) { handleChevron(event); } } }); } // Handle for disposal coolItem.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent event) { handleWidgetDispose(event); } }); // Sets the size of the coolItem updateSize(true); } } /** * Returns 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. */ 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; } } return adjustedWrapIndices; } /** * Returns the current height of the corresponding cool item. * * @return the current height */ public int getCurrentHeight() { if (checkDisposed()) { return -1; } return currentHeight; } /** * Returns the current width of the corresponding cool item. * * @return the current size */ public int getCurrentWidth() { if (checkDisposed()) { return -1; } return currentWidth; } /** * Returns the minimum number of tool items to show in the cool item. * * @return the minimum number of tool items to show, or <code>SHOW_ALL_ITEMS</code> * if a value was not set * @see #setMinimumItemsToShow(int) */ public int getMinimumItemsToShow() { if (checkDisposed()) { return -1; } return minimumItemsToShow; } /** * Returns the internal tool bar manager of the contribution item. * * @return the tool bar manager, or <code>null</code> if one is not * defined. * @see IToolBarManager */ public IToolBarManager getToolBarManager() { if (checkDisposed()) { return null; } return toolBarManager; } /** * Returns whether chevron support is enabled. * * @return <code>true</code> if chevron support is enabled, <code>false</code> * otherwise */ public boolean getUseChevron() { if (checkDisposed()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -