jdk/src/share/classes/java/awt/MenuShortcut.java
author yan
Fri, 27 Mar 2009 12:01:24 +0300
changeset 2473 3f4bbd3be2f1
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6680988: KeyEvent is still missing VK values for many keyboards Summary: 2 new methods and some fields added to KeyEvent, plus hash of constants introduced Reviewed-by: art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2473
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
     2
 * Copyright 1996-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.event.KeyEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The <code>MenuShortcut</code>class represents a keyboard accelerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * for a MenuItem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Menu shortcuts are created using virtual keycodes, not characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * For example, a menu shortcut for Ctrl-a (assuming that Control is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * the accelerator key) would be created with code like the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
2473
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
    37
 * <code>MenuShortcut ms = new MenuShortcut(KeyEvent.VK_A, false);</code>
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
    38
 * <p> or alternatively
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
    39
 * <p>
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
    40
 * <code>MenuShortcut ms = new MenuShortcut(KeyEvent.getExtendedKeyCodeForChar('A'), false);</code>
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
    41
 * <p>
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
    42
 * Menu shortcuts may also be constructed for a wider set of keycodes
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
    43
 * using the <code>java.awt.event.KeyEvent.getExtendedKeyCodeForChar</code> call.
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
    44
 * For example, a menu shortcut for "Ctrl+cyrillic ef" is created by
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
    45
 * <p>
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
    46
 * <code>MenuShortcut ms = new MenuShortcut(KeyEvent.getExtendedKeyCodeForChar('\u0444'), false);</code>
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
    47
 * <p>
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
    48
 * Note that shortcuts created with a keycode or an extended keycode defined as a constant in <code>KeyEvent</code>
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
    49
 * work regardless of the current keyboard layout. However, a shortcut made of
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
    50
 * an extended keycode not listed in <code>KeyEvent</code>
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
    51
 * only work if the current keyboard layout produces a corresponding letter.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * The accelerator key is platform-dependent and may be obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * via {@link Toolkit#getMenuShortcutKeyMask}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Thomas Ball
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class MenuShortcut implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * The virtual keycode for the menu shortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * This is the keycode with which the menu shortcut will be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Note that it is a virtual keycode, not a character,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * e.g. KeyEvent.VK_A, not 'a'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Note: in 1.1.x you must use setActionCommand() on a menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * in order for its shortcut to work, otherwise it will fire a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * action command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @see #getKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @see #usesShiftModifier()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    int key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Indicates whether the shft key was pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * If true, the shift key was pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * If false, the shift key was not pressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @see #usesShiftModifier()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    boolean usesShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     private static final long serialVersionUID = 143448358473180225L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Constructs a new MenuShortcut for the specified virtual keycode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param key the raw keycode for this MenuShortcut, as would be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * this key were pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public MenuShortcut(int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this(key, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Constructs a new MenuShortcut for the specified virtual keycode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param key the raw keycode for this MenuShortcut, as would be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * this key were pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param useShiftModifier indicates whether this MenuShortcut is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * with the SHIFT key down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public MenuShortcut(int key, boolean useShiftModifier) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.usesShift = useShiftModifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Returns the raw keycode of this MenuShortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @return the raw keycode of this MenuShortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public int getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Returns whether this MenuShortcut must be invoked using the SHIFT key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @return <code>true</code> if this MenuShortcut must be invoked using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * SHIFT key, <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public boolean usesShiftModifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return usesShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Returns whether this MenuShortcut is the same as another:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * equality is defined to mean that both MenuShortcuts use the same key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * and both either use or don't use the SHIFT key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param s the MenuShortcut to compare with this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return <code>true</code> if this MenuShortcut is the same as another,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public boolean equals(MenuShortcut s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return (s != null && (s.getKey() == key) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                (s.usesShiftModifier() == usesShift));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Returns whether this MenuShortcut is the same as another:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * equality is defined to mean that both MenuShortcuts use the same key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * and both either use or don't use the SHIFT key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param obj the Object to compare with this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @return <code>true</code> if this MenuShortcut is the same as another,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (obj instanceof MenuShortcut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return equals( (MenuShortcut) obj );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Returns the hashcode for this MenuShortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return the hashcode for this MenuShortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return (usesShift) ? (~key) : key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Returns an internationalized description of the MenuShortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @return a string representation of this MenuShortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        int modifiers = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (usesShiftModifier()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            modifiers |= Event.SHIFT_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return KeyEvent.getKeyModifiersText(modifiers) + "+" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
               KeyEvent.getKeyText(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Returns the parameter string representing the state of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * MenuShortcut. This string is useful for debugging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @return    the parameter string of this MenuShortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        String str = "key=" + key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (usesShiftModifier()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            str += ",usesShiftModifier";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
}