Class Shortcut
- java.lang.Object
-
- org.openstreetmap.josm.tools.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classShortcut.ShortcutCollection
-
Field Summary
Fields Modifier and Type Field Description static intALTAlt groupstatic intALT_CTRLAlt-Command group.static intALT_CTRL_SHIFTAlt-Command-Shift group.static intALT_SHIFTAlt-Shift groupprivate booleanassignedDefaulttrue if it got assigned what was requested.private intassignedKeythe key that actually is usedprivate intassignedModifierthe modifiers that are usedprivate booleanassignedUsertrue if the user changed this shortcutprivate booleanautomatictrue if the user cannot change this shortcut (Note: it also will not be saved into the preferences)static intCTRLCommand group.static intCTRL_SHIFTCommand-Shift group.static intDIRECTDirect group: no modifierprivate static java.util.Map<java.lang.Integer,java.lang.Integer>groupsprivate static booleaninitdoneprivate static int[]keysprivate java.lang.StringlongTexta human readable description that will be shown in the preferencesstatic intMNEMONICprivate static int[]modsstatic intNONENone group: used with KeyEvent.CHAR_UNDEFINED if no shortcut is definedprivate intrequestedGroupthe group, the caller requestedprivate intrequestedKeythe key, the caller requestedstatic intRESERVEDReserved group: for system shortcuts onlyprivate booleanresettrue 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 intSHIFTShift groupprivate static Shortcut.ShortcutCollectionshortcutsprivate java.lang.StringshortTextthe unique ID of the shortcut
-
Constructor Summary
Constructors Modifier Constructor Description privateShortcut(java.lang.String prefString)privateShortcut(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 voiddoInit()private static intfindModifier(int group, java.lang.Integer modifier)private static intfindNewOsxModifier(int requestedGroup)static java.util.Optional<Shortcut>findShortcut(int requestedKey, int modifier)Returns the registered shortcut fot the key and modifierprivate static java.util.Optional<Shortcut>findShortcutByKeyOrShortText(int requestedKey, int modifier, java.lang.String shortText)intgetAssignedKey()intgetAssignedModifier()static javax.swing.KeyStrokegetCopyKeyStroke()Replies the platform specific key stroke for the 'Copy' command, i.e.static javax.swing.KeyStrokegetCutKeyStroke()Replies the platform specific key stroke for the 'Cut' command, i.e.private static intgetGroupModifier(int group)javax.swing.KeyStrokegetKeyStroke()Use this to register the shortcut with Swingprivate static javax.swing.KeyStrokegetKeyStrokeForShortKey(java.lang.String shortKey)java.lang.StringgetKeyText()Returns a human readable text for the shortcut.static java.lang.StringgetKeyText(javax.swing.KeyStroke keyStroke)Returns a human readable text for the key stroke.java.lang.StringgetLongText()static javax.swing.KeyStrokegetPasteKeyStroke()Replies the platform specific key stroke for the 'Paste' command, i.e.java.lang.StringgetShortText()booleanisAssignedDefault()booleanisAssignedUser()booleanisAutomatic()booleanisChangeable()(package private) static booleanisDeadKey(int keyCode)booleanisEvent(java.awt.event.KeyEvent e)private booleanisReset()private booleanisSame(int isKey, int isModifier)static java.util.List<Shortcut>listAll()Returns a list of all shortcuts.static java.lang.StringmakeTooltip(java.lang.String tooltip, javax.swing.KeyStroke keyStroke)Returns the tooltip text plus the key stroke text.private static ShortcutreassignShortcut(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 ShortcutregisterShortcut(java.lang.String shortText, java.lang.String longText, int requestedKey, int requestedGroup)Register a shortcut.private static ShortcutregisterShortcut(java.lang.String shortText, java.lang.String longText, int requestedKey, int requestedGroup, java.lang.Integer modifier)static ShortcutregisterSystemShortcut(java.lang.String shortText, java.lang.String longText, int key, int modifier)FOR PLATFORMHOOK USE ONLY.private booleansave()private voidsaveDefault()static booleansavePrefs()Save shortcuts to preferencesvoidsetAccelerator(javax.swing.AbstractAction action)use this to set a actions's acceleratorvoidsetAssignedKey(int assignedKey)FOR PREF PANE ONLY.voidsetAssignedModifier(int assignedModifier)FOR PREF PANE ONLY.voidsetAssignedUser(boolean assignedUser)FOR PREF PANE ONLY.voidsetAutomatic()FOR PREF PANE ONLYvoidsetFocusAccelerator(javax.swing.text.JTextComponent component)Sets the mnemonic key on a text component.private voidsetLongText(java.lang.String longText)voidsetMnemonic(javax.swing.AbstractButton button)use this to set a buttons's mnemonicvoidsetMnemonic(javax.swing.JMenu menu)use this to set a menu's mnemonicvoidsetTooltip(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 voidsetTooltip(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.StringtoString()
-
-
-
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
-
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 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
-
MNEMONIC
public static final int MNEMONIC
- See Also:
- Constant Field Values
-
RESERVED
public static final int RESERVED
Reserved group: for system shortcuts only- See Also:
- Constant Field Values
-
DIRECT
public static final int DIRECT
Direct group: no modifier- See Also:
- Constant Field Values
-
ALT
public static final int ALT
Alt group- See Also:
- Constant Field Values
-
SHIFT
public static final int SHIFT
Shift group- 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_SHIFT
public static final int ALT_SHIFT
Alt-Shift group- 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
-
-
Method Detail
-
getShortText
public java.lang.String getShortText()
-
getLongText
public java.lang.String getLongText()
-
setLongText
private void setLongText(java.lang.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-trueif the user has changed this shortcut
-
getKeyStroke
public javax.swing.KeyStroke 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(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- actiontooltip- 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- actiontooltip- Tooltip text to displaykeyStroke- Key stroke associated (to display accelerator between parenthesis)- Since:
- 14689
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
findShortcut
public static java.util.Optional<Shortcut> findShortcut(int requestedKey, int modifier)
Returns the registered shortcut fot the key and modifier- Parameters:
requestedKey- the requested keymodifier- the modifier- Returns:
- an
Optionalregistered shortcut, nevernull
-
findShortcutByKeyOrShortText
private static java.util.Optional<Shortcut> findShortcutByKeyOrShortText(int requestedKey, int modifier, java.lang.String shortText)
-
listAll
public static java.util.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, java.lang.Integer modifier)
-
savePrefs
public static boolean savePrefs()
Save shortcuts to preferences- Returns:
trueif 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 aKeyEvent.VK_*constant here.modifier- the modifier. Use aKeyEvent.*_MASKconstant here.- Returns:
- the system shortcut
-
registerMultiShortcuts
public 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.- 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 preferrequestedGroup- 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 aKeyEvent.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)
-
findNewOsxModifier
private static int findNewOsxModifier(int requestedGroup)
-
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 displaykeyStroke- Key stroke associated (to display accelerator between parenthesis)- Returns:
- Full tooltip text (tooltip + accelerator)
- Since:
- 14689
-
-