Class Shortcut


  • public final class Shortcut
    extends Object
    Global shortcut class. Note: This class represents a single shortcut, contains the factory to obtain shortcut objects from, manages shortcuts and shortcut collisions, and finally manages loading and saving shortcuts to/from the preferences. Action authors: You only need the registerShortcut(java.lang.String, java.lang.String, int, int) factory. Ignore everything else. All: Use only public methods that are also marked to be used. The others are public so the shortcut preferences can use them.
    Since:
    1084
    • Field Detail

      • longText

        private String longText
        a human readable description that will be shown in the preferences
      • requestedKey

        private final int requestedKey
        the key, the caller requested
      • requestedGroup

        private final int requestedGroup
        the group, the caller requested
      • assignedKey

        private int assignedKey
        the key that actually is used
      • assignedDefault

        private boolean assignedDefault
        true if it got assigned what was requested. (Note: modifiers will be ignored in favour of group when loading it from the preferences then.)
      • assignedUser

        private boolean assignedUser
        true if the user changed this shortcut
      • automatic

        private boolean automatic
        true if the user cannot change this shortcut (Note: it also will not be saved into the preferences)
      • reset

        private boolean reset
        true if the user requested this shortcut to be set to its default value (will happen on next restart, as this shortcut will not be saved to the preferences)
      • NONE

        public static final int NONE
        None group: used with KeyEvent.CHAR_UNDEFINED if no shortcut is defined
        See Also:
        Constant Field Values
      • CTRL

        public static final int CTRL
        Command group. Matches CTRL modifier on Windows/Linux but META modifier on OS X
        See Also:
        Constant Field Values
      • ALT_CTRL

        public static final int ALT_CTRL
        Alt-Command group. Matches ALT-CTRL modifier on Windows/Linux but ALT-META modifier on OS X
        See Also:
        Constant Field Values
      • CTRL_SHIFT

        public static final int CTRL_SHIFT
        Command-Shift group. Matches CTRL-SHIFT modifier on Windows/Linux but META-SHIFT modifier on OS X
        See Also:
        Constant Field Values
      • ALT_CTRL_SHIFT

        public static final int ALT_CTRL_SHIFT
        Alt-Command-Shift group. Matches ALT-CTRL-SHIFT modifier on Windows/Linux but ALT-META-SHIFT modifier on OS X
        See Also:
        Constant Field Values
      • mods

        private static final int[] mods
      • keys

        private static final int[] keys
      • initdone

        private static boolean initdone
    • Constructor Detail

      • Shortcut

        private Shortcut​(String shortText,
                         String longText,
                         int requestedKey,
                         int requestedGroup,
                         int assignedKey,
                         int assignedModifier,
                         boolean assignedDefault,
                         boolean assignedUser)
    • Method Detail

      • isReset

        private boolean isReset()
      • setAutomatic

        public void setAutomatic()
        FOR PREF PANE ONLY
      • setAssignedModifier

        public void setAssignedModifier​(int assignedModifier)
        FOR PREF PANE ONLY.

        Sets the modifiers that are used.

        Parameters:
        assignedModifier - assigned modifier
      • setAssignedKey

        public void setAssignedKey​(int assignedKey)
        FOR PREF PANE ONLY.

        Sets the key that actually is used.

        Parameters:
        assignedKey - assigned key
      • setAssignedUser

        public void setAssignedUser​(boolean assignedUser)
        FOR PREF PANE ONLY.

        Sets whether the user has changed this shortcut.

        Parameters:
        assignedUser - true if the user has changed this shortcut
      • getKeyStroke

        public KeyStroke getKeyStroke()
        Use this to register the shortcut with Swing
        Returns:
        the key stroke
      • save

        private boolean save()
      • isSame

        private boolean isSame​(int isKey,
                               int isModifier)
      • setMnemonic

        public void setMnemonic​(JMenu menu)
        use this to set a menu's mnemonic
        Parameters:
        menu - menu
      • setMnemonic

        public void setMnemonic​(AbstractButton button)
        use this to set a buttons's mnemonic
        Parameters:
        button - button
      • setAccelerator

        public void setAccelerator​(AbstractAction action)
        use this to set a actions's accelerator
        Parameters:
        action - action
      • getKeyText

        public String getKeyText()
        Returns a human readable text for the shortcut.
        Returns:
        a human readable text for the shortcut
      • getKeyText

        public static String getKeyText​(KeyStroke keyStroke)
        Returns a human readable text for the key stroke.
        Parameters:
        keyStroke - key stroke to convert to human readable text
        Returns:
        a human readable text for the key stroke
        Since:
        12520
      • setTooltip

        public void setTooltip​(Action action,
                               String tooltip)
        Sets the action tooltip to the tooltip text plus the key stroke text this shortcut represents.
        Parameters:
        action - action
        tooltip - Tooltip text to display
        Since:
        14689
      • setTooltip

        public static void setTooltip​(Action action,
                                      String tooltip,
                                      KeyStroke keyStroke)
        Sets the action tooltip to the tooltip text plus the key stroke text.
        Parameters:
        action - action
        tooltip - Tooltip text to display
        keyStroke - Key stroke associated (to display accelerator between parenthesis)
        Since:
        14689
      • findShortcut

        public static Optional<ShortcutfindShortcut​(int requestedKey,
                                                      int modifier)
        Returns the registered shortcut fot the key and modifier
        Parameters:
        requestedKey - the requested key
        modifier - the modifier
        Returns:
        an Optional registered shortcut, never null
      • listAll

        public static List<ShortcutlistAll()
        Returns a list of all shortcuts.
        Returns:
        a list of all shortcuts
      • doInit

        private static void doInit()
      • savePrefs

        public static boolean savePrefs()
      • registerSystemShortcut

        public static Shortcut registerSystemShortcut​(String shortText,
                                                      String longText,
                                                      int key,
                                                      int modifier)
        FOR PLATFORMHOOK USE ONLY.

        This registers a system shortcut. See PlatformHook for details.

        Parameters:
        shortText - an ID. re-use a "system:*" ID if possible, else use something unique.
        longText - this will be displayed in the shortcut preferences dialog. Better use something the user will recognize...
        key - the key. Use a KeyEvent.VK_* constant here.
        modifier - the modifier. Use a KeyEvent.*_MASK constant here.
        Returns:
        the system shortcut
      • registerMultiShortcuts

        public static List<ShortcutregisterMultiShortcuts​(String shortText,
                                                            String longText,
                                                            List<Character> characters,
                                                            int requestedGroup)
        Register a shortcut linked to several characters.
        Parameters:
        shortText - an ID. re-use a "system:*" ID if possible, else use something unique. "menu:*" is reserved for menu mnemonics, "core:*" is reserved for actions that are part of JOSM's core. Use something like <pluginname>+":"+<actionname>.
        longText - this will be displayed in the shortcut preferences dialog. Better use something the user will recognize...
        characters - the characters you'd prefer
        requestedGroup - the group this shortcut fits best. This will determine the modifiers your shortcut will get assigned. Use the constants defined above.
        Returns:
        the shortcut
      • isDeadKey

        static boolean isDeadKey​(int keyCode)
      • registerShortcut

        public static Shortcut registerShortcut​(String shortText,
                                                String longText,
                                                int requestedKey,
                                                int requestedGroup)
        Register a shortcut. Here you get your shortcuts from. The parameters are:
        Parameters:
        shortText - an ID. re-use a "system:*" ID if possible, else use something unique. "menu:*" is reserved for menu mnemonics, "core:*" is reserved for actions that are part of JOSM's core. Use something like <pluginname>+":"+<actionname>.
        longText - this will be displayed in the shortcut preferences dialog. Better use something the user will recognize...
        requestedKey - the key you'd prefer. Use a KeyEvent.VK_* constant here.
        requestedGroup - the group this shortcut fits best. This will determine the modifiers your shortcut will get assigned. Use the constants defined above.
        Returns:
        the shortcut
      • getCopyKeyStroke

        public static KeyStroke getCopyKeyStroke()
        Replies the platform specific key stroke for the 'Copy' command, i.e. 'Ctrl-C' on windows or 'Meta-C' on a Mac. null, if the platform specific copy command isn't known.
        Returns:
        the platform specific key stroke for the 'Copy' command
      • getPasteKeyStroke

        public static KeyStroke getPasteKeyStroke()
        Replies the platform specific key stroke for the 'Paste' command, i.e. 'Ctrl-V' on windows or 'Meta-V' on a Mac. null, if the platform specific paste command isn't known.
        Returns:
        the platform specific key stroke for the 'Paste' command
      • getCutKeyStroke

        public static KeyStroke getCutKeyStroke()
        Replies the platform specific key stroke for the 'Cut' command, i.e. 'Ctrl-X' on windows or 'Meta-X' on a Mac. null, if the platform specific 'Cut' command isn't known.
        Returns:
        the platform specific key stroke for the 'Cut' command
      • makeTooltip

        public static String makeTooltip​(String tooltip,
                                         KeyStroke keyStroke)
        Returns the tooltip text plus the key stroke text. Tooltips are usually not system dependent, unless the JVM is too dumb to provide correct names for all the keys. Some LAFs don't understand HTML, such as the OSX LAFs.
        Parameters:
        tooltip - Tooltip text to display
        keyStroke - Key stroke associated (to display accelerator between parenthesis)
        Returns:
        Full tooltip text (tooltip + accelerator)
        Since:
        14689