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