001 /*
002 * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package java.awt;
026
027 import java.awt.event.KeyEvent;
028
029 /**
030 * The <code>MenuShortcut</code>class represents a keyboard accelerator
031 * for a MenuItem.
032 * <p>
033 * Menu shortcuts are created using virtual keycodes, not characters.
034 * For example, a menu shortcut for Ctrl-a (assuming that Control is
035 * the accelerator key) would be created with code like the following:
036 * <p>
037 * MenuShortcut ms = new MenuShortcut(KeyEvent.VK_A, false);
038 * <p>
039 * The accelerator key is platform-dependent and may be obtained
040 * via {@link Toolkit#getMenuShortcutKeyMask}.
041 *
042 * @author Thomas Ball
043 * @version 1.32, 05/05/07
044 * @since JDK1.1
045 */
046 public class MenuShortcut implements java.io.Serializable {
047 /**
048 * The virtual keycode for the menu shortcut.
049 * This is the keycode with which the menu shortcut will be created.
050 * Note that it is a virtual keycode, not a character,
051 * e.g. KeyEvent.VK_A, not 'a'.
052 * Note: in 1.1.x you must use setActionCommand() on a menu item
053 * in order for its shortcut to work, otherwise it will fire a null
054 * action command.
055 *
056 * @serial
057 * @see #getKey()
058 * @see #usesShiftModifier()
059 * @see java.awt.event.KeyEvent
060 * @since JDK1.1
061 */
062 int key;
063
064 /**
065 * Indicates whether the shft key was pressed.
066 * If true, the shift key was pressed.
067 * If false, the shift key was not pressed
068 *
069 * @serial
070 * @see #usesShiftModifier()
071 * @since JDK1.1
072 */
073 boolean usesShift;
074
075 /*
076 * JDK 1.1 serialVersionUID
077 */
078 private static final long serialVersionUID = 143448358473180225L;
079
080 /**
081 * Constructs a new MenuShortcut for the specified virtual keycode.
082 * @param key the raw keycode for this MenuShortcut, as would be returned
083 * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
084 * this key were pressed.
085 * @see java.awt.event.KeyEvent
086 **/
087 public MenuShortcut(int key) {
088 this (key, false);
089 }
090
091 /**
092 * Constructs a new MenuShortcut for the specified virtual keycode.
093 * @param key the raw keycode for this MenuShortcut, as would be returned
094 * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
095 * this key were pressed.
096 * @param useShiftModifier indicates whether this MenuShortcut is invoked
097 * with the SHIFT key down.
098 * @see java.awt.event.KeyEvent
099 **/
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) && (s
136 .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()
173 .getMenuShortcutKeyMask();
174 }
175 if (usesShiftModifier()) {
176 modifiers |= Event.SHIFT_MASK;
177 }
178 return KeyEvent.getKeyModifiersText(modifiers) + "+"
179 + KeyEvent.getKeyText(key);
180 }
181
182 /**
183 * Returns the parameter string representing the state of this
184 * MenuShortcut. This string is useful for debugging.
185 * @return the parameter string of this MenuShortcut.
186 * @since JDK1.1
187 */
188 protected String paramString() {
189 String str = "key=" + key;
190 if (usesShiftModifier()) {
191 str += ",usesShiftModifier";
192 }
193 return str;
194 }
195 }
|