1 21 package org.openide.awt; 22 23 import org.openide.util.Utilities; 24 import java.util.MissingResourceException ; 25 import java.util.ResourceBundle ; 26 import java.util.logging.Logger ; 27 import javax.swing.AbstractButton ; 28 import javax.swing.JLabel ; 29 30 31 37 public final class Mnemonics extends Object { 38 39 private Mnemonics() { 40 } 41 42 48 private static void setLocalizedText2(Object item, String text) { 49 if (text == null) { setText(item, null); 53 54 return; 55 } 56 57 int i = findMnemonicAmpersand(text); 58 59 if (i < 0) { 60 setText(item, text); 62 setMnemonic(item, 0); 63 } else { 64 setText(item, text.substring(0, i) + text.substring(i + 1)); 65 if (Utilities.isMac()) { 67 setMnemonic(item, 0); 68 } else { 69 char ch = text.charAt(i + 1); 70 if (text.startsWith("<html>")) { setText(item, text.substring(0, i) + "<u>" + ch + "</u>" + text.substring(i + 2)); i += 3; } 75 if (((ch >= 'A') && (ch <= 'Z')) || ((ch >= 'a') && (ch <= 'z')) || ((ch >= '0') && (ch <= '9'))) { 76 setMnemonic(item, ch); 79 80 setMnemonicIndex(item, i); 84 } else { 85 try { 87 int latinCode = getLatinKeycode(ch); 88 setMnemonic(item, latinCode); 89 setMnemonicIndex(item, i); 90 } catch (MissingResourceException e) { 91 Logger.getLogger(Mnemonics.class.getName()).info("Mapping from a non-Latin character '"+ch+ 92 "' not found in a localized (branded) version of "+ 93 "openide/awt/src/org/openide/awt/Mnemonics.properties - "+ 94 "mnemonic cannot be assigned in "+text); 95 } 96 } 97 } 98 } 99 } 100 101 114 public static void setLocalizedText(AbstractButton item, String text) { 115 setLocalizedText2(item, text); 116 } 117 118 124 public static void setLocalizedText(JLabel item, String text) { 125 setLocalizedText2(item, text); 126 } 127 128 145 public static int findMnemonicAmpersand(String text) { 146 int i = -1; 147 boolean isHTML = text.startsWith("<html>"); 148 149 do { 150 i = text.indexOf('&', i + 1); 152 153 if ((i >= 0) && ((i + 1) < text.length())) { 154 if (isHTML) { 155 boolean startsEntity = false; 156 for (int j = i + 1; j < text.length(); j++) { 157 char c = text.charAt(j); 158 if (c == ';') { 159 startsEntity = true; 160 break; 161 } 162 if (!Character.isLetterOrDigit(c)) { 163 break; 164 } 165 } 166 if (!startsEntity) { 167 return i; 168 } 169 } 170 else { 171 if (text.charAt(i + 1) == ' ') { 173 continue; 174 175 } else if ((text.charAt(i + 1) == '\'') && (i > 0) && (text.charAt(i - 1) == '\'')) { 177 continue; 178 } 179 180 return i; 182 } 183 } 184 } while (i >= 0); 185 186 return -1; 187 } 188 189 199 private static int getLatinKeycode(char localeChar) throws MissingResourceException { 200 String str = getBundle().getString("MNEMONIC_" + localeChar); 204 if (str.length() == 1) { 205 return str.charAt(0); 206 } else { 207 return Integer.parseInt(str); 208 } 209 } 210 211 219 private static void setMnemonicIndex(Object item, int index) { 220 if (item instanceof AbstractButton ) { 221 ((AbstractButton ) item).setDisplayedMnemonicIndex(index); 222 } else if (item instanceof JLabel ) { 223 ((JLabel ) item).setDisplayedMnemonicIndex(index); 224 } 225 } 226 227 232 private static void setText(Object item, String text) { 233 if (item instanceof AbstractButton ) { 234 ((AbstractButton ) item).setText(text); 235 } else { 236 ((JLabel ) item).setText(text); 237 } 238 } 239 240 245 private static void setMnemonic(Object item, int mnem) { 246 if (Utilities.isMac()) { 247 return; 250 } 251 252 if ((mnem >= 'a') && (mnem <= 'z')) { 253 mnem = mnem + ('A' - 'a'); 254 } 255 256 if (item instanceof AbstractButton ) { 257 ((AbstractButton ) item).setMnemonic(mnem); 258 } else { 259 ((JLabel ) item).setDisplayedMnemonic(mnem); 260 } 261 } 262 263 269 private static ResourceBundle getBundle() { 270 return ResourceBundle.getBundle("org.openide.awt.Mnemonics"); } 272 } 273 | Popular Tags |