0N/A/*
2362N/A * Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage java.awt;
0N/A
0N/Aimport java.awt.event.KeyEvent;
0N/A
0N/A/**
0N/A * The <code>MenuShortcut</code>class represents a keyboard accelerator
0N/A * for a MenuItem.
0N/A * <p>
0N/A * Menu shortcuts are created using virtual keycodes, not characters.
0N/A * For example, a menu shortcut for Ctrl-a (assuming that Control is
0N/A * the accelerator key) would be created with code like the following:
0N/A * <p>
1067N/A * <code>MenuShortcut ms = new MenuShortcut(KeyEvent.VK_A, false);</code>
1067N/A * <p> or alternatively
1067N/A * <p>
1067N/A * <code>MenuShortcut ms = new MenuShortcut(KeyEvent.getExtendedKeyCodeForChar('A'), false);</code>
1067N/A * <p>
1067N/A * Menu shortcuts may also be constructed for a wider set of keycodes
1067N/A * using the <code>java.awt.event.KeyEvent.getExtendedKeyCodeForChar</code> call.
1067N/A * For example, a menu shortcut for "Ctrl+cyrillic ef" is created by
1067N/A * <p>
1067N/A * <code>MenuShortcut ms = new MenuShortcut(KeyEvent.getExtendedKeyCodeForChar('\u0444'), false);</code>
1067N/A * <p>
1067N/A * Note that shortcuts created with a keycode or an extended keycode defined as a constant in <code>KeyEvent</code>
1067N/A * work regardless of the current keyboard layout. However, a shortcut made of
1067N/A * an extended keycode not listed in <code>KeyEvent</code>
1067N/A * only work if the current keyboard layout produces a corresponding letter.
0N/A * <p>
0N/A * The accelerator key is platform-dependent and may be obtained
0N/A * via {@link Toolkit#getMenuShortcutKeyMask}.
0N/A *
0N/A * @author Thomas Ball
0N/A * @since JDK1.1
0N/A */
0N/Apublic class MenuShortcut implements java.io.Serializable
0N/A{
0N/A /**
0N/A * The virtual keycode for the menu shortcut.
0N/A * This is the keycode with which the menu shortcut will be created.
0N/A * Note that it is a virtual keycode, not a character,
0N/A * e.g. KeyEvent.VK_A, not 'a'.
0N/A * Note: in 1.1.x you must use setActionCommand() on a menu item
0N/A * in order for its shortcut to work, otherwise it will fire a null
0N/A * action command.
0N/A *
0N/A * @serial
0N/A * @see #getKey()
0N/A * @see #usesShiftModifier()
0N/A * @see java.awt.event.KeyEvent
0N/A * @since JDK1.1
0N/A */
0N/A int key;
0N/A
0N/A /**
0N/A * Indicates whether the shft key was pressed.
0N/A * If true, the shift key was pressed.
0N/A * If false, the shift key was not pressed
0N/A *
0N/A * @serial
0N/A * @see #usesShiftModifier()
0N/A * @since JDK1.1
0N/A */
0N/A boolean usesShift;
0N/A
0N/A /*
0N/A * JDK 1.1 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = 143448358473180225L;
0N/A
0N/A /**
0N/A * Constructs a new MenuShortcut for the specified virtual keycode.
0N/A * @param key the raw keycode for this MenuShortcut, as would be returned
0N/A * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
0N/A * this key were pressed.
0N/A * @see java.awt.event.KeyEvent
0N/A **/
0N/A public MenuShortcut(int key) {
0N/A this(key, false);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new MenuShortcut for the specified virtual keycode.
0N/A * @param key the raw keycode for this MenuShortcut, as would be returned
0N/A * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
0N/A * this key were pressed.
0N/A * @param useShiftModifier indicates whether this MenuShortcut is invoked
0N/A * with the SHIFT key down.
0N/A * @see java.awt.event.KeyEvent
0N/A **/
0N/A public MenuShortcut(int key, boolean useShiftModifier) {
0N/A this.key = key;
0N/A this.usesShift = useShiftModifier;
0N/A }
0N/A
0N/A /**
0N/A * Returns the raw keycode of this MenuShortcut.
0N/A * @return the raw keycode of this MenuShortcut.
0N/A * @see java.awt.event.KeyEvent
0N/A * @since JDK1.1
0N/A */
0N/A public int getKey() {
0N/A return key;
0N/A }
0N/A
0N/A /**
0N/A * Returns whether this MenuShortcut must be invoked using the SHIFT key.
0N/A * @return <code>true</code> if this MenuShortcut must be invoked using the
0N/A * SHIFT key, <code>false</code> otherwise.
0N/A * @since JDK1.1
0N/A */
0N/A public boolean usesShiftModifier() {
0N/A return usesShift;
0N/A }
0N/A
0N/A /**
0N/A * Returns whether this MenuShortcut is the same as another:
0N/A * equality is defined to mean that both MenuShortcuts use the same key
0N/A * and both either use or don't use the SHIFT key.
0N/A * @param s the MenuShortcut to compare with this.
0N/A * @return <code>true</code> if this MenuShortcut is the same as another,
0N/A * <code>false</code> otherwise.
0N/A * @since JDK1.1
0N/A */
0N/A public boolean equals(MenuShortcut s) {
0N/A return (s != null && (s.getKey() == key) &&
0N/A (s.usesShiftModifier() == usesShift));
0N/A }
0N/A
0N/A /**
0N/A * Returns whether this MenuShortcut is the same as another:
0N/A * equality is defined to mean that both MenuShortcuts use the same key
0N/A * and both either use or don't use the SHIFT key.
0N/A * @param obj the Object to compare with this.
0N/A * @return <code>true</code> if this MenuShortcut is the same as another,
0N/A * <code>false</code> otherwise.
0N/A * @since 1.2
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (obj instanceof MenuShortcut) {
0N/A return equals( (MenuShortcut) obj );
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns the hashcode for this MenuShortcut.
0N/A * @return the hashcode for this MenuShortcut.
0N/A * @since 1.2
0N/A */
0N/A public int hashCode() {
0N/A return (usesShift) ? (~key) : key;
0N/A }
0N/A
0N/A /**
0N/A * Returns an internationalized description of the MenuShortcut.
0N/A * @return a string representation of this MenuShortcut.
0N/A * @since JDK1.1
0N/A */
0N/A public String toString() {
0N/A int modifiers = 0;
0N/A if (!GraphicsEnvironment.isHeadless()) {
0N/A modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
0N/A }
0N/A if (usesShiftModifier()) {
0N/A modifiers |= Event.SHIFT_MASK;
0N/A }
0N/A return KeyEvent.getKeyModifiersText(modifiers) + "+" +
0N/A KeyEvent.getKeyText(key);
0N/A }
0N/A
0N/A /**
0N/A * Returns the parameter string representing the state of this
0N/A * MenuShortcut. This string is useful for debugging.
0N/A * @return the parameter string of this MenuShortcut.
0N/A * @since JDK1.1
0N/A */
0N/A protected String paramString() {
0N/A String str = "key=" + key;
0N/A if (usesShiftModifier()) {
0N/A str += ",usesShiftModifier";
0N/A }
0N/A return str;
0N/A }
0N/A}