1. /*
  2. * @(#)MenuShortcut.java 1.25 03/12/19
  3. *
  4. * Copyright 2004 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. * The <code>MenuShortcut</code>class represents a keyboard accelerator
  11. * for a MenuItem.
  12. * <p>
  13. * Menu shortcuts are created using virtual keycodes, not characters.
  14. * For example, a menu shortcut for Ctrl-a (assuming that Control is
  15. * the accelerator key) would be created with code like the following:
  16. * <p>
  17. * MenuShortcut ms = new MenuShortcut(KeyEvent.VK_A, false);
  18. * <p>
  19. * The accelerator key is platform-dependent and may be obtained
  20. * via {@link Toolkit#getMenuShortcutKeyMask}.
  21. *
  22. * @author Thomas Ball
  23. * @version 1.25, 12/19/03
  24. * @since JDK1.1
  25. */
  26. public class MenuShortcut implements java.io.Serializable
  27. {
  28. /**
  29. * The virtual keycode for the menu shortcut.
  30. * This is the keycode with which the menu shortcut will be created.
  31. * Note that it is a virtual keycode, not a character,
  32. * e.g. KeyEvent.VK_A, not 'a'.
  33. * Note: in 1.1.x you must use setActionCommand() on a menu item
  34. * in order for its shortcut to work, otherwise it will fire a null
  35. * action command.
  36. *
  37. * @serial
  38. * @see #getKey()
  39. * @see #usesShiftModifier()
  40. * @see java.awt.event.KeyEvent
  41. * @since JDK1.1
  42. */
  43. int key;
  44. /**
  45. * Indicates whether the shft key was pressed.
  46. * If true, the shift key was pressed.
  47. * If false, the shift key was not pressed
  48. *
  49. * @serial
  50. * @see #usesShiftModifier()
  51. * @since JDK1.1
  52. */
  53. boolean usesShift;
  54. /*
  55. * JDK 1.1 serialVersionUID
  56. */
  57. private static final long serialVersionUID = 143448358473180225L;
  58. /**
  59. * Constructs a new MenuShortcut for the specified virtual keycode.
  60. * @param key the raw keycode for this MenuShortcut, as would be returned
  61. * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
  62. * this key were pressed.
  63. * @see java.awt.event.KeyEvent
  64. **/
  65. public MenuShortcut(int key) {
  66. this(key, false);
  67. }
  68. /**
  69. * Constructs a new MenuShortcut for the specified virtual keycode.
  70. * @param key the raw keycode for this MenuShortcut, as would be returned
  71. * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
  72. * this key were pressed.
  73. * @param useShiftModifier indicates whether this MenuShortcut is invoked
  74. * with the SHIFT key down.
  75. * @see java.awt.event.KeyEvent
  76. **/
  77. public MenuShortcut(int key, boolean useShiftModifier) {
  78. this.key = key;
  79. this.usesShift = useShiftModifier;
  80. }
  81. /**
  82. * Returns the raw keycode of this MenuShortcut.
  83. * @return the raw keycode of this MenuShortcut.
  84. * @see java.awt.event.KeyEvent
  85. * @since JDK1.1
  86. */
  87. public int getKey() {
  88. return key;
  89. }
  90. /**
  91. * Returns whether this MenuShortcut must be invoked using the SHIFT key.
  92. * @return <code>true</code> if this MenuShortcut must be invoked using the
  93. * SHIFT key, <code>false</code> otherwise.
  94. * @since JDK1.1
  95. */
  96. public boolean usesShiftModifier() {
  97. return usesShift;
  98. }
  99. /**
  100. * Returns whether this MenuShortcut is the same as another:
  101. * equality is defined to mean that both MenuShortcuts use the same key
  102. * and both either use or don't use the SHIFT key.
  103. * @param s the MenuShortcut to compare with this.
  104. * @return <code>true</code> if this MenuShortcut is the same as another,
  105. * <code>false</code> otherwise.
  106. * @since JDK1.1
  107. */
  108. public boolean equals(MenuShortcut s) {
  109. return (s != null && (s.getKey() == key) &&
  110. (s.usesShiftModifier() == usesShift));
  111. }
  112. /**
  113. * Returns whether this MenuShortcut is the same as another:
  114. * equality is defined to mean that both MenuShortcuts use the same key
  115. * and both either use or don't use the SHIFT key.
  116. * @param obj the Object to compare with this.
  117. * @return <code>true</code> if this MenuShortcut is the same as another,
  118. * <code>false</code> otherwise.
  119. * @since 1.2
  120. */
  121. public boolean equals(Object obj) {
  122. if (obj instanceof MenuShortcut) {
  123. return equals( (MenuShortcut) obj );
  124. }
  125. return false;
  126. }
  127. /**
  128. * Returns the hashcode for this MenuShortcut.
  129. * @return the hashcode for this MenuShortcut.
  130. * @since 1.2
  131. */
  132. public int hashCode() {
  133. return (usesShift) ? (~key) : key;
  134. }
  135. /**
  136. * Returns an internationalized description of the MenuShortcut.
  137. * @return a string representation of this MenuShortcut.
  138. * @since JDK1.1
  139. */
  140. public String toString() {
  141. int modifiers = 0;
  142. if (!GraphicsEnvironment.isHeadless()) {
  143. modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
  144. }
  145. if (usesShiftModifier()) {
  146. modifiers |= Event.SHIFT_MASK;
  147. }
  148. return KeyEvent.getKeyModifiersText(modifiers) + "+" +
  149. KeyEvent.getKeyText(key);
  150. }
  151. /**
  152. * Returns the parameter string representing the state of this
  153. * MenuShortcut. This string is useful for debugging.
  154. * @return the parameter string of this MenuShortcut.
  155. * @since JDK1.1
  156. */
  157. protected String paramString() {
  158. String str = "key=" + key;
  159. if (usesShiftModifier()) {
  160. str += ",usesShiftModifier";
  161. }
  162. return str;
  163. }
  164. }