📄 actioncontributionitem.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 org.eclipse.jface.action.ExternalActionManager.IBindingManagerCallback;import org.eclipse.jface.bindings.Trigger;import org.eclipse.jface.bindings.TriggerSequence;import org.eclipse.jface.bindings.keys.IKeyLookup;import org.eclipse.jface.bindings.keys.KeyLookupFactory;import org.eclipse.jface.bindings.keys.KeyStroke;import org.eclipse.jface.resource.ImageDescriptor;import org.eclipse.jface.resource.JFaceResources;import org.eclipse.jface.resource.LocalResourceManager;import org.eclipse.jface.resource.ResourceManager;import org.eclipse.jface.util.IPropertyChangeListener;import org.eclipse.jface.util.Policy;import org.eclipse.jface.util.PropertyChangeEvent;import org.eclipse.swt.SWT;import org.eclipse.swt.graphics.GC;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Item;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Menu;import org.eclipse.swt.widgets.MenuItem;import org.eclipse.swt.widgets.ToolBar;import org.eclipse.swt.widgets.ToolItem;import org.eclipse.swt.widgets.Widget;/** * A contribution item which delegates to an action. * <p> * This class may be instantiated; it is not intended to be subclassed. * </p> */public class ActionContributionItem extends ContributionItem { /** * Mode bit: Show text on tool items, even if an image is present. * If this mode bit is not set, text is only shown on tool items if there is * no image present. * * @since 3.0 */ public static int MODE_FORCE_TEXT = 1; /** a string inserted in the middle of text that has been shortened */ private static final String ellipsis = "..."; //$NON-NLS-1$ private static boolean USE_COLOR_ICONS = true; /** * Returns whether color icons should be used in toolbars. * * @return <code>true</code> if color icons should be used in toolbars, * <code>false</code> otherwise */ public static boolean getUseColorIconsInToolbars() { return USE_COLOR_ICONS; } /** * Sets whether color icons should be used in toolbars. * * @param useColorIcons <code>true</code> if color icons should be used in toolbars, * <code>false</code> otherwise */ public static void setUseColorIconsInToolbars(boolean useColorIcons) { USE_COLOR_ICONS = useColorIcons; } /** * The presentation mode. */ private int mode = 0; /** * The action. */ private IAction action; /** * The listener for changes to the text of the action contributed by an * external source. */ private final IPropertyChangeListener actionTextListener = new IPropertyChangeListener() { /** * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent) */ public void propertyChange(PropertyChangeEvent event) { update(event.getProperty()); } }; /** * Remembers all images in use by this contribution item */ private LocalResourceManager imageManager; /** * Listener for SWT button widget events. */ private Listener buttonListener; /** * Listener for SWT menu item widget events. */ private Listener menuItemListener; /** * Listener for action property change notifications. */ private final IPropertyChangeListener propertyListener = new IPropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { actionPropertyChange(event); } }; /** * Listener for SWT tool item widget events. */ private Listener toolItemListener; /** * The widget created for this item; <code>null</code> * before creation and after disposal. */ private Widget widget = null; /** * Creates a new contribution item from the given action. The id of the * action is used as the id of the item. * * @param action * the action */ public ActionContributionItem(IAction action) { super(action.getId()); this.action = action; } /** * Handles a property change event on the action (forwarded by nested listener). */ private void actionPropertyChange(final PropertyChangeEvent e) { // This code should be removed. Avoid using free asyncExec if (isVisible() && widget != null) { Display display = widget.getDisplay(); if (display.getThread() == Thread.currentThread()) { update(e.getProperty()); } else { display.asyncExec(new Runnable() { public void run() { update(e.getProperty()); } }); } } } /** * Compares this action contribution item with another object. * Two action contribution items are equal if they refer to the identical Action. */ public boolean equals(Object o) { if (!(o instanceof ActionContributionItem)) { return false; } return action.equals(((ActionContributionItem) o).action); } /** * The <code>ActionContributionItem</code> implementation of this * <code>IContributionItem</code> method creates an SWT <code>Button</code> for * the action using the action's style. If the action's checked property has * been set, the button is created and primed to the value of the checked * property. */ public void fill(Composite parent) { if (widget == null && parent != null) { int flags = SWT.PUSH; if (action != null) { if (action.getStyle() == IAction.AS_CHECK_BOX) { flags = SWT.TOGGLE; } if (action.getStyle() == IAction.AS_RADIO_BUTTON) { flags = SWT.RADIO; } } Button b = new Button(parent, flags); b.setData(this); b.addListener(SWT.Dispose, getButtonListener()); // Don't hook a dispose listener on the parent b.addListener(SWT.Selection, getButtonListener()); if (action.getHelpListener() != null) { b.addHelpListener(action.getHelpListener()); } widget = b; update(null); // Attach some extra listeners. action.addPropertyChangeListener(propertyListener); if (action != null) { String commandId = action.getActionDefinitionId(); ExternalActionManager.ICallback callback = ExternalActionManager .getInstance().getCallback(); if ((callback != null) && (commandId != null)) { callback.addPropertyChangeListener(commandId, actionTextListener); } } } } /** * The <code>ActionContributionItem</code> implementation of this * <code>IContributionItem</code> method creates an SWT <code>MenuItem</code> * for the action using the action's style. If the action's checked property has * been set, a button is created and primed to the value of the checked * property. If the action's menu creator property has been set, a cascading * submenu is created. */ public void fill(Menu parent, int index) { if (widget == null && parent != null) { Menu subMenu = null; int flags = SWT.PUSH; if (action != null) { int style = action.getStyle(); if (style == IAction.AS_CHECK_BOX) { flags = SWT.CHECK; } else if (style == IAction.AS_RADIO_BUTTON) { flags = SWT.RADIO; } else if (style == IAction.AS_DROP_DOWN_MENU) { IMenuCreator mc = action.getMenuCreator(); if (mc != null) { subMenu = mc.getMenu(parent); flags = SWT.CASCADE; } } } MenuItem mi = null; if (index >= 0) { mi = new MenuItem(parent, flags, index); } else { mi = new MenuItem(parent, flags); } widget = mi; mi.setData(this); mi.addListener(SWT.Dispose, getMenuItemListener()); mi.addListener(SWT.Selection, getMenuItemListener()); if (action.getHelpListener() != null) { mi.addHelpListener(action.getHelpListener()); } if (subMenu != null) { mi.setMenu(subMenu); } update(null); // Attach some extra listeners. action.addPropertyChangeListener(propertyListener); if (action != null) { String commandId = action.getActionDefinitionId(); ExternalActionManager.ICallback callback = ExternalActionManager .getInstance().getCallback(); if ((callback != null) && (commandId != null)) { callback.addPropertyChangeListener(commandId, actionTextListener); } } } } /** * The <code>ActionContributionItem</code> implementation of this , * <code>IContributionItem</code> method creates an SWT <code>ToolItem</code> * for the action using the action's style. If the action's checked property has * been set, a button is created and primed to the value of the checked * property. If the action's menu creator property has been set, a drop-down * tool item is created. */ public void fill(ToolBar parent, int index) { if (widget == null && parent != null) { int flags = SWT.PUSH; if (action != null) { int style = action.getStyle(); if (style == IAction.AS_CHECK_BOX) { flags = SWT.CHECK; } else if (style == IAction.AS_RADIO_BUTTON) { flags = SWT.RADIO; } else if (style == IAction.AS_DROP_DOWN_MENU) { flags = SWT.DROP_DOWN; } } ToolItem ti = null; if (index >= 0) { ti = new ToolItem(parent, flags, index); } else { ti = new ToolItem(parent, flags); } ti.setData(this); ti.addListener(SWT.Selection, getToolItemListener()); ti.addListener(SWT.Dispose, getToolItemListener()); widget = ti; update(null); // Attach some extra listeners. action.addPropertyChangeListener(propertyListener); if (action != null) { String commandId = action.getActionDefinitionId(); ExternalActionManager.ICallback callback = ExternalActionManager .getInstance().getCallback(); if ((callback != null) && (commandId != null)) { callback.addPropertyChangeListener(commandId, actionTextListener); } } } } /** * Returns the action associated with this contribution item. * * @return the action */ public IAction getAction() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -