home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / MenuShortcut.java < prev    next >
Text File  |  1997-05-20  |  4KB  |  115 lines

  1. /*
  2.  * @(#)MenuShortcut.java    1.9 97/01/27
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22. package java.awt;
  23.  
  24. import java.awt.event.KeyEvent;
  25.  
  26. /**
  27.  * A class which represents a keyboard accelerator for a MenuItem.
  28.  *
  29.  * @version 1.9, 01/27/97
  30.  * @author Thomas Ball
  31.  */
  32. public class MenuShortcut implements java.io.Serializable 
  33. {
  34.  
  35.     int key;
  36.     boolean usesShift;
  37.  
  38.     /*
  39.      * JDK 1.1 serialVersionUID 
  40.      */
  41.      private static final long serialVersionUID = 143448358473180225L;
  42.  
  43.     /**
  44.      * Constructs a new MenuShortcut for the specified key.
  45.      * @param key the raw keycode for this MenuShortcut, as would be returned
  46.      * in the keyCode field of a KeyEvent if this key were pressed.
  47.      **/
  48.     public MenuShortcut(int key) {
  49.         this(key, false);
  50.     }
  51.  
  52.     /**
  53.      * Constructs a new MenuShortcut for the specified key.
  54.      * @param key the raw keycode for this MenuShortcut, as would be returned
  55.      * in the keyCode field of a KeyEvent if this key were pressed.
  56.      * @param useShiftModifier indicates whether this MenuShortcut is invoked
  57.      * with the SHIFT key down.
  58.      **/
  59.     public MenuShortcut(int key, boolean useShiftModifier) {
  60.         // Convenience conversion for programmers who confuse key posts with
  61.         // ASCII characters -- do not internationalize!  They *should* be
  62.         // using KeyEvent virtual keys, such as VK_A.
  63.         if (key >= 'a' && key <= 'z') {
  64.             key = (int)Character.toUpperCase((char)key);
  65.         }
  66.         this.key = key;
  67.         this.usesShift = useShiftModifier;
  68.     }
  69.  
  70.     /**
  71.      * Return the raw keycode of this MenuShortcut.
  72.      */
  73.     public int getKey() {
  74.         return key;
  75.     }
  76.  
  77.     /**
  78.      * Return whether this MenuShortcut must be invoked using the SHIFT key.
  79.      */
  80.     public boolean usesShiftModifier() {
  81.         return usesShift;
  82.     }
  83.  
  84.     /**
  85.      * Returns whether this MenuShortcut is the same as another:
  86.      * equality is defined to mean that both MenuShortcuts use the same key
  87.      * and both either use or don't use the SHIFT key.
  88.      * @param s the MenuShortcut to compare with this.
  89.      */
  90.     public boolean equals(MenuShortcut s) {
  91.     return (s != null && (s.getKey() == key) && 
  92.                 (s.usesShiftModifier() == usesShift));
  93.     }
  94.  
  95.     /**
  96.      * Returns an internationalized description of the MenuShortcut.
  97.      */
  98.     public String toString() {
  99.         int modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
  100.         if (usesShiftModifier()) {
  101.             modifiers |= Event.SHIFT_MASK;
  102.         }
  103.     return KeyEvent.getKeyModifiersText(modifiers) + "+" + 
  104.                KeyEvent.getKeyText(key);
  105.     }
  106.  
  107.     protected String paramString() {
  108.         String str = "key=" + key;
  109.     if (usesShiftModifier()) {
  110.         str += ",usesShiftModifier";
  111.     }
  112.     return str;
  113.     }
  114. }
  115.