|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun * Microsystems, Inc. All Rights Reserved. * * Contributors: Maxym Mykhalchuk */ package org.openide.awt; import java.util.MissingResourceException; import java.util.ResourceBundle; import javax.swing.AbstractButton; import javax.swing.JLabel; /** * Support class for setting button, menu, and label text strings with mnemonics. * @author Maxym Mykhalchuk * @since 3.37 * @see Issue #26640 */ public final class Mnemonics extends Object { /** Private constructor in order that this class is never instantiated. */ private Mnemonics() {} /** * Actual setter of the text & mnemonics for the AbstractButton/JLabel or * their subclasses. * @param item AbstractButton/JLabel * @param text new label */ private static void setLocalizedText2(Object item, String text) { // #17664. Handle null text also. if(text == null) { setText(item, null); return; } int i = findMnemonicAmpersand(text); if (i < 0) { // no '&' - don't set the mnemonic setText(item, text); setMnemonic(item, 0); } else { setText(item, text.substring(0, i) + text.substring(i + 1)); char ch = text.charAt(i + 1); if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) { // it's latin character or arabic digit, // setting it as mnemonics setMnemonic(item, ch); // If it's something like "Save &As", we need to set another // mnemonic index (at least under 1.4 or later) // see #29676 setMnemonicIndex(item, i); } else { // it's non-latin, getting the latin correspondance int latinCode = getLatinKeycode(ch); setMnemonic(item, latinCode); setMnemonicIndex(item, i); } } } /** * Sets the text for a menu item or other subclass of AbstractButton. *
MNEMONIC_\u0424=A .
* @param localeChar non-Latin character or a punctuator to be used as mnemonic
* @return character on latin keyboard, corresponding to the locale character,
* or the appropriate VK_*** code (if there's no latin character
* "under" the non-Latin one
*/
private static int getLatinKeycode(char localeChar) {
try {
// associated should be a latin character, arabic digit
// or an integer (KeyEvent.VK_***)
String str=getBundle().getString("MNEMONIC_" + localeChar); // NOI18N
if( str.length()==1 )
return str.charAt(0);
else
return Integer.parseInt(str);
} catch (MissingResourceException x) {
// correspondence not found, it IS an error,
// but we eat it, and return the character itself
x.printStackTrace();
return localeChar;
}
}
/**
* Wrapper for the
* AbstractButton.setMnemonicIndex or
* JLabel.setDisplayedMnemonicIndex method.
* |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.