Class Shortcut

java.lang.Object
org.openstreetmap.josm.tools.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 Details

    • shortText

      private final String shortText
      the unique ID of the shortcut
    • 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
    • assignedModifier

      private int assignedModifier
      the modifiers that are 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)
    • shortcuts

      private static final Shortcut.ShortcutCollection shortcuts
    • groups

      private static final Map<Integer,Integer> groups
    • NONE

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

      public static final int MNEMONIC
      See Also:
    • RESERVED

      public static final int RESERVED
      Reserved group: for system shortcuts only
      See Also:
    • DIRECT

      public static final int DIRECT
      Direct group: no modifier
      See Also:
    • ALT

      public static final int ALT
      Alt group
      See Also:
    • SHIFT

      public static final int SHIFT
      Shift group
      See Also:
    • CTRL

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

      public static final int ALT_SHIFT
      Alt-Shift group
      See Also:
    • 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:
    • 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:
    • 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:
    • mods

      private static final int[] mods
    • keys

      private static final int[] keys
    • initdone

      private static boolean initdone
  • Constructor Details

    • Shortcut

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

      private Shortcut(String prefString)
  • Method Details

    • getShortText

      public String getShortText()
    • getLongText

      public String getLongText()
    • setLongText

      private void setLongText(String longText)
    • getAssignedKey

      public int getAssignedKey()
    • getAssignedModifier

      public int getAssignedModifier()
    • isAssignedDefault

      public boolean isAssignedDefault()
    • isAssignedUser

      public boolean isAssignedUser()
    • isAutomatic

      public boolean isAutomatic()
    • isChangeable

      public boolean isChangeable()
    • 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

      Use this to register the shortcut with Swing
      Returns:
      the key stroke
    • saveDefault

      private void saveDefault()
    • save

      private boolean save()
    • isSame

      private boolean isSame(int isKey, int isModifier)
    • isEvent

      public boolean isEvent(KeyEvent e)
    • 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
    • setFocusAccelerator

      public void setFocusAccelerator(JTextComponent component)
      Sets the mnemonic key on a text component.
      Parameters:
      component - component
    • 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
    • toString

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

      public static Optional<Shortcut> findShortcut(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
    • findShortcutByKeyOrShortText

      private static Optional<Shortcut> findShortcutByKeyOrShortText(int requestedKey, int modifier, String shortText)
    • listAll

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

      private static void doInit()
    • getGroupModifier

      private static int getGroupModifier(int group)
    • findModifier

      private static int findModifier(int group, Integer modifier)
    • savePrefs

      public static boolean savePrefs()
      Save shortcuts to preferences
      Returns:
      true if preferences were changed
    • 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<Shortcut> registerMultiShortcuts(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
    • registerShortcut

      private static Shortcut registerShortcut(String shortText, String longText, int requestedKey, int requestedGroup, Integer modifier)
    • findNewOsxModifier

      private static int findNewOsxModifier(int requestedGroup)
    • reassignShortcut

      private static Shortcut reassignShortcut(String shortText, String longText, int requestedKey, Shortcut conflict, int m, int k, int newmodifier)
    • 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
    • getKeyStrokeForShortKey

      private static KeyStroke getKeyStrokeForShortKey(String shortKey)
    • 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