📄 bindingmanager.java
字号:
/******************************************************************************* * Copyright (c) 2004, 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.bindings;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.Set;import java.util.StringTokenizer;import org.eclipse.core.commands.CommandManager;import org.eclipse.core.commands.ParameterizedCommand;import org.eclipse.core.commands.common.HandleObjectManager;import org.eclipse.core.commands.common.NotDefinedException;import org.eclipse.core.commands.contexts.Context;import org.eclipse.core.commands.contexts.ContextManager;import org.eclipse.core.commands.contexts.ContextManagerEvent;import org.eclipse.core.commands.contexts.IContextManagerListener;import org.eclipse.core.commands.util.Tracing;import org.eclipse.core.runtime.IStatus;import org.eclipse.core.runtime.Status;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.contexts.IContextIds;import org.eclipse.jface.util.Policy;import org.eclipse.jface.util.Util;import org.eclipse.swt.SWT;/** * <p> * A central repository for bindings -- both in the defined and undefined * states. Schemes and bindings can be created and retrieved using this manager. * It is possible to listen to changes in the collection of schemes and bindings * by adding a listener to the manager. * </p> * <p> * The binding manager is very sensitive to performance. Misusing the manager * can render an application unenjoyable to use. As such, each of the public * methods states the current run-time performance. In future releases, it is * guaranteed that the method will run in at least the stated time constraint -- * though it might get faster. Where possible, we have also tried to be memory * efficient. * </p> * * @since 3.1 */public final class BindingManager extends HandleObjectManager implements IContextManagerListener, ISchemeListener { /** * This flag can be set to <code>true</code> if the binding manager should * print information to <code>System.out</code> when certain boundary * conditions occur. */ public static boolean DEBUG = false; /** * Returned for optimized lookup. */ private static final TriggerSequence[] EMPTY_TRIGGER_SEQUENCE = new TriggerSequence[0]; /** * The separator character used in locales. */ private static final String LOCALE_SEPARATOR = "_"; //$NON-NLS-1$ /** * </p> * A utility method for adding entries to a map. The map is checked for * entries at the key. If such an entry exists, it is expected to be a * <code>Collection</code>. The value is then appended to the collection. * If no such entry exists, then a collection is created, and the value * added to the collection. * </p> * * @param map * The map to modify; if this value is <code>null</code>, then * this method simply returns. * @param key * The key to look up in the map; may be <code>null</code>. * @param value * The value to look up in the map; may be <code>null</code>. */ private static final void addReverseLookup(final Map map, final Object key, final Object value) { if (map == null) { return; } final Object currentValue = map.get(key); if (currentValue != null) { final Collection values = (Collection) currentValue; values.add(value); } else { // currentValue == null final Collection values = new ArrayList(1); values.add(value); map.put(key, values); } } /** * <p> * Takes a fully-specified string, and converts it into an array of * increasingly less-specific strings. So, for example, "en_GB" would become * ["en_GB", "en", "", null]. * </p> * <p> * This method runs in linear time (O(n)) over the length of the string. * </p> * * @param string * The string to break apart into its less specific components; * should not be <code>null</code>. * @param separator * The separator that indicates a separation between a degrees of * specificity; should not be <code>null</code>. * @return An array of strings from the most specific (i.e., * <code>string</code>) to the least specific (i.e., * <code>null</code>). */ private static final String[] expand(String string, final String separator) { // Test for boundary conditions. if (string == null || separator == null) { return new String[0]; } final List strings = new ArrayList(); final StringBuffer stringBuffer = new StringBuffer(); string = string.trim(); // remove whitespace if (string.length() > 0) { final StringTokenizer stringTokenizer = new StringTokenizer(string, separator); while (stringTokenizer.hasMoreElements()) { if (stringBuffer.length() > 0) { stringBuffer.append(separator); } stringBuffer.append(((String) stringTokenizer.nextElement()) .trim()); strings.add(stringBuffer.toString()); } } Collections.reverse(strings); strings.add(Util.ZERO_LENGTH_STRING); strings.add(null); return (String[]) strings.toArray(new String[strings.size()]); } /** * The active bindings. This is a map of triggers ( * <code>TriggerSequence</code>) to bindings (<code>Binding</code>). * This value will only be <code>null</code> if the active bindings have * not yet been computed. Otherwise, this value may be empty. */ private Map activeBindings = null; /** * The active bindings indexed by fully-parameterized commands. This is a * map of fully-parameterized commands (<code>ParameterizedCommand</code>) * to triggers ( <code>TriggerSequence</code>). This value will only be * <code>null</code> if the active bindings have not yet been computed. * Otherwise, this value may be empty. */ private Map activeBindingsByParameterizedCommand = null; /** * The scheme that is currently active. An active scheme is the one that is * currently dictating which bindings will actually work. This value may be * <code>null</code> if there is no active scheme. If the active scheme * becomes undefined, then this should automatically revert to * <code>null</code>. */ private Scheme activeScheme = null; /** * The array of scheme identifiers, starting with the active scheme and * moving up through its parents. This value may be <code>null</code> if * there is no active scheme. */ private String[] activeSchemeIds = null; /** * The number of bindings in the <code>bindings</code> array. */ private int bindingCount = 0; /** * A cache of context IDs that weren't defined. */ private Set bindingErrors = new HashSet(); /** * The array of all bindings currently handled by this manager. This array * is the raw list of bindings, as provided to this manager. This value may * be <code>null</code> if there are no bindings. The size of this array * is not necessarily the number of bindings. */ private Binding[] bindings = null; /** * A cache of the bindings previously computed by this manager. This value * may be empty, but it is never <code>null</code>. This is a map of * <code>CachedBindingSet</code> to <code>CachedBindingSet</code>. */ private Map cachedBindings = new HashMap(); /** * The command manager for this binding manager. This manager is only needed * for the <code>getActiveBindingsFor(String)</code> method. This value is * guaranteed to never be <code>null</code>. */ private final CommandManager commandManager; /** * The context manager for this binding manager. For a binding manager to * function, it needs to listen for changes to the contexts. This value is * guaranteed to never be <code>null</code>. */ private final ContextManager contextManager; /** * The locale for this manager. This defaults to the current locale. The * value will never be <code>null</code>. */ private String locale = Locale.getDefault().toString(); /** * The array of locales, starting with the active locale and moving up * through less specific representations of the locale. For example, * ["en_US", "en", "", null]. This value will never be <code>null</code>. */ private String[] locales = expand(locale, LOCALE_SEPARATOR); /** * The platform for this manager. This defaults to the current platform. The * value will never be <code>null</code>. */ private String platform = SWT.getPlatform(); /** * The array of platforms, starting with the active platform and moving up * through less specific representations of the platform. For example, * ["gtk", "", null]. This value will never be <code>null,/code>. */ private String[] platforms = expand(platform, Util.ZERO_LENGTH_STRING); /** * A map of prefixes (<code>TriggerSequence</code>) to a map of * available completions (possibly <code>null</code>, which means there * is an exact match). The available completions is a map of trigger (<code>TriggerSequence</code>) * to bindings (<code>Binding</code>). This value may be * <code>null</code> if there is no existing solution. */ private Map prefixTable = null; /** * <p> * Constructs a new instance of <code>BindingManager</code>. * </p> * <p> * This method completes in amortized constant time (O(1)). * </p> * * @param contextManager * The context manager that will support this binding manager. * This value must not be <code>null</code>. * @param commandManager * The command manager that will support this binding manager. * This value must not be <code>null</code>. */ public BindingManager(final ContextManager contextManager, final CommandManager commandManager) { if (contextManager == null) { throw new NullPointerException( "A binding manager requires a context manager"); //$NON-NLS-1$ } if (commandManager == null) { throw new NullPointerException( "A binding manager requires a command manager"); //$NON-NLS-1$ } this.contextManager = contextManager; contextManager.addContextManagerListener(this); this.commandManager = commandManager; } /** * <p> * Adds a single new binding to the existing array of bindings. If the array * is currently <code>null</code>, then a new array is created and this * binding is added to it. This method does not detect duplicates. * </p> * <p> * This method completes in amortized <code>O(1)</code>. * </p> * * @param binding * The binding to be added; must not be <code>null</code>. */ public final void addBinding(final Binding binding) { if (binding == null) { throw new NullPointerException("Cannot add a null binding"); //$NON-NLS-1$ } if (bindings == null) { bindings = new Binding[1]; } else if (bindingCount >= bindings.length) { final Binding[] oldBindings = bindings; bindings = new Binding[oldBindings.length * 2]; System.arraycopy(oldBindings, 0, bindings, 0, oldBindings.length); } bindings[bindingCount++] = binding; clearCache(); } /** * <p> * Adds a listener to this binding manager. The listener will be notified * when the set of defined schemes or bindings changes. This can be used to * track the global appearance and disappearance of bindings. * </p> * <p> * This method completes in amortized constant time (<code>O(1)</code>). * </p> * * @param listener * The listener to attach; must not be <code>null</code>. */ public final void addBindingManagerListener( final IBindingManagerListener listener) { addListenerObject(listener); } /** * <p> * Builds a prefix table look-up for a map of active bindings. * </p> * <p> * This method takes <code>O(mn)</code>, where <code>m</code> is the * length of the trigger sequences and <code>n</code> is the number of * bindings. * </p> * * @param activeBindings * The map of triggers (<code>TriggerSequence</code>) to * command ids (<code>String</code>) which are currently * active. This value may be <code>null</code> if there are no * active bindings, and it may be empty. It must not be * <code>null</code>. * @return A map of prefixes (<code>TriggerSequence</code>) to a map of * available completions (possibly <code>null</code>, which means * there is an exact match). The available completions is a map of * trigger (<code>TriggerSequence</code>) to command identifier (<code>String</code>). * This value will never be <code>null</code>, but may be empty. */ private final Map buildPrefixTable(final Map activeBindings) { final Map prefixTable = new HashMap(); final Iterator bindingItr = activeBindings.entrySet().iterator(); while (bindingItr.hasNext()) { final Map.Entry entry = (Map.Entry) bindingItr.next(); final TriggerSequence triggerSequence = (TriggerSequence) entry .getKey(); // Add the perfect match. if (!prefixTable.containsKey(triggerSequence)) { prefixTable.put(triggerSequence, null); } final TriggerSequence[] prefixes = triggerSequence.getPrefixes(); final int prefixesLength = prefixes.length; if (prefixesLength == 0) { continue; } // Break apart the trigger sequence. final Binding binding = (Binding) entry.getValue(); for (int i = 0; i < prefixesLength; i++) { final TriggerSequence prefix = prefixes[i]; final Object value = prefixTable.get(prefix); if ((prefixTable.containsKey(prefix)) && (value instanceof Map)) { ((Map) value).put(triggerSequence, binding); } else { final Map map = new HashMap(); prefixTable.put(prefix, map);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -