1 11 package org.eclipse.ui.views.markers.internal; 12 13 import java.util.HashSet ; 14 import java.util.Set ; 15 16 25 public class MnemonicAssigner { 26 private Set assigned = new HashSet (); 28 29 private String validHotkeys = new String (); 31 32 private Set validKeys = new HashSet (); 34 35 public MnemonicAssigner() { 36 addKeys(Messages.getString("MnemonicAssigner.valid_mnemonics")); } 39 40 47 private void addKeys(String keys) { 48 validHotkeys = validHotkeys + keys; 49 50 for(int idx = 0; idx < validHotkeys.length(); idx++) { 51 validKeys.add(new Character (Character.toLowerCase(keys.charAt(idx)))); 52 } 53 } 54 55 public boolean isReserved(char toCheck) { 56 return assigned.contains(new Character (Character.toLowerCase(toCheck))); 57 } 58 59 67 private static int getAmpersandIndex(String inputString) { 68 for(int idx = 0; idx < inputString.length() - 1; idx++) { 69 char next = inputString.charAt(idx); 70 71 if (next == '&') { 72 if (inputString.charAt(idx + 1) == '&') { 73 idx++; 75 } else { 76 return idx; 77 } 78 } 79 } 80 81 return inputString.length(); 82 } 83 84 92 public static char getMnemonic(String inputString) { 93 int idx = getAmpersandIndex(inputString); 94 95 if (idx < inputString.length() - 1) { 96 return inputString.charAt(idx + 1); 97 } 98 99 return 0; 100 } 101 102 public void reserve(char toReserve) { 103 assigned.add(new Character (Character.toLowerCase(toReserve))); 104 } 105 106 113 public static String withoutMnemonic(String toRemove) { 114 String working = toRemove; 115 116 int idx = getAmpersandIndex(working); 117 while(idx < working.length()) { 118 working = working.substring(0, idx) + working.substring(idx + 1, working.length()); 119 idx = getAmpersandIndex(working); 120 } 121 122 return working; 123 } 124 125 133 public String assign(String inputString) { 134 String result = suggest(inputString); 135 reserve(result); 136 return result; 137 } 138 139 145 public void reserve(String inputString) { 146 reserve(getMnemonic(inputString)); 147 } 148 149 156 public boolean isValidMnemonic(char next) { 157 return validKeys.contains(new Character (Character.toLowerCase(next))); 158 } 159 160 168 public String suggest(String inputString) { 169 char mnemonic = getMnemonic(inputString); 170 171 if (mnemonic != 0 && !isReserved(mnemonic)) { 172 return inputString; 173 } 174 175 String stripped = withoutMnemonic(inputString); 177 178 int bestMnemonic = -1; 180 181 int mnemonicRank = -1; 186 187 boolean lastWasWhitespace = true; 188 189 for (int idx = 0; idx < stripped.length(); idx++) { 190 char next = stripped.charAt(idx); 191 192 if (isValidMnemonic(next) && !isReserved(next)) { 193 int thisRank = 0; 194 195 if (Character.isUpperCase(next)) { 197 thisRank += 1; 198 } 199 200 if (lastWasWhitespace) { 202 thisRank += 2; 203 } 204 205 if (thisRank > mnemonicRank) { 206 bestMnemonic = idx; 207 mnemonicRank = thisRank; 208 } 209 210 break; 211 } 212 213 lastWasWhitespace = (Character.isWhitespace(next)); 214 } 215 216 if (bestMnemonic >= 0) { 218 return stripped.substring(0, bestMnemonic) + '&' 219 + stripped.substring(bestMnemonic, stripped.length()); 220 } 221 222 for (int idx = 0; idx < validHotkeys.length(); idx++) { 224 char next = validHotkeys.charAt(idx); 225 226 if (!isReserved(next)) { 227 return Messages.format(Messages.getString("MnemonicAssigner.missing_mnemonic_format"), new String [] {stripped, "&" + next}); } 229 } 230 231 return inputString; 233 } 234 } 235 | Popular Tags |