⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 legacyactiontools.java

📁 jfa2ce 源码帮助开发人员更好的理解运用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************* * Copyright (c) 2005, 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.HashMap;import java.util.Map;import java.util.StringTokenizer;import org.eclipse.jface.resource.JFaceResources;import org.eclipse.swt.SWT;/** * <p> * Some static utility methods for handling labels on actions. This includes * mnemonics and accelerators. * </p> * <p> * Clients may neither instantiate this class nor extend. * </p> *  * @since 3.2 */public final class LegacyActionTools {	/**	 * Table of key codes (key type: <code>String</code>, value type:	 * <code>Integer</code>); <code>null</code> if not yet initialized.	 * 	 * @see #findKeyCode	 */	private static Map keyCodes = null;	/**	 * Table of string representations of keys (key type: <code>Integer</code>,	 * value type: <code>String</code>); <code>null</code>> if not yet	 * initialized.	 * 	 * @see #findKeyString	 */	private static Map keyStrings = null;	/**	 * The localized uppercase version of ALT	 */	private static String localizedAlt;	/**	 * The localized uppercase version of COMMAND	 */	private static String localizedCommand;	/**	 * The localized uppercase version of CTRL	 */	private static String localizedCtrl;	/**	 * Table of key codes (key type: <code>String</code>, value type:	 * <code>Integer</code>); <code>null</code> if not yet initialized. The	 * key is the localalized name of the key as it appears in menus.	 * 	 * @see #findLocalizedKeyCode	 */	private static Map localizedKeyCodes = null;	/**	 * The localized uppercase version of SHIFT	 */	private static String localizedShift;	/**	 * The constant to use if there is no mnemonic for this location.	 */	public static final char MNEMONIC_NONE = 0;	/**	 * Converts an accelerator key code to a string representation.	 * 	 * @param keyCode	 *            the key code to be translated	 * @return a string representation of the key code	 */	public static final String convertAccelerator(final int keyCode) {		String modifier = getModifierString(keyCode);		String fullKey;		if (modifier.equals("")) { //$NON-NLS-1$			fullKey = findKeyString(keyCode);		} else {			fullKey = modifier + "+" + findKeyString(keyCode); //$NON-NLS-1$		}		return fullKey;	}	/**	 * Parses the given accelerator text, and converts it to an accelerator key	 * code.	 * 	 * @param acceleratorText	 *            the accelerator text	 * @return the SWT key code, or 0 if there is no accelerator	 */	public static final int convertAccelerator(final String acceleratorText) {		int accelerator = 0;		StringTokenizer stok = new StringTokenizer(acceleratorText, "+"); //$NON-NLS-1$		int keyCode = -1;		boolean hasMoreTokens = stok.hasMoreTokens();		while (hasMoreTokens) {			String token = stok.nextToken();			hasMoreTokens = stok.hasMoreTokens();			// Every token except the last must be one of the modifiers			// Ctrl, Shift, Alt, or Command			if (hasMoreTokens) {				int modifier = findModifier(token);				if (modifier != 0) {					accelerator |= modifier;				} else { // Leave if there are none					return 0;				}			} else {				keyCode = findKeyCode(token);			}		}		if (keyCode != -1) {			accelerator |= keyCode;		}		return accelerator;	}	/**	 * Parses the given accelerator text, and converts it to an accelerator key	 * code.	 * 	 * Support for localized modifiers is for backwards compatibility with 1.0.	 * Use setAccelerator(int) to set accelerators programatically or the	 * <code>accelerator</code> tag in action definitions in plugin.xml.	 * 	 * @param acceleratorText	 *            the accelerator text localized to the current locale	 * @return the SWT key code, or 0 if there is no accelerator	 */	static final int convertLocalizedAccelerator(final String acceleratorText) {		int accelerator = 0;		StringTokenizer stok = new StringTokenizer(acceleratorText, "+"); //$NON-NLS-1$		int keyCode = -1;		boolean hasMoreTokens = stok.hasMoreTokens();		while (hasMoreTokens) {			String token = stok.nextToken();			hasMoreTokens = stok.hasMoreTokens();			// Every token except the last must be one of the modifiers			// Ctrl, Shift, Alt, or Command			if (hasMoreTokens) {				int modifier = findLocalizedModifier(token);				if (modifier != 0) {					accelerator |= modifier;				} else { // Leave if there are none					return 0;				}			} else {				keyCode = findLocalizedKeyCode(token);			}		}		if (keyCode != -1) {			accelerator |= keyCode;		}		return accelerator;	}	/**	 * Extracts the accelerator text from the given text. Returns	 * <code>null</code> if there is no accelerator text, and the empty string	 * if there is no text after the accelerator delimeter (tab or '@').	 * 	 * @param text	 *            the text for the action; may be <code>null</code>.	 * @return the accelerator text, or <code>null</code>	 */	public static final String extractAcceleratorText(final String text) {		if (text == null) {			return null;		}		int index = text.lastIndexOf('\t');		if (index == -1) {			index = text.lastIndexOf('@');		}		if (index >= 0) {			return text.substring(index + 1);		}		return null;	}	/**	 * Extracts the mnemonic text from the given string.	 * 	 * @param text	 *            The text from which the mnemonic should be extracted; may be	 *            <code>null</code>	 * @return The text of the mnemonic; will be {@link #MNEMONIC_NONE} if there	 *         is no mnemonic;	 */	public static final char extractMnemonic(final String text) {		if (text == null) {			return MNEMONIC_NONE;		}		int index = text.indexOf('&');		if (index == -1) {			return MNEMONIC_NONE;		}		final int textLength = text.length();		// Ignore '&' at the end of the string.		if (index == textLength - 1) {			return MNEMONIC_NONE;		}		// Ignore two consecutive ampersands.		while (text.charAt(index + 1) == '&') {			index = text.indexOf('&', ++index);			if (index == textLength - 1) {				return MNEMONIC_NONE;			}		}		return text.charAt(index + 1);	}	/**	 * Maps a standard keyboard key name to an SWT key code. Key names are	 * converted to upper case before comparison. If the key name is a single	 * letter, for example "S", its character code is returned.	 * <p>	 * The following key names are known (case is ignored):	 * <ul>	 * <li><code>"BACKSPACE"</code></li>	 * <li><code>"TAB"</code></li>	 * <li><code>"RETURN"</code></li>	 * <li><code>"ENTER"</code></li>	 * <li><code>"ESC"</code></li>	 * <li><code>"ESCAPE"</code></li>	 * <li><code>"DELETE"</code></li>	 * <li><code>"SPACE"</code></li>	 * <li><code>"ARROW_UP"</code>, <code>"ARROW_DOWN"</code>,	 * <code>"ARROW_LEFT"</code>, and <code>"ARROW_RIGHT"</code></li>	 * <li><code>"PAGE_UP"</code> and <code>"PAGE_DOWN"</code></li>	 * <li><code>"HOME"</code></li>	 * <li><code>"END"</code></li>	 * <li><code>"INSERT"</code></li>	 * <li><code>"F1"</code>, <code>"F2"</code> through <code>"F12"</code></li>	 * </ul>	 * </p>	 * 	 * @param token	 *            the key name	 * @return the SWT key code, <code>-1</code> if no match was found	 * @see SWT	 */	public static final int findKeyCode(String token) {		if (keyCodes == null) {			initKeyCodes();		}		token = token.toUpperCase();		Integer i = (Integer) keyCodes.get(token);		if (i != null) {			return i.intValue();		}		if (token.length() == 1) {			return token.charAt(0);		}		return -1;	}	/**	 * Maps an SWT key code to a standard keyboard key name. The key code is	 * stripped of modifiers (SWT.CTRL, SWT.ALT, SWT.SHIFT, and SWT.COMMAND). If	 * the key code is not an SWT code (for example if it a key code for the key	 * 'S'), a string containing a character representation of the key code is	 * returned.	 * 	 * @param keyCode	 *            the key code to be translated	 * @return the string representation of the key code	 * @see SWT	 * @since 2.0	 */	public static final String findKeyString(final int keyCode) {		if (keyStrings == null) {			initKeyStrings();		}		int i = keyCode & ~(SWT.CTRL | SWT.ALT | SWT.SHIFT | SWT.COMMAND);		Integer integer = new Integer(i);		String result = (String) keyStrings.get(integer);		if (result != null) {			return result;		}		result = new String(new char[] { (char) i });		return result;	}	/**	 * Find the supplied code for a localized key. As #findKeyCode but localized	 * to the current locale.	 * 	 * Support for localized modifiers is for backwards compatibility with 1.0.	 * Use setAccelerator(int) to set accelerators programatically or the	 * <code>accelerator</code> tag in action definitions in plugin.xml.	 * 	 * @param token	 *            the localized key name	 * @return the SWT key code, <code>-1</code> if no match was found	 * @see #findKeyCode	 */	private static final int findLocalizedKeyCode(String token) {		if (localizedKeyCodes == null) {			initLocalizedKeyCodes();		}		token = token.toUpperCase();		Integer i = (Integer) localizedKeyCodes.get(token);		if (i != null) {			return i.intValue();		}		if (token.length() == 1) {			return token.charAt(0);		}		return -1;	}	/**	 * Maps the localized modifier names to a code in the same manner as	 * #findModifier.	 * 	 * Support for localized modifiers is for backwards compatibility with 1.0.	 * Use setAccelerator(int) to set accelerators programatically or the	 * <code>accelerator</code> tag in action definitions in plugin.xml.	 * 	 * @see #findModifier	 */	private static final int findLocalizedModifier(String token) {		if (localizedCtrl == null) {			initLocalizedModifiers();		}		token = token.toUpperCase();		if (token.equals(localizedCtrl)) {			return SWT.CTRL;		}		if (token.equals(localizedShift)) {			return SWT.SHIFT;		}		if (token.equals(localizedAlt)) {			return SWT.ALT;		}		if (token.equals(localizedCommand)) {			return SWT.COMMAND;		}		return 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -