blob: 50eef973f13be1d8942bab79e103bc10ecb10526 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package java.awt;
26
27import 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 * MenuShortcut ms = new MenuShortcut(KeyEvent.VK_A, false);
38 * <p>
39 * The accelerator key is platform-dependent and may be obtained
40 * via {@link Toolkit#getMenuShortcutKeyMask}.
41 *
42 * @author Thomas Ball
43 * @since JDK1.1
44 */
45public class MenuShortcut implements java.io.Serializable
46{
47 /**
48 * The virtual keycode for the menu shortcut.
49 * This is the keycode with which the menu shortcut will be created.
50 * Note that it is a virtual keycode, not a character,
51 * e.g. KeyEvent.VK_A, not 'a'.
52 * Note: in 1.1.x you must use setActionCommand() on a menu item
53 * in order for its shortcut to work, otherwise it will fire a null
54 * action command.
55 *
56 * @serial
57 * @see #getKey()
58 * @see #usesShiftModifier()
59 * @see java.awt.event.KeyEvent
60 * @since JDK1.1
61 */
62 int key;
63
64 /**
65 * Indicates whether the shft key was pressed.
66 * If true, the shift key was pressed.
67 * If false, the shift key was not pressed
68 *
69 * @serial
70 * @see #usesShiftModifier()
71 * @since JDK1.1
72 */
73 boolean usesShift;
74
75 /*
76 * JDK 1.1 serialVersionUID
77 */
78 private static final long serialVersionUID = 143448358473180225L;
79
80 /**
81 * Constructs a new MenuShortcut for the specified virtual keycode.
82 * @param key the raw keycode for this MenuShortcut, as would be returned
83 * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
84 * this key were pressed.
85 * @see java.awt.event.KeyEvent
86 **/
87 public MenuShortcut(int key) {
88 this(key, false);
89 }
90
91 /**
92 * Constructs a new MenuShortcut for the specified virtual keycode.
93 * @param key the raw keycode for this MenuShortcut, as would be returned
94 * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
95 * this key were pressed.
96 * @param useShiftModifier indicates whether this MenuShortcut is invoked
97 * with the SHIFT key down.
98 * @see java.awt.event.KeyEvent
99 **/
100 public MenuShortcut(int key, boolean useShiftModifier) {
101 this.key = key;
102 this.usesShift = useShiftModifier;
103 }
104
105 /**
106 * Returns the raw keycode of this MenuShortcut.
107 * @return the raw keycode of this MenuShortcut.
108 * @see java.awt.event.KeyEvent
109 * @since JDK1.1
110 */
111 public int getKey() {
112 return key;
113 }
114
115 /**
116 * Returns whether this MenuShortcut must be invoked using the SHIFT key.
117 * @return <code>true</code> if this MenuShortcut must be invoked using the
118 * SHIFT key, <code>false</code> otherwise.
119 * @since JDK1.1
120 */
121 public boolean usesShiftModifier() {
122 return usesShift;
123 }
124
125 /**
126 * Returns whether this MenuShortcut is the same as another:
127 * equality is defined to mean that both MenuShortcuts use the same key
128 * and both either use or don't use the SHIFT key.
129 * @param s the MenuShortcut to compare with this.
130 * @return <code>true</code> if this MenuShortcut is the same as another,
131 * <code>false</code> otherwise.
132 * @since JDK1.1
133 */
134 public boolean equals(MenuShortcut s) {
135 return (s != null && (s.getKey() == key) &&
136 (s.usesShiftModifier() == 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 obj the Object 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.2
147 */
148 public boolean equals(Object obj) {
149 if (obj instanceof MenuShortcut) {
150 return equals( (MenuShortcut) obj );
151 }
152 return false;
153 }
154
155 /**
156 * Returns the hashcode for this MenuShortcut.
157 * @return the hashcode for this MenuShortcut.
158 * @since 1.2
159 */
160 public int hashCode() {
161 return (usesShift) ? (~key) : key;
162 }
163
164 /**
165 * Returns an internationalized description of the MenuShortcut.
166 * @return a string representation of this MenuShortcut.
167 * @since JDK1.1
168 */
169 public String toString() {
170 int modifiers = 0;
171 if (!GraphicsEnvironment.isHeadless()) {
172 modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
173 }
174 if (usesShiftModifier()) {
175 modifiers |= Event.SHIFT_MASK;
176 }
177 return KeyEvent.getKeyModifiersText(modifiers) + "+" +
178 KeyEvent.getKeyText(key);
179 }
180
181 /**
182 * Returns the parameter string representing the state of this
183 * MenuShortcut. This string is useful for debugging.
184 * @return the parameter string of this MenuShortcut.
185 * @since JDK1.1
186 */
187 protected String paramString() {
188 String str = "key=" + key;
189 if (usesShiftModifier()) {
190 str += ",usesShiftModifier";
191 }
192 return str;
193 }
194}