1 11 12 package org.eclipse.jface.action; 13 14 import java.util.HashMap ; 15 import java.util.Map ; 16 import java.util.StringTokenizer ; 17 18 import org.eclipse.jface.resource.JFaceResources; 19 import org.eclipse.swt.SWT; 20 21 32 public final class LegacyActionTools { 33 34 40 private static Map keyCodes = null; 41 42 49 private static Map keyStrings = null; 50 51 54 private static String localizedAlt; 55 56 59 private static String localizedCommand; 60 61 64 private static String localizedCtrl; 65 66 73 private static Map localizedKeyCodes = null; 74 75 78 private static String localizedShift; 79 80 83 public static final char MNEMONIC_NONE = 0; 84 85 92 public static final String convertAccelerator(final int keyCode) { 93 String modifier = getModifierString(keyCode); 94 String fullKey; 95 if (modifier.equals("")) { fullKey = findKeyString(keyCode); 97 } else { 98 fullKey = modifier + "+" + findKeyString(keyCode); } 100 return fullKey; 101 } 102 103 111 public static final int convertAccelerator(final String acceleratorText) { 112 int accelerator = 0; 113 StringTokenizer stok = new StringTokenizer (acceleratorText, "+"); 115 int keyCode = -1; 116 117 boolean hasMoreTokens = stok.hasMoreTokens(); 118 while (hasMoreTokens) { 119 String token = stok.nextToken(); 120 hasMoreTokens = stok.hasMoreTokens(); 121 if (hasMoreTokens) { 124 int modifier = findModifier(token); 125 if (modifier != 0) { 126 accelerator |= modifier; 127 } else { return 0; 129 } 130 } else { 131 keyCode = findKeyCode(token); 132 } 133 } 134 if (keyCode != -1) { 135 accelerator |= keyCode; 136 } 137 return accelerator; 138 } 139 140 152 static final int convertLocalizedAccelerator(final String acceleratorText) { 153 int accelerator = 0; 154 StringTokenizer stok = new StringTokenizer (acceleratorText, "+"); 156 int keyCode = -1; 157 158 boolean hasMoreTokens = stok.hasMoreTokens(); 159 while (hasMoreTokens) { 160 String token = stok.nextToken(); 161 hasMoreTokens = stok.hasMoreTokens(); 162 if (hasMoreTokens) { 165 int modifier = findLocalizedModifier(token); 166 if (modifier != 0) { 167 accelerator |= modifier; 168 } else { return 0; 170 } 171 } else { 172 keyCode = findLocalizedKeyCode(token); 173 } 174 } 175 if (keyCode != -1) { 176 accelerator |= keyCode; 177 } 178 return accelerator; 179 } 180 181 190 public static final String extractAcceleratorText(final String text) { 191 if (text == null) { 192 return null; 193 } 194 195 int index = text.lastIndexOf('\t'); 196 if (index == -1) { 197 index = text.lastIndexOf('@'); 198 } 199 if (index >= 0) { 200 return text.substring(index + 1); 201 } 202 return null; 203 } 204 205 214 public static final char extractMnemonic(final String text) { 215 if (text == null) { 216 return MNEMONIC_NONE; 217 } 218 219 int index = text.indexOf('&'); 220 if (index == -1) { 221 return MNEMONIC_NONE; 222 } 223 224 final int textLength = text.length(); 225 226 if (index == textLength - 1) { 228 return MNEMONIC_NONE; 229 } 230 231 while (text.charAt(index + 1) == '&') { 233 index = text.indexOf('&', ++index); 234 if (index == textLength - 1) { 235 return MNEMONIC_NONE; 236 } 237 } 238 239 return text.charAt(index + 1); 240 } 241 242 272 public static final int findKeyCode(String token) { 273 if (keyCodes == null) { 274 initKeyCodes(); 275 } 276 token = token.toUpperCase(); 277 Integer i = (Integer ) keyCodes.get(token); 278 if (i != null) { 279 return i.intValue(); 280 } 281 if (token.length() == 1) { 282 return token.charAt(0); 283 } 284 return -1; 285 } 286 287 300 public static final String findKeyString(final int keyCode) { 301 if (keyStrings == null) { 302 initKeyStrings(); 303 } 304 int i = keyCode & ~(SWT.CTRL | SWT.ALT | SWT.SHIFT | SWT.COMMAND); 305 Integer integer = new Integer (i); 306 String result = (String ) keyStrings.get(integer); 307 if (result != null) { 308 return result; 309 } 310 result = new String (new char[] { (char) i }); 311 return result; 312 } 313 314 327 private static final int findLocalizedKeyCode(String token) { 328 if (localizedKeyCodes == null) { 329 initLocalizedKeyCodes(); 330 } 331 token = token.toUpperCase(); 332 Integer i = (Integer ) localizedKeyCodes.get(token); 333 if (i != null) { 334 return i.intValue(); 335 } 336 if (token.length() == 1) { 337 return token.charAt(0); 338 } 339 return -1; 340 } 341 342 352 private static final int findLocalizedModifier(String token) { 353 if (localizedCtrl == null) { 354 initLocalizedModifiers(); 355 } 356 357 token = token.toUpperCase(); 358 if (token.equals(localizedCtrl)) { 359 return SWT.CTRL; 360 } 361 if (token.equals(localizedShift)) { 362 return SWT.SHIFT; 363 } 364 if (token.equals(localizedAlt)) { 365 return SWT.ALT; 366 } 367 if (token.equals(localizedCommand)) { 368 return SWT.COMMAND; 369 } 370 return 0; 371 } 372 373 385 public static final int findModifier(String token) { 386 token = token.toUpperCase(); 387 if (token.equals("CTRL")) { return SWT.CTRL; 389 } 390 if (token.equals("SHIFT")) { return SWT.SHIFT; 392 } 393 if (token.equals("ALT")) { return SWT.ALT; 395 } 396 if (token.equals("COMMAND")) { return SWT.COMMAND; 398 } 399 return 0; 400 } 401 402 413 public static final String findModifierString(final int keyCode) { 414 if (keyCode == SWT.CTRL) { 415 return JFaceResources.getString("Ctrl"); } 417 if (keyCode == SWT.ALT) { 418 return JFaceResources.getString("Alt"); } 420 if (keyCode == SWT.SHIFT) { 421 return JFaceResources.getString("Shift"); } 423 if (keyCode == SWT.COMMAND) { 424 return JFaceResources.getString("Command"); } 426 return null; 427 } 428 429 438 private static String getModifierString(int keyCode) { 439 String modString = ""; 441 if ((keyCode & SWT.CTRL) != 0) { 442 modString = findModifierString(keyCode & SWT.CTRL); 443 } 444 445 if ((keyCode & SWT.ALT) != 0) { 446 if (modString.equals("")) { modString = findModifierString(keyCode & SWT.ALT); 448 } else { 449 modString = modString 450 + "+" + findModifierString(keyCode & SWT.ALT); } 452 } 453 454 if ((keyCode & SWT.SHIFT) != 0) { 455 if (modString.equals("")) { modString = findModifierString(keyCode & SWT.SHIFT); 457 } else { 458 modString = modString 459 + "+" + findModifierString(keyCode & SWT.SHIFT); } 461 } 462 463 if ((keyCode & SWT.COMMAND) != 0) { 464 if (modString.equals("")) { modString = findModifierString(keyCode & SWT.COMMAND); 466 } else { 467 modString = modString 468 + "+" + findModifierString(keyCode & SWT.COMMAND); } 470 } 471 472 return modString; 473 } 474 475 478 private static final void initKeyCodes() { 479 keyCodes = new HashMap (40); 480 481 keyCodes.put("BACKSPACE", new Integer (8)); keyCodes.put("TAB", new Integer (9)); keyCodes.put("RETURN", new Integer (13)); keyCodes.put("ENTER", new Integer (13)); keyCodes.put("ESCAPE", new Integer (27)); keyCodes.put("ESC", new Integer (27)); keyCodes.put("DELETE", new Integer (127)); 489 keyCodes.put("SPACE", new Integer (' ')); keyCodes.put("ARROW_UP", new Integer (SWT.ARROW_UP)); keyCodes.put("ARROW_DOWN", new Integer (SWT.ARROW_DOWN)); keyCodes.put("ARROW_LEFT", new Integer (SWT.ARROW_LEFT)); keyCodes.put("ARROW_RIGHT", new Integer (SWT.ARROW_RIGHT)); keyCodes.put("PAGE_UP", new Integer (SWT.PAGE_UP)); keyCodes.put("PAGE_DOWN", new Integer (SWT.PAGE_DOWN)); keyCodes.put("HOME", new Integer (SWT.HOME)); keyCodes.put("END", new Integer (SWT.END)); keyCodes.put("INSERT", new Integer (SWT.INSERT)); keyCodes.put("F1", new Integer (SWT.F1)); keyCodes.put("F2", new Integer (SWT.F2)); keyCodes.put("F3", new Integer (SWT.F3)); keyCodes.put("F4", new Integer (SWT.F4)); keyCodes.put("F5", new Integer (SWT.F5)); keyCodes.put("F6", new Integer (SWT.F6)); keyCodes.put("F7", new Integer (SWT.F7)); keyCodes.put("F8", new Integer (SWT.F8)); keyCodes.put("F9", new Integer (SWT.F9)); keyCodes.put("F10", new Integer (SWT.F10)); keyCodes.put("F11", new Integer (SWT.F11)); keyCodes.put("F12", new Integer (SWT.F12)); } 512 513 516 private static void initKeyStrings() { 517 keyStrings = new HashMap (40); 518 519 keyStrings.put(new Integer (8), JFaceResources.getString("Backspace")); keyStrings.put(new Integer (9), JFaceResources.getString("Tab")); keyStrings.put(new Integer (13), JFaceResources.getString("Return")); keyStrings.put(new Integer (13), JFaceResources.getString("Enter")); keyStrings.put(new Integer (27), JFaceResources.getString("Escape")); keyStrings.put(new Integer (27), JFaceResources.getString("Esc")); keyStrings.put(new Integer (127), JFaceResources.getString("Delete")); 527 keyStrings.put(new Integer (' '), JFaceResources.getString("Space")); 529 keyStrings.put(new Integer (SWT.ARROW_UP), JFaceResources 530 .getString("Arrow_Up")); keyStrings.put(new Integer (SWT.ARROW_DOWN), JFaceResources 532 .getString("Arrow_Down")); keyStrings.put(new Integer (SWT.ARROW_LEFT), JFaceResources 534 .getString("Arrow_Left")); keyStrings.put(new Integer (SWT.ARROW_RIGHT), JFaceResources 536 .getString("Arrow_Right")); keyStrings.put(new Integer (SWT.PAGE_UP), JFaceResources 538 .getString("Page_Up")); keyStrings.put(new Integer (SWT.PAGE_DOWN), JFaceResources 540 .getString("Page_Down")); keyStrings.put(new Integer (SWT.HOME), JFaceResources.getString("Home")); keyStrings.put(new Integer (SWT.END), JFaceResources.getString("End")); keyStrings.put(new Integer (SWT.INSERT), JFaceResources 544 .getString("Insert")); keyStrings.put(new Integer (SWT.F1), JFaceResources.getString("F1")); keyStrings.put(new Integer (SWT.F2), JFaceResources.getString("F2")); keyStrings.put(new Integer (SWT.F3), JFaceResources.getString("F3")); keyStrings.put(new Integer (SWT.F4), JFaceResources.getString("F4")); keyStrings.put(new Integer (SWT.F5), JFaceResources.getString("F5")); keyStrings.put(new Integer (SWT.F6), JFaceResources.getString("F6")); keyStrings.put(new Integer (SWT.F7), JFaceResources.getString("F7")); keyStrings.put(new Integer (SWT.F8), JFaceResources.getString("F8")); keyStrings.put(new Integer (SWT.F9), JFaceResources.getString("F9")); keyStrings.put(new Integer (SWT.F10), JFaceResources.getString("F10")); keyStrings.put(new Integer (SWT.F11), JFaceResources.getString("F11")); keyStrings.put(new Integer (SWT.F12), JFaceResources.getString("F12")); } 558 559 562 private static void initLocalizedKeyCodes() { 563 localizedKeyCodes = new HashMap (40); 564 565 localizedKeyCodes.put(JFaceResources 566 .getString("Backspace").toUpperCase(), new Integer (8)); localizedKeyCodes.put( 568 JFaceResources.getString("Tab").toUpperCase(), new Integer (9)); localizedKeyCodes 570 .put( 571 JFaceResources.getString("Return").toUpperCase(), new Integer (13)); localizedKeyCodes 573 .put( 574 JFaceResources.getString("Enter").toUpperCase(), new Integer (13)); localizedKeyCodes 576 .put( 577 JFaceResources.getString("Escape").toUpperCase(), new Integer (27)); localizedKeyCodes.put( 579 JFaceResources.getString("Esc").toUpperCase(), new Integer (27)); localizedKeyCodes 581 .put( 582 JFaceResources.getString("Delete").toUpperCase(), new Integer (127)); 584 localizedKeyCodes 585 .put( 586 JFaceResources.getString("Space").toUpperCase(), new Integer (' ')); 588 localizedKeyCodes 589 .put( 590 JFaceResources.getString("Arrow_Up").toUpperCase(), new Integer (SWT.ARROW_UP)); localizedKeyCodes 592 .put( 593 JFaceResources.getString("Arrow_Down").toUpperCase(), new Integer (SWT.ARROW_DOWN)); localizedKeyCodes 595 .put( 596 JFaceResources.getString("Arrow_Left").toUpperCase(), new Integer (SWT.ARROW_LEFT)); localizedKeyCodes 598 .put( 599 JFaceResources.getString("Arrow_Right").toUpperCase(), new Integer (SWT.ARROW_RIGHT)); localizedKeyCodes 601 .put( 602 JFaceResources.getString("Page_Up").toUpperCase(), new Integer (SWT.PAGE_UP)); localizedKeyCodes 604 .put( 605 JFaceResources.getString("Page_Down").toUpperCase(), new Integer (SWT.PAGE_DOWN)); localizedKeyCodes 607 .put( 608 JFaceResources.getString("Home").toUpperCase(), new Integer (SWT.HOME)); localizedKeyCodes 610 .put( 611 JFaceResources.getString("End").toUpperCase(), new Integer (SWT.END)); localizedKeyCodes 613 .put( 614 JFaceResources.getString("Insert").toUpperCase(), new Integer (SWT.INSERT)); localizedKeyCodes 616 .put( 617 JFaceResources.getString("F1").toUpperCase(), new Integer (SWT.F1)); localizedKeyCodes 619 .put( 620 JFaceResources.getString("F2").toUpperCase(), new Integer (SWT.F2)); localizedKeyCodes 622 .put( 623 JFaceResources.getString("F3").toUpperCase(), new Integer (SWT.F3)); localizedKeyCodes 625 .put( 626 JFaceResources.getString("F4").toUpperCase(), new Integer (SWT.F4)); localizedKeyCodes 628 .put( 629 JFaceResources.getString("F5").toUpperCase(), new Integer (SWT.F5)); localizedKeyCodes 631 .put( 632 JFaceResources.getString("F6").toUpperCase(), new Integer (SWT.F6)); localizedKeyCodes 634 .put( 635 JFaceResources.getString("F7").toUpperCase(), new Integer (SWT.F7)); localizedKeyCodes 637 .put( 638 JFaceResources.getString("F8").toUpperCase(), new Integer (SWT.F8)); localizedKeyCodes 640 .put( 641 JFaceResources.getString("F9").toUpperCase(), new Integer (SWT.F9)); localizedKeyCodes 643 .put( 644 JFaceResources.getString("F10").toUpperCase(), new Integer (SWT.F10)); localizedKeyCodes 646 .put( 647 JFaceResources.getString("F11").toUpperCase(), new Integer (SWT.F11)); localizedKeyCodes 649 .put( 650 JFaceResources.getString("F12").toUpperCase(), new Integer (SWT.F12)); } 652 653 656 private static void initLocalizedModifiers() { 657 localizedCtrl = JFaceResources.getString("Ctrl").toUpperCase(); localizedShift = JFaceResources.getString("Shift").toUpperCase(); localizedAlt = JFaceResources.getString("Alt").toUpperCase(); localizedCommand = JFaceResources.getString("Command").toUpperCase(); } 662 663 672 public static final String removeAcceleratorText(final String text) { 673 int index = text.lastIndexOf('\t'); 674 if (index == -1) { 675 index = text.lastIndexOf('@'); 676 } 677 if (index >= 0) { 678 return text.substring(0, index); 679 } 680 return text; 681 } 682 683 692 public static final String removeMnemonics(final String text) { 693 int index = text.indexOf('&'); 694 if (index == -1) { 695 return text; 696 } 697 int len = text.length(); 698 StringBuffer sb = new StringBuffer (len); 699 int lastIndex = 0; 700 while (index != -1) { 701 if (index == len - 1) { 703 break; 704 } 705 if (text.charAt(index + 1) == '&') { 707 ++index; 708 } 709 710 if (index > 0 && text.charAt(index - 1) == '(' 712 && text.length() >= index + 3 713 && text.charAt(index + 2) == ')') { 714 sb.append(text.substring(lastIndex, index - 1)); 715 index += 3; 716 } else { 717 sb.append(text.substring(lastIndex, index)); 718 ++index; 720 } 721 722 lastIndex = index; 723 index = text.indexOf('&', index); 724 } 725 if (lastIndex < len) { 726 sb.append(text.substring(lastIndex, len)); 727 } 728 return sb.toString(); 729 } 730 731 734 private LegacyActionTools() { 735 } 737 738 } 739 | Popular Tags |