Source for java.awt.MenuShortcut

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