📄 externalactionmanager.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.text.MessageFormat; // Not using ICU to support standalone JFace scenarioimport java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.ResourceBundle;import java.util.Set;import org.eclipse.core.commands.Command;import org.eclipse.core.commands.CommandEvent;import org.eclipse.core.commands.CommandManager;import org.eclipse.core.commands.ICommandListener;import org.eclipse.core.commands.ParameterizedCommand;import org.eclipse.core.runtime.IStatus;import org.eclipse.core.runtime.Status;import org.eclipse.jface.bindings.BindingManager;import org.eclipse.jface.bindings.BindingManagerEvent;import org.eclipse.jface.bindings.IBindingManagerListener;import org.eclipse.jface.bindings.Trigger;import org.eclipse.jface.bindings.TriggerSequence;import org.eclipse.jface.bindings.keys.KeySequence;import org.eclipse.jface.bindings.keys.KeyStroke;import org.eclipse.jface.bindings.keys.SWTKeySupport;import org.eclipse.jface.util.IPropertyChangeListener;import org.eclipse.jface.util.Policy;import org.eclipse.jface.util.PropertyChangeEvent;import org.eclipse.jface.util.Util;/** * <p> * A manager for a callback facility which is capable of querying external * interfaces for additional information about actions and action contribution * items. This information typically includes things like accelerators and * textual representations. * </p> * <p> * <em>It is only necessary to use this mechanism if you will be using a mix of * actions and commands, and wish the interactions to work properly.</em> * </p> * <p> * For example, in the Eclipse workbench, this mechanism is used to allow the * command architecture to override certain values in action contribution items. * </p> * <p> * This class is not intended to be called or extended by any external clients. * </p> * * @since 3.0 */public final class ExternalActionManager { /** * A simple implementation of the <code>ICallback</code> mechanism that * simply takes a <code>BindingManager</code> and a * <code>CommandManager</code>. * * @since 3.1 */ public static final class CommandCallback implements IBindingManagerListener, IBindingManagerCallback { /** * The internationalization bundle for text produced by this class. */ private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle .getBundle(ExternalActionManager.class.getName()); /** * The callback capable of responding to whether a command is active. */ private final IActiveChecker activeChecker; /** * The binding manager for your application. Must not be * <code>null</code>. */ private final BindingManager bindingManager; /** * Whether a listener has been attached to the binding manager yet. */ private boolean bindingManagerListenerAttached = false; /** * The command manager for your application. Must not be * <code>null</code>. */ private final CommandManager commandManager; /** * A set of all the command identifiers that have been logged as broken * so far. For each of these, there will be a listener on the * corresponding command. If the command ever becomes defined, the item * will be removed from this set and the listener removed. This value * may be empty, but never <code>null</code>. */ private final Set loggedCommandIds = new HashSet(); /** * The list of listeners that have registered for property change * notification. This is a map of command identifiers (<code>String</code>) * to listeners (<code>IPropertyChangeListener</code>). */ private final Map registeredListeners = new HashMap(); /** * Constructs a new instance of <code>CommandCallback</code> with the * workbench it should be using. All commands will be considered active. * * @param bindingManager * The binding manager which will provide the callback; must * not be <code>null</code>. * @param commandManager * The command manager which will provide the callback; must * not be <code>null</code>. * * @since 3.1 */ public CommandCallback(final BindingManager bindingManager, final CommandManager commandManager) { this(bindingManager, commandManager, new IActiveChecker() { public boolean isActive(String commandId) { return true; } }); } /** * Constructs a new instance of <code>CommandCallback</code> with the * workbench it should be using. * * @param bindingManager * The binding manager which will provide the callback; must * not be <code>null</code>. * @param commandManager * The command manager which will provide the callback; must * not be <code>null</code>. * @param activeChecker * The callback mechanism for checking whether a command is * active; must not be <code>null</code>. * * @since 3.1 */ public CommandCallback(final BindingManager bindingManager, final CommandManager commandManager, final IActiveChecker activeChecker) { if (bindingManager == null) { throw new NullPointerException( "The callback needs a binding manager"); //$NON-NLS-1$ } if (commandManager == null) { throw new NullPointerException( "The callback needs a command manager"); //$NON-NLS-1$ } if (activeChecker == null) { throw new NullPointerException( "The callback needs an active callback"); //$NON-NLS-1$ } this.activeChecker = activeChecker; this.bindingManager = bindingManager; this.commandManager = commandManager; } /** * @see org.eclipse.jface.action.ExternalActionManager.ICallback#addPropertyChangeListener(String, * IPropertyChangeListener) */ public final void addPropertyChangeListener(final String commandId, final IPropertyChangeListener listener) { registeredListeners.put(commandId, listener); if (!bindingManagerListenerAttached) { bindingManager.addBindingManagerListener(this); bindingManagerListenerAttached = true; } } public final void bindingManagerChanged(final BindingManagerEvent event) { if (event.isActiveBindingsChanged()) { final Iterator listenerItr = registeredListeners.entrySet() .iterator(); while (listenerItr.hasNext()) { final Map.Entry entry = (Map.Entry) listenerItr.next(); final String commandId = (String) entry.getKey(); final Command command = commandManager .getCommand(commandId); final ParameterizedCommand parameterizedCommand = new ParameterizedCommand( command, null); if (event.isActiveBindingsChangedFor(parameterizedCommand)) { final IPropertyChangeListener listener = (IPropertyChangeListener) entry .getValue(); listener.propertyChange(new PropertyChangeEvent(event .getManager(), IAction.TEXT, null, null)); } } } } /** * @see org.eclipse.jface.action.ExternalActionManager.ICallback#getAccelerator(String) */ public final Integer getAccelerator(final String commandId) { final TriggerSequence triggerSequence = bindingManager .getBestActiveBindingFor(commandId); if (triggerSequence != null) { final Trigger[] triggers = triggerSequence.getTriggers(); if (triggers.length == 1) { final Trigger trigger = triggers[0]; if (trigger instanceof KeyStroke) { final KeyStroke keyStroke = (KeyStroke) trigger; final int accelerator = SWTKeySupport .convertKeyStrokeToAccelerator(keyStroke); return new Integer(accelerator); } } } return null; } /** * @see org.eclipse.jface.action.ExternalActionManager.ICallback#getAcceleratorText(String) */ public final String getAcceleratorText(final String commandId) { final TriggerSequence triggerSequence = bindingManager .getBestActiveBindingFor(commandId); if (triggerSequence == null) { return null; } return triggerSequence.format(); } /** * Returns the active bindings for a particular command identifier. * * @param commandId * The identifier of the command whose bindings are * requested. This argument may be <code>null</code>. It * is assumed that the command has no parameters. * @return The array of active triggers (<code>TriggerSequence</code>) * for a particular command identifier. This value is guaranteed * not to be <code>null</code>, but it may be empty. * @since 3.2 */ public final TriggerSequence[] getActiveBindingsFor( final String commandId) { return bindingManager.getActiveBindingsFor(commandId); } /** * @see org.eclipse.jface.action.ExternalActionManager.ICallback#isAcceleratorInUse(int) */ public final boolean isAcceleratorInUse(final int accelerator) { final KeySequence keySequence = KeySequence .getInstance(SWTKeySupport .convertAcceleratorToKeyStroke(accelerator)); return bindingManager.isPerfectMatch(keySequence) || bindingManager.isPartialMatch(keySequence); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -