001/* MenuShortcut.java -- A class for menu accelerators
002   Copyright (C) 1999, 2002 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package java.awt;
040
041/**
042  * This class implements a keyboard accelerator for a menu item.
043  *
044  * @author Aaron M. Renn (arenn@urbanophile.com)
045  */
046public class MenuShortcut implements java.io.Serializable
047{
048
049/*
050 * Static Variables
051 */
052
053// Serialization Constant
054private static final long serialVersionUID = 143448358473180225L;
055
056/*************************************************************************/
057
058/*
059 * Instance Variables
060 */
061
062/**
063  * @serial The virtual keycode for the shortcut.
064  */
065private int key;
066
067/**
068  * @serial <code>true</code> if the shift key was used with this shortcut,
069  * or <code>false</code> otherwise.
070  */
071private boolean usesShift;
072
073private String keyName;
074
075/*************************************************************************/
076
077/**
078  * Initializes a new instance of <code>MenuShortcut</code> with the
079  * specified virtual key value.
080  *
081  * @param key The virtual keycode for the shortcut.
082  */
083public
084MenuShortcut(int key)
085{
086  this(key, false);
087}
088
089/*************************************************************************/
090
091/**
092  * Initializes a new instance of <code>MenuShortcut</code> with the
093  * specified virtual key value and shift setting.
094  *
095  * @param key The virtual keycode for the shortcut.
096  * @param usesShift <code>true</code> if the shift key was pressed,
097  * <code>false</code> otherwise.
098  */
099public
100MenuShortcut(int key, boolean usesShift)
101{
102  this.key = key;
103  this.usesShift = usesShift;
104  setKeyName(key);
105}
106
107/*************************************************************************/
108
109/*
110 * Instance Methods
111 */
112
113/**
114  * Returns the virtual keycode for this shortcut.
115  *
116  * @return The virtual keycode for this shortcut.
117  */
118public int
119getKey()
120{
121  return(key);
122}
123
124/*************************************************************************/
125
126/**
127  * Returns the shift setting for this shortcut.
128  *
129  * @return <code>true</code> if the shift key was pressed, <code>false</code>
130  * otherwise.
131  */
132public boolean
133usesShiftModifier()
134{
135  return(usesShift);
136}
137
138/*************************************************************************/
139
140/**
141  * Tests this object for equality against the specified object.  The two
142  * objects will be considered equal if and only if the specified object
143  * is an instance of <code>MenuShortcut</code> and has the same key value
144  * and shift setting as this object.
145  *
146  * @param obj The object to test for equality against.
147  *
148  * @return <code>true</code> if the two objects are equal, <code>false</code>
149  * otherwise.
150  */
151public boolean
152equals(MenuShortcut obj)
153{
154  if (obj == null)
155    return(false);
156
157  if (obj.key != this.key)
158    return(false);
159
160  if (obj.usesShift != this.usesShift)
161    return(false);
162
163  return(true);
164}
165
166public boolean
167equals(Object obj)
168{
169  if (obj instanceof MenuShortcut)
170    {
171      MenuShortcut ms = (MenuShortcut) obj;
172      return (ms.key == key && ms.usesShift == usesShift);
173    }      
174  return false;
175}
176
177/*************************************************************************/
178
179/**
180  * Returns a string representation of this shortcut.
181  *
182  * @return A string representation of this shortcut.
183  */
184public String
185toString()
186{
187  String temp = "Ctrl+";
188  if (usesShift)
189    temp = temp + "Shift+";
190  temp = temp + keyName;
191  return temp;
192}
193
194public int
195hashCode()
196{
197  // Arbitrary.
198  return key + (usesShift ? 23 : 57);
199}
200
201/*************************************************************************/
202
203/**
204  * Returns a debugging string for this object.
205  *
206  * @return A debugging string for this object.
207  */
208protected String
209paramString()
210{
211  return "key=" + key + ",usesShift=" + usesShift;
212}
213
214private void 
215setKeyName(int key)
216{
217  if (key == '\n')
218    keyName = "Enter";
219  else if (key == '\b')
220    keyName = "Backspace";
221  else if (key == '\t')
222    keyName = "Tab";
223  else if (key == ' ')
224    keyName = "Space";
225  else if (key == ',')
226    keyName = "Comma";
227  else if (key == '.')
228    keyName = "Period";
229  else if (key == '/')
230    keyName = "Slash";
231  else if (key == '\\')
232    keyName = "Back Slash";
233  else if (key == ';')
234    keyName = "Semicolon";
235  else if (key == '=')
236    keyName = "Equals";
237  else if (key == '[')
238    keyName = "Open Bracket";
239  else if (key == ']')
240    keyName = "Close Bracket";
241  else if (key == '0')
242    keyName = "0";
243  else if (key == '1')
244    keyName = "1";
245  else if (key == '2')
246    keyName = "2";
247  else if (key == '3')
248    keyName = "3";
249  else if (key == '4')
250    keyName = "4";
251  else if (key == '5')
252    keyName = "5";
253  else if (key == '6')
254    keyName = "6";
255  else if (key == '7')
256    keyName = "7";
257  else if (key == '8')
258    keyName = "8";
259  else if (key == '9')
260    keyName = "9";
261  else if (key == 'A')
262    keyName = "A";
263  else if (key == 'B')
264    keyName = "B";
265  else if (key == 'C')
266    keyName = "C";
267  else if (key == 'D')
268    keyName = "D";
269  else if (key == 'E')
270    keyName = "E";
271  else if (key == 'F')
272    keyName = "F";
273  else if (key == 'G')
274    keyName = "G";
275  else if (key == 'H')
276    keyName = "H";
277  else if (key == 'I')
278    keyName = "I";
279  else if (key == 'J')
280    keyName = "J";
281  else if (key == 'K')
282    keyName = "K";
283  else if (key == 'L')
284    keyName = "L";
285  else if (key == 'M')
286    keyName = "M";
287  else if (key == 'N')
288    keyName = "N";
289  else if (key == 'O')
290    keyName = "O";
291  else if (key == 'P')
292    keyName = "P";
293  else if (key == 'Q')
294    keyName = "Q";
295  else if (key == 'R')
296    keyName = "R";
297  else if (key == 'S')
298    keyName = "S";
299  else if (key == 'T')
300    keyName = "T";
301  else if (key == 'U')
302    keyName = "U";
303  else if (key == 'V')
304    keyName = "V";
305  else if (key == 'W')
306    keyName = "W";
307  else if (key == 'X')
308    keyName = "X";
309  else if (key == 'Y')
310    keyName = "Y";
311  else if (key == 'Z')
312    keyName = "Z";
313  else if (key == 3)
314    keyName = "Cancel";
315  else if (key == 12)
316    keyName = "Clear";
317  else if (key == 16)
318    keyName = "Shift";
319  else if (key == 17)
320    keyName = "Ctrl";
321  else if (key == 18)
322    keyName = "Alt";
323  else if (key == 19)
324    keyName = "Pause";
325  else if (key == 20)
326    keyName = "Caps Lock";
327  else if (key == 21)
328    keyName = "Kana";
329  else if (key == 24)
330    keyName = "Final";
331  else if (key == 25)
332    keyName = "Kanji";
333  else if (key == 27)
334    keyName = "Escape";
335  else if (key == 28)
336    keyName = "Convert";
337  else if (key == 29)
338    keyName = "No Convert";
339  else if (key == 30)
340    keyName = "Accept";
341  else if (key == 31)
342    keyName = "Mode Change";
343  else if (key == 33)
344    keyName = "Page Up";
345  else if (key == 34)
346    keyName = "Page Down";
347  else if (key == 35)
348    keyName = "End";
349  else if (key == 36)
350    keyName = "Home";
351  else if (key == 37)
352    keyName = "Left";
353  else if (key == 38)
354    keyName = "Up";
355  else if (key == 39)
356    keyName = "Right";
357  else if (key == 40)
358    keyName = "Down";
359  else if (key == 96)
360    keyName = "NumPad-0";
361  else if (key == 97)
362    keyName = "NumPad-1";
363  else if (key == 98)
364    keyName = "NumPad-2";
365  else if (key == 99)
366    keyName = "NumPad-3";
367  else if (key == 100)
368    keyName = "NumPad-4";
369  else if (key == 101)
370    keyName = "NumPad-5";
371  else if (key == 102)
372    keyName = "NumPad-6";
373  else if (key == 103)
374    keyName = "NumPad-7";
375  else if (key == 104)
376    keyName = "NumPad-8";
377  else if (key == 105)
378    keyName = "NumPad-9";
379  else if (key == 106)
380    keyName = "NumPad *";
381  else if (key == 107)
382    keyName = "NumPad +";
383  else if (key == 108)
384    keyName = "NumPad ,";
385  else if (key == 109)
386    keyName = "NumPad -";
387  else if (key == 110)
388    keyName = "NumPad .";
389  else if (key == 111)
390    keyName = "NumPad /";
391  else if (key == 112)
392    keyName = "F1";
393  else if (key == 113)
394    keyName = "F2";
395  else if (key == 114)
396    keyName = "F3";
397  else if (key == 115)
398    keyName = "F4";
399  else if (key == 116)
400    keyName = "F5";
401  else if (key == 117)
402    keyName = "F6";
403  else if (key == 118)
404    keyName = "F7";
405  else if (key == 119)
406    keyName = "F8";
407  else if (key == 120)
408    keyName = "F9";
409  else if (key == 121)
410    keyName = "F10";
411  else if (key == 122)
412    keyName = "F11";
413  else if (key == 123)
414    keyName = "F12";
415  else if (key == 127)
416    keyName = "Delete";
417  else if (key == 144)
418    keyName = "Num Lock";
419  else if (key == 145)
420    keyName = "Scroll Lock";
421  else if (key == 154)
422    keyName = "Print Screen";
423  else if (key == 155)
424    keyName = "Insert";
425  else if (key == 156)
426    keyName = "Help";
427  else if (key == 157)
428    keyName = "Meta";
429  else if (key == 192)
430    keyName = "Back Quote";
431  else if (key == 222)
432    keyName = "Quote";
433}
434} // class MenuShortcut