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