1. /*
  2. * @(#)MenuShortcut.java 1.16 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.awt.event.KeyEvent;
  9. /**
  10. * A class which represents a keyboard accelerator for a MenuItem.
  11. *
  12. * @version 1.16, 11/29/01
  13. * @author Thomas Ball
  14. */
  15. public class MenuShortcut implements java.io.Serializable
  16. {
  17. /**
  18. * This is indicates the virtual keycode for the menu shortcut.
  19. * It is the key code with which the menu short cut will be created.
  20. * In 1.1.2 you must use setActionCommand() on a menu item
  21. * in order for its shortcut to work, otherwise it will fire a null
  22. * action command.
  23. * Must use KeyEvent virtual keys - eg : VK_A.
  24. *
  25. * @serial
  26. * @see getKey()
  27. * @see usesShiftModifier()
  28. */
  29. int key;
  30. /**
  31. * Indicates whether the shft key was pressed.
  32. * If true, the shift key was pressed.
  33. * If false, the shift key was not pressed
  34. *
  35. * @serial
  36. * @see usesShiftModifier()
  37. */
  38. boolean usesShift;
  39. /*
  40. * JDK 1.1 serialVersionUID
  41. */
  42. private static final long serialVersionUID = 143448358473180225L;
  43. /**
  44. * Constructs a new MenuShortcut for the specified key.
  45. * @param key the raw keycode for this MenuShortcut, as would be returned
  46. * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
  47. * this key were pressed.
  48. **/
  49. public MenuShortcut(int key) {
  50. this(key, false);
  51. }
  52. /**
  53. * Constructs a new MenuShortcut for the specified key.
  54. * @param key the raw keycode for this MenuShortcut, as would be returned
  55. * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
  56. * this key were pressed.
  57. * @param useShiftModifier indicates whether this MenuShortcut is invoked
  58. * with the SHIFT key down.
  59. **/
  60. public MenuShortcut(int key, boolean useShiftModifier) {
  61. // Convenience conversion for programmers who confuse key posts with
  62. // ASCII characters -- do not internationalize! They *should* be
  63. // using KeyEvent virtual keys, such as VK_A.
  64. if (key >= 'a' && key <= 'z') {
  65. key = (int)Character.toUpperCase((char)key);
  66. }
  67. this.key = key;
  68. this.usesShift = useShiftModifier;
  69. }
  70. /**
  71. * Return the raw keycode of this MenuShortcut.
  72. */
  73. public int getKey() {
  74. return key;
  75. }
  76. /**
  77. * Return whether this MenuShortcut must be invoked using the SHIFT key.
  78. */
  79. public boolean usesShiftModifier() {
  80. return usesShift;
  81. }
  82. /**
  83. * Returns whether this MenuShortcut is the same as another:
  84. * equality is defined to mean that both MenuShortcuts use the same key
  85. * and both either use or don't use the SHIFT key.
  86. * @param s the MenuShortcut to compare with this.
  87. */
  88. public boolean equals(MenuShortcut s) {
  89. return (s != null && (s.getKey() == key) &&
  90. (s.usesShiftModifier() == usesShift));
  91. }
  92. /**
  93. * Returns whether this MenuShortcut is the same as another:
  94. * equality is defined to mean that both MenuShortcuts use the same key
  95. * and both either use or don't use the SHIFT key.
  96. * @param obj the Object to compare with this.
  97. */
  98. public boolean equals(Object obj) {
  99. if (obj instanceof MenuShortcut) {
  100. return equals( (MenuShortcut) obj );
  101. }
  102. return false;
  103. }
  104. /**
  105. * Returns the hashcode for this MenuShortcut.
  106. */
  107. public int hashCode() {
  108. return (usesShift) ? (~key) : key;
  109. }
  110. /**
  111. * Returns an internationalized description of the MenuShortcut.
  112. */
  113. public String toString() {
  114. int modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
  115. if (usesShiftModifier()) {
  116. modifiers |= Event.SHIFT_MASK;
  117. }
  118. return KeyEvent.getKeyModifiersText(modifiers) + "+" +
  119. KeyEvent.getKeyText(key);
  120. }
  121. protected String paramString() {
  122. String str = "key=" + key;
  123. if (usesShiftModifier()) {
  124. str += ",usesShiftModifier";
  125. }
  126. return str;
  127. }
  128. }