jdk/src/share/classes/java/awt/MenuShortcut.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2473 3f4bbd3be2f1
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2003 Sun Microsystems, Inc.  All Rights Reserved.
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * MenuShortcut ms = new MenuShortcut(KeyEvent.VK_A, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The accelerator key is platform-dependent and may be obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * via {@link Toolkit#getMenuShortcutKeyMask}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Thomas Ball
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class MenuShortcut implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * The virtual keycode for the menu shortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * This is the keycode with which the menu shortcut will be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Note that it is a virtual keycode, not a character,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * e.g. KeyEvent.VK_A, not 'a'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Note: in 1.1.x you must use setActionCommand() on a menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * in order for its shortcut to work, otherwise it will fire a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * action command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @see #getKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @see #usesShiftModifier()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Indicates whether the shft key was pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * If true, the shift key was pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * If false, the shift key was not pressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @see #usesShiftModifier()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    boolean usesShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     private static final long serialVersionUID = 143448358473180225L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Constructs a new MenuShortcut for the specified virtual keycode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param key the raw keycode for this MenuShortcut, as would be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * this key were pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public MenuShortcut(int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this(key, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Constructs a new MenuShortcut for the specified virtual keycode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param key the raw keycode for this MenuShortcut, as would be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * this key were pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param useShiftModifier indicates whether this MenuShortcut is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * with the SHIFT key down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public MenuShortcut(int key, boolean useShiftModifier) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.usesShift = useShiftModifier;
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
     * Returns the raw keycode of this MenuShortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return the raw keycode of this MenuShortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public int getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Returns whether this MenuShortcut must be invoked using the SHIFT key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @return <code>true</code> if this MenuShortcut must be invoked using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * SHIFT key, <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public boolean usesShiftModifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return usesShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Returns whether this MenuShortcut is the same as another:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * equality is defined to mean that both MenuShortcuts use the same key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * and both either use or don't use the SHIFT key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param s the MenuShortcut to compare with this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @return <code>true</code> if this MenuShortcut is the same as another,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public boolean equals(MenuShortcut s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return (s != null && (s.getKey() == key) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                (s.usesShiftModifier() == 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 obj the Object 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 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (obj instanceof MenuShortcut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return equals( (MenuShortcut) obj );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Returns the hashcode for this MenuShortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @return the hashcode for this MenuShortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return (usesShift) ? (~key) : key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns an internationalized description of the MenuShortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @return a string representation of this MenuShortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        int modifiers = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (usesShiftModifier()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            modifiers |= Event.SHIFT_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return KeyEvent.getKeyModifiersText(modifiers) + "+" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
               KeyEvent.getKeyText(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Returns the parameter string representing the state of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * MenuShortcut. This string is useful for debugging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @return    the parameter string of this MenuShortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        String str = "key=" + key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (usesShiftModifier()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            str += ",usesShiftModifier";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
}