10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.awt; 26 27 import java.awt.event.KeyEvent; 28 29 /** 30 * The <code>MenuShortcut</code>class represents a keyboard accelerator 31 * for a MenuItem. 32 * <p> 33 * Menu shortcuts are created using virtual keycodes, not characters. 34 * For example, a menu shortcut for Ctrl-a (assuming that Control is 35 * the accelerator key) would be created with code like the following: 36 * <p> 37 * <code>MenuShortcut ms = new MenuShortcut(KeyEvent.VK_A, false);</code> 38 * <p> or alternatively 39 * <p> 40 * <code>MenuShortcut ms = new MenuShortcut(KeyEvent.getExtendedKeyCodeForChar('A'), false);</code> 41 * <p> 42 * Menu shortcuts may also be constructed for a wider set of keycodes 43 * using the <code>java.awt.event.KeyEvent.getExtendedKeyCodeForChar</code> call. 44 * For example, a menu shortcut for "Ctrl+cyrillic ef" is created by 45 * <p> 46 * <code>MenuShortcut ms = new MenuShortcut(KeyEvent.getExtendedKeyCodeForChar('\u0444'), false);</code> 47 * <p> 48 * Note that shortcuts created with a keycode or an extended keycode defined as a constant in <code>KeyEvent</code> 49 * work regardless of the current keyboard layout. However, a shortcut made of 50 * an extended keycode not listed in <code>KeyEvent</code> 51 * only work if the current keyboard layout produces a corresponding letter. 52 * <p> 53 * The accelerator key is platform-dependent and may be obtained 54 * via {@link Toolkit#getMenuShortcutKeyMask}. 55 * 56 * @author Thomas Ball 57 * @since 1.1 58 */ 59 public class MenuShortcut implements java.io.Serializable 60 { 61 /** 62 * The virtual keycode for the menu shortcut. 63 * This is the keycode with which the menu shortcut will be created. 64 * Note that it is a virtual keycode, not a character, 65 * e.g. KeyEvent.VK_A, not 'a'. 66 * Note: in 1.1.x you must use setActionCommand() on a menu item 67 * in order for its shortcut to work, otherwise it will fire a null 68 * action command. 69 * 70 * @serial 111 * with the SHIFT key down. 112 * @see java.awt.event.KeyEvent 113 **/ 114 public MenuShortcut(int key, boolean useShiftModifier) { 115 this.key = key; 116 this.usesShift = useShiftModifier; 117 } 118 119 /** 120 * Returns the raw keycode of this MenuShortcut. 121 * @return the raw keycode of this MenuShortcut. 122 * @see java.awt.event.KeyEvent 123 * @since 1.1 124 */ 125 public int getKey() { 126 return key; 127 } 128 129 /** 130 * Returns whether this MenuShortcut must be invoked using the SHIFT key. 131 * @return <code>true</code> if this MenuShortcut must be invoked using the 132 * SHIFT key, <code>false</code> otherwise. 133 * @since 1.1 134 */ 135 public boolean usesShiftModifier() { 136 return usesShift; 137 } 138 139 /** 140 * Returns whether this MenuShortcut is the same as another: 141 * equality is defined to mean that both MenuShortcuts use the same key 142 * and both either use or don't use the SHIFT key. 143 * @param s the MenuShortcut to compare with this. 144 * @return <code>true</code> if this MenuShortcut is the same as another, 145 * <code>false</code> otherwise. 146 * @since 1.1 147 */ 148 public boolean equals(MenuShortcut s) { 149 return (s != null && (s.getKey() == key) && 150 (s.usesShiftModifier() == usesShift)); 151 } 152 153 /** 154 * Returns whether this MenuShortcut is the same as another: 155 * equality is defined to mean that both MenuShortcuts use the same key 156 * and both either use or don't use the SHIFT key. 157 * @param obj the Object to compare with this. 158 * @return <code>true</code> if this MenuShortcut is the same as another, 159 * <code>false</code> otherwise. 160 * @since 1.2 161 */ 162 public boolean equals(Object obj) { 163 if (obj instanceof MenuShortcut) { 164 return equals( (MenuShortcut) obj ); 165 } 166 return false; 167 } 168 169 /** 170 * Returns the hashcode for this MenuShortcut. 171 * @return the hashcode for this MenuShortcut. 172 * @since 1.2 173 */ 174 public int hashCode() { 175 return (usesShift) ? (~key) : key; 176 } 177 178 /** 179 * Returns an internationalized description of the MenuShortcut. | 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.awt; 26 27 import java.awt.event.KeyEvent; 28 29 /** 30 * The {@code MenuShortcut} class represents a keyboard accelerator 31 * for a MenuItem. 32 * <p> 33 * Menu shortcuts are created using virtual keycodes, not characters. 34 * For example, a menu shortcut for Ctrl-a (assuming that Control is 35 * the accelerator key) would be created with code like the following: 36 * <p> 37 * {@code MenuShortcut ms = new MenuShortcut(KeyEvent.VK_A, false);} 38 * <p> or alternatively 39 * <p> 40 * {@code MenuShortcut ms = new MenuShortcut(KeyEvent.getExtendedKeyCodeForChar('A'), false);} 41 * <p> 42 * Menu shortcuts may also be constructed for a wider set of keycodes 43 * using the {@code java.awt.event.KeyEvent.getExtendedKeyCodeForChar} call. 44 * For example, a menu shortcut for "Ctrl+cyrillic ef" is created by 45 * <p> 46 * <code>MenuShortcut ms = new MenuShortcut(KeyEvent.getExtendedKeyCodeForChar('\u0444'), false);</code> 47 * <p> 48 * Note that shortcuts created with a keycode or an extended keycode defined as a constant in {@code KeyEvent} 49 * work regardless of the current keyboard layout. However, a shortcut made of 50 * an extended keycode not listed in {@code KeyEvent} 51 * only work if the current keyboard layout produces a corresponding letter. 52 * <p> 53 * The accelerator key is platform-dependent and may be obtained 54 * via {@link Toolkit#getMenuShortcutKeyMask}. 55 * 56 * @author Thomas Ball 57 * @since 1.1 58 */ 59 public class MenuShortcut implements java.io.Serializable 60 { 61 /** 62 * The virtual keycode for the menu shortcut. 63 * This is the keycode with which the menu shortcut will be created. 64 * Note that it is a virtual keycode, not a character, 65 * e.g. KeyEvent.VK_A, not 'a'. 66 * Note: in 1.1.x you must use setActionCommand() on a menu item 67 * in order for its shortcut to work, otherwise it will fire a null 68 * action command. 69 * 70 * @serial 111 * with the SHIFT key down. 112 * @see java.awt.event.KeyEvent 113 **/ 114 public MenuShortcut(int key, boolean useShiftModifier) { 115 this.key = key; 116 this.usesShift = useShiftModifier; 117 } 118 119 /** 120 * Returns the raw keycode of this MenuShortcut. 121 * @return the raw keycode of this MenuShortcut. 122 * @see java.awt.event.KeyEvent 123 * @since 1.1 124 */ 125 public int getKey() { 126 return key; 127 } 128 129 /** 130 * Returns whether this MenuShortcut must be invoked using the SHIFT key. 131 * @return {@code true} if this MenuShortcut must be invoked using the 132 * SHIFT key, {@code false} otherwise. 133 * @since 1.1 134 */ 135 public boolean usesShiftModifier() { 136 return usesShift; 137 } 138 139 /** 140 * Returns whether this MenuShortcut is the same as another: 141 * equality is defined to mean that both MenuShortcuts use the same key 142 * and both either use or don't use the SHIFT key. 143 * @param s the MenuShortcut to compare with this. 144 * @return {@code true} if this MenuShortcut is the same as another, 145 * {@code false} otherwise. 146 * @since 1.1 147 */ 148 public boolean equals(MenuShortcut s) { 149 return (s != null && (s.getKey() == key) && 150 (s.usesShiftModifier() == usesShift)); 151 } 152 153 /** 154 * Returns whether this MenuShortcut is the same as another: 155 * equality is defined to mean that both MenuShortcuts use the same key 156 * and both either use or don't use the SHIFT key. 157 * @param obj the Object to compare with this. 158 * @return {@code true} if this MenuShortcut is the same as another, 159 * {@code false} otherwise. 160 * @since 1.2 161 */ 162 public boolean equals(Object obj) { 163 if (obj instanceof MenuShortcut) { 164 return equals( (MenuShortcut) obj ); 165 } 166 return false; 167 } 168 169 /** 170 * Returns the hashcode for this MenuShortcut. 171 * @return the hashcode for this MenuShortcut. 172 * @since 1.2 173 */ 174 public int hashCode() { 175 return (usesShift) ? (~key) : key; 176 } 177 178 /** 179 * Returns an internationalized description of the MenuShortcut. |