Class Shortcut


  • public final class Shortcut
    extends java.lang.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 Summary

      Fields 
      Modifier and Type Field Description
      static int ALT
      Alt group
      static int ALT_CTRL
      Alt-Command group.
      static int ALT_CTRL_SHIFT
      Alt-Command-Shift group.
      static int ALT_SHIFT
      Alt-Shift group
      private boolean assignedDefault
      true if it got assigned what was requested.
      private int assignedKey
      the key that actually is used
      private int assignedModifier
      the modifiers that are used
      private boolean assignedUser
      true if the user changed this shortcut
      private boolean automatic
      true if the user cannot change this shortcut (Note: it also will not be saved into the preferences)
      static int CTRL
      Command group.
      static int CTRL_SHIFT
      Command-Shift group.
      static int DIRECT
      Direct group: no modifier
      private static java.util.Map<java.lang.Integer,​java.lang.Integer> groups  
      private static boolean initdone  
      private static int[] keys  
      private java.lang.String longText
      a human readable description that will be shown in the preferences
      static int MNEMONIC  
      private static int[] mods  
      static int NONE
      None group: used with KeyEvent.CHAR_UNDEFINED if no shortcut is defined
      private int requestedGroup
      the group, the caller requested
      private int requestedKey
      the key, the caller requested
      static int RESERVED
      Reserved group: for system shortcuts only
      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)
      static int SHIFT
      Shift group
      private static Shortcut.ShortcutCollection shortcuts  
      private java.lang.String shortText
      the unique ID of the shortcut
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Shortcut​(java.lang.String prefString)  
      private Shortcut​(java.lang.String shortText, java.lang.String longText, int requestedKey, int requestedGroup, int assignedKey, int assignedModifier, boolean assignedDefault, boolean assignedUser)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static void doInit()  
      private static int findModifier​(int group, java.lang.Integer modifier)  
      private static int findNewOsxModifier​(int requestedGroup)  
      static java.util.Optional<Shortcut> findShortcut​(int requestedKey, int modifier)
      Returns the registered shortcut fot the key and modifier
      private static java.util.Optional<Shortcut> findShortcutByKeyOrShortText​(int requestedKey, int modifier, java.lang.String shortText)  
      int getAssignedKey()  
      int getAssignedModifier()  
      static javax.swing.KeyStroke getCopyKeyStroke()
      Replies the platform specific key stroke for the 'Copy' command, i.e.
      static javax.swing.KeyStroke getCutKeyStroke()
      Replies the platform specific key stroke for the 'Cut' command, i.e.
      private static int getGroupModifier​(int group)  
      javax.swing.KeyStroke getKeyStroke()
      Use this to register the shortcut with Swing
      private static javax.swing.KeyStroke getKeyStrokeForShortKey​(java.lang.String shortKey)  
      java.lang.String getKeyText()
      Returns a human readable text for the shortcut.
      static java.lang.String getKeyText​(javax.swing.KeyStroke keyStroke)
      Returns a human readable text for the key stroke.
      java.lang.String getLongText()  
      static javax.swing.KeyStroke getPasteKeyStroke()
      Replies the platform specific key stroke for the 'Paste' command, i.e.
      java.lang.String getShortText()  
      boolean isAssignedDefault()  
      boolean isAssignedUser()  
      boolean isAutomatic()  
      boolean isChangeable()  
      (package private) static boolean isDeadKey​(int keyCode)  
      boolean isEvent​(java.awt.event.KeyEvent e)  
      private boolean isReset()  
      private boolean isSame​(int isKey, int isModifier)  
      static java.util.List<Shortcut> listAll()
      Returns a list of all shortcuts.
      static java.lang.String makeTooltip​(java.lang.String tooltip, javax.swing.KeyStroke keyStroke)
      Returns the tooltip text plus the key stroke text.
      private static Shortcut reassignShortcut​(java.lang.String shortText, java.lang.String longText, int requestedKey, Shortcut conflict, int m, int k, int newmodifier)  
      static java.util.List<Shortcut> registerMultiShortcuts​(java.lang.String shortText, java.lang.String longText, java.util.List<java.lang.Character> characters, int requestedGroup)
      Register a shortcut linked to several characters.
      static Shortcut registerShortcut​(java.lang.String shortText, java.lang.String longText, int requestedKey, int requestedGroup)
      Register a shortcut.
      private static Shortcut registerShortcut​(java.lang.String shortText, java.lang.String longText, int requestedKey, int requestedGroup, java.lang.Integer modifier)  
      static Shortcut registerSystemShortcut​(java.lang.String shortText, java.lang.String longText, int key, int modifier)
      FOR PLATFORMHOOK USE ONLY.
      private boolean save()  
      private void saveDefault()  
      static boolean savePrefs()
      Save shortcuts to preferences
      void setAccelerator​(javax.swing.AbstractAction action)
      use this to set a actions's accelerator
      void setAssignedKey​(int assignedKey)
      FOR PREF PANE ONLY.
      void setAssignedModifier​(int assignedModifier)
      FOR PREF PANE ONLY.
      void setAssignedUser​(boolean assignedUser)
      FOR PREF PANE ONLY.
      void setAutomatic()
      FOR PREF PANE ONLY
      void setFocusAccelerator​(javax.swing.text.JTextComponent component)
      Sets the mnemonic key on a text component.
      private void setLongText​(java.lang.String longText)  
      void setMnemonic​(javax.swing.AbstractButton button)
      use this to set a buttons's mnemonic
      void setMnemonic​(javax.swing.JMenu menu)
      use this to set a menu's mnemonic
      void setTooltip​(javax.swing.Action action, java.lang.String tooltip)
      Sets the action tooltip to the tooltip text plus the key stroke text this shortcut represents.
      static void setTooltip​(javax.swing.Action action, java.lang.String tooltip, javax.swing.KeyStroke keyStroke)
      Sets the action tooltip to the tooltip text plus the key stroke text.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • shortText

        private final java.lang.String shortText
        the unique ID of the shortcut
      • longText

        private java.lang.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)
      • groups

        private static final java.util.Map<java.lang.Integer,​java.lang.Integer> groups
      • 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​(java.lang.String shortText,
                         java.lang.String longText,
                         int requestedKey,
                         int requestedGroup,
                         int assignedKey,
                         int assignedModifier,
                         boolean assignedDefault,
                         boolean assignedUser)
      • Shortcut

        private Shortcut​(java.lang.String prefString)
    • Method Detail

      • setLongText

        private void setLongText​(java.lang.String longText)
      • 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 javax.swing.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)
      • isEvent

        public boolean isEvent​(java.awt.event.KeyEvent e)
      • setMnemonic

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

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

        public void setFocusAccelerator​(javax.swing.text.JTextComponent component)
        Sets the mnemonic key on a text component.
        Parameters:
        component - component
      • setAccelerator

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

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

        public static java.lang.String getKeyText​(javax.swing.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​(javax.swing.Action action,
                               java.lang.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​(javax.swing.Action action,
                                      java.lang.String tooltip,
                                      javax.swing.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
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • findShortcut

        public static java.util.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 java.util.List<ShortcutlistAll()
        Returns a list of all shortcuts.
        Returns:
        a list of all shortcuts
      • doInit

        private static void doInit()
      • findModifier

        private static int findModifier​(int group,
                                        java.lang.Integer modifier)
      • savePrefs

        public static boolean savePrefs()
        Save shortcuts to preferences
        Returns:
        true if preferences were changed
      • registerSystemShortcut

        public static Shortcut registerSystemShortcut​(java.lang.String shortText,
                                                      java.lang.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 java.util.List<ShortcutregisterMultiShortcuts​(java.lang.String shortText,
                                                                      java.lang.String longText,
                                                                      java.util.List<java.lang.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​(java.lang.String shortText,
                                                java.lang.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
      • registerShortcut

        private static Shortcut registerShortcut​(java.lang.String shortText,
                                                 java.lang.String longText,
                                                 int requestedKey,
                                                 int requestedGroup,
                                                 java.lang.Integer modifier)
      • reassignShortcut

        private static Shortcut reassignShortcut​(java.lang.String shortText,
                                                 java.lang.String longText,
                                                 int requestedKey,
                                                 Shortcut conflict,
                                                 int m,
                                                 int k,
                                                 int newmodifier)
      • getCopyKeyStroke

        public static javax.swing.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 javax.swing.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 javax.swing.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
      • getKeyStrokeForShortKey

        private static javax.swing.KeyStroke getKeyStrokeForShortKey​(java.lang.String shortKey)
      • makeTooltip

        public static java.lang.String makeTooltip​(java.lang.String tooltip,
                                                   javax.swing.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