KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > MenuShortcut


1 /*
2  * @(#)MenuShortcut.java 1.25 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.awt;
8
9 import java.awt.event.KeyEvent JavaDoc;
10
11 /**
12  * The <code>MenuShortcut</code>class represents a keyboard accelerator
13  * for a MenuItem.
14  * <p>
15  * Menu shortcuts are created using virtual keycodes, not characters.
16  * For example, a menu shortcut for Ctrl-a (assuming that Control is
17  * the accelerator key) would be created with code like the following:
18  * <p>
19  * MenuShortcut ms = new MenuShortcut(KeyEvent.VK_A, false);
20  * <p>
21  * The accelerator key is platform-dependent and may be obtained
22  * via {@link Toolkit#getMenuShortcutKeyMask}.
23  *
24  * @author Thomas Ball
25  * @version 1.25, 12/19/03
26  * @since JDK1.1
27  */

28 public class MenuShortcut implements java.io.Serializable JavaDoc
29 {
30     /**
31      * The virtual keycode for the menu shortcut.
32      * This is the keycode with which the menu shortcut will be created.
33      * Note that it is a virtual keycode, not a character,
34      * e.g. KeyEvent.VK_A, not 'a'.
35      * Note: in 1.1.x you must use setActionCommand() on a menu item
36      * in order for its shortcut to work, otherwise it will fire a null
37      * action command.
38      *
39      * @serial
40      * @see #getKey()
41      * @see #usesShiftModifier()
42      * @see java.awt.event.KeyEvent
43      * @since JDK1.1
44      */

45     int key;
46
47     /**
48      * Indicates whether the shft key was pressed.
49      * If true, the shift key was pressed.
50      * If false, the shift key was not pressed
51      *
52      * @serial
53      * @see #usesShiftModifier()
54      * @since JDK1.1
55      */

56     boolean usesShift;
57
58     /*
59      * JDK 1.1 serialVersionUID
60      */

61      private static final long serialVersionUID = 143448358473180225L;
62
63     /**
64      * Constructs a new MenuShortcut for the specified virtual keycode.
65      * @param key the raw keycode for this MenuShortcut, as would be returned
66      * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
67      * this key were pressed.
68      * @see java.awt.event.KeyEvent
69      **/

70     public MenuShortcut(int key) {
71         this(key, false);
72     }
73
74     /**
75      * Constructs a new MenuShortcut for the specified virtual keycode.
76      * @param key the raw keycode for this MenuShortcut, as would be returned
77      * in the keyCode field of a {@link java.awt.event.KeyEvent KeyEvent} if
78      * this key were pressed.
79      * @param useShiftModifier indicates whether this MenuShortcut is invoked
80      * with the SHIFT key down.
81      * @see java.awt.event.KeyEvent
82      **/

83     public MenuShortcut(int key, boolean useShiftModifier) {
84         this.key = key;
85         this.usesShift = useShiftModifier;
86     }
87
88     /**
89      * Returns the raw keycode of this MenuShortcut.
90      * @return the raw keycode of this MenuShortcut.
91      * @see java.awt.event.KeyEvent
92      * @since JDK1.1
93      */

94     public int getKey() {
95         return key;
96     }
97
98     /**
99      * Returns whether this MenuShortcut must be invoked using the SHIFT key.
100      * @return <code>true</code> if this MenuShortcut must be invoked using the
101      * SHIFT key, <code>false</code> otherwise.
102      * @since JDK1.1
103      */

104     public boolean usesShiftModifier() {
105         return usesShift;
106     }
107
108     /**
109      * Returns whether this MenuShortcut is the same as another:
110      * equality is defined to mean that both MenuShortcuts use the same key
111      * and both either use or don't use the SHIFT key.
112      * @param s the MenuShortcut to compare with this.
113      * @return <code>true</code> if this MenuShortcut is the same as another,
114      * <code>false</code> otherwise.
115      * @since JDK1.1
116      */

117     public boolean equals(MenuShortcut JavaDoc s) {
118     return (s != null && (s.getKey() == key) &&
119                 (s.usesShiftModifier() == usesShift));
120     }
121
122     /**
123      * Returns whether this MenuShortcut is the same as another:
124      * equality is defined to mean that both MenuShortcuts use the same key
125      * and both either use or don't use the SHIFT key.
126      * @param obj the Object to compare with this.
127      * @return <code>true</code> if this MenuShortcut is the same as another,
128      * <code>false</code> otherwise.
129      * @since 1.2
130      */

131     public boolean equals(Object JavaDoc obj) {
132         if (obj instanceof MenuShortcut JavaDoc) {
133             return equals( (MenuShortcut JavaDoc) obj );
134         }
135         return false;
136     }
137
138     /**
139      * Returns the hashcode for this MenuShortcut.
140      * @return the hashcode for this MenuShortcut.
141      * @since 1.2
142      */

143     public int hashCode() {
144         return (usesShift) ? (~key) : key;
145     }
146
147     /**
148      * Returns an internationalized description of the MenuShortcut.
149      * @return a string representation of this MenuShortcut.
150      * @since JDK1.1
151      */

152     public String JavaDoc toString() {
153         int modifiers = 0;
154         if (!GraphicsEnvironment.isHeadless()) {
155             modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
156     }
157         if (usesShiftModifier()) {
158             modifiers |= Event.SHIFT_MASK;
159         }
160     return KeyEvent.getKeyModifiersText(modifiers) + "+" +
161                KeyEvent.getKeyText(key);
162     }
163
164     /**
165      * Returns the parameter string representing the state of this
166      * MenuShortcut. This string is useful for debugging.
167      * @return the parameter string of this MenuShortcut.
168      * @since JDK1.1
169      */

170     protected String JavaDoc paramString() {
171         String JavaDoc str = "key=" + key;
172     if (usesShiftModifier()) {
173         str += ",usesShiftModifier";
174     }
175     return str;
176     }
177 }
178
Popular Tags