com.alee.managers.hotkey
Class HotkeyManager

java.lang.Object
  extended by com.alee.managers.hotkey.HotkeyManager

public class HotkeyManager
extends java.lang.Object

This manager allows you to quickly register global hotkeys (like accelerators on menu items in menubar menus) for any Swing component. Additionally you can specify a component which will limit hotkey events to its area (meaning that hotkey event will occur only if this component or any of its childs is focused when hotkey pressed).

TooltipManager is integrated with this manager to automatically show component hotkeys in its tooltip if needed/allowed by tooltip and hotkey settings.

All hotkeys are stored into WeakHashMap so hotkeys will be removed as soon as the component for which hotkey is registered gets finalized. HotkeyInfo also keeps a weak reference to both top and hotkey components.

Author:
Mikle Garin

Field Summary
static java.lang.String COMPONENT_HOTKEYS_LIST_KEY
          Keys used to store custom data in JComponent.
static java.lang.String CONTAINER_HOTKEY_CONDITIONS_LIST_KEY
           
protected static java.util.Map<javax.swing.JComponent,java.lang.ref.WeakReference<java.util.List<HotkeyCondition>>> containerConditions
          Conditions for top components which might.
protected static java.util.List<HotkeyInfo> globalHotkeys
          Global hotkeys list.
protected static Filter<HotkeyInfo> HOTKEY_DISPLAY_FILTER
          Displayed hotkeys filter.
protected static TextProvider<HotkeyInfo> HOTKEY_TEXT_PROVIDER
          HotkeyInfo text provider.
protected static java.util.Map<javax.swing.JComponent,java.lang.ref.WeakReference<java.util.List<HotkeyInfo>>> hotkeys
          Added hotkeys.
protected static java.lang.String HOTKEYS_SEPARATOR
          Separator used between multiply hotkeys displayed in a single line.
protected static boolean hotkeysEnabled
          Global hotkeys block flag.
protected static boolean initialized
          Initialization mark.
protected static java.lang.Object sync
          Synchronization object.
protected static boolean transferFocus
          Pass focus to fired hotkey component.
 
Constructor Summary
HotkeyManager()
           
 
Method Summary
static void addContainerHotkeyCondition(javax.swing.JComponent container, HotkeyCondition hotkeyCondition)
          Sets top component additional hotkey trigger condition
protected static void cacheHotkey(HotkeyInfo hotkeyInfo)
          Hotkeys cache methods
protected static void clearHotkeyCache(HotkeyInfo hotkeyInfo)
           
protected static void clearHotkeysCache(javax.swing.JComponent component)
           
protected static void clearHotkeysCache(java.util.List<HotkeyInfo> hotkeysInfo)
           
protected static java.util.Map<java.awt.Component,java.util.List<HotkeyInfo>> copyComponentHotkeys()
          Returns a full copy of hotkeys map.
protected static java.util.Map<javax.swing.JComponent,java.util.List<HotkeyCondition>> copyContainerConditions()
          Returns a full copy of container conditions map.
protected static HotkeyRunnable createAction(javax.swing.AbstractButton forComponent)
           
static void disableHotkeys()
          Global hotkey block
static void enableHotkeys()
           
static java.util.List<HotkeyInfo> getComponentHotkeys(javax.swing.JComponent component)
           
protected static java.util.List<HotkeyInfo> getComponentHotkeysCache(javax.swing.JComponent forComponent)
           
static java.lang.String getComponentHotkeysString(javax.swing.JComponent component)
          All component hotkeys list
static java.util.List<HotkeyCondition> getContainerHotkeyConditions(javax.swing.JComponent container)
           
protected static java.util.List<HotkeyCondition> getContainerHotkeyConditionsCache(javax.swing.JComponent container)
           
protected static boolean hotkeyForEventExists(java.awt.event.KeyEvent keyEvent)
          Returns whether at least one hotkey for the specified key event exists or not.
static void initialize()
          Initializes hotkey manager.
static void installShowAllHotkeysAction(javax.swing.JComponent topComponent)
          Installs "show all hotkeys" action on window or component
static void installShowAllHotkeysAction(javax.swing.JComponent topComponent, HotkeyData hotkeyData)
           
static boolean isTransferFocus()
          Should transfer focus to fired hotkey component or not
protected static boolean meetsParentConditions(java.awt.Component forComponent)
           
protected static void processHotkey(java.awt.event.KeyEvent e, HotkeyInfo hotkeyInfo)
          Processes single hotkey.
protected static void processHotkeys(java.awt.event.KeyEvent e)
          Processes all available registered hotkeys.
static HotkeyInfo registerHotkey(javax.swing.AbstractButton forComponent, HotkeyData hotkeyData)
          Button-specific hotkey register methods
static HotkeyInfo registerHotkey(javax.swing.AbstractButton forComponent, HotkeyData hotkeyData, boolean hidden)
           
static HotkeyInfo registerHotkey(javax.swing.AbstractButton forComponent, HotkeyData hotkeyData, TooltipWay tooltipWay)
           
static HotkeyInfo registerHotkey(java.awt.Component topComponent, javax.swing.AbstractButton forComponent, HotkeyData hotkeyData)
           
static HotkeyInfo registerHotkey(java.awt.Component topComponent, javax.swing.AbstractButton forComponent, HotkeyData hotkeyData, boolean hidden)
           
static HotkeyInfo registerHotkey(java.awt.Component topComponent, javax.swing.AbstractButton forComponent, HotkeyData hotkeyData, TooltipWay tooltipWay)
           
static HotkeyInfo registerHotkey(java.awt.Component topComponent, javax.swing.JComponent forComponent, HotkeyData hotkeyData, HotkeyRunnable action)
           
static HotkeyInfo registerHotkey(java.awt.Component topComponent, javax.swing.JComponent forComponent, HotkeyData hotkeyData, HotkeyRunnable action, boolean hidden)
           
static HotkeyInfo registerHotkey(java.awt.Component topComponent, javax.swing.JComponent forComponent, HotkeyData hotkeyData, HotkeyRunnable action, boolean hidden, TooltipWay tooltipWay)
           
static HotkeyInfo registerHotkey(java.awt.Component topComponent, javax.swing.JComponent forComponent, HotkeyData hotkeyData, HotkeyRunnable action, TooltipWay tooltipWay)
           
static HotkeyInfo registerHotkey(HotkeyData hotkeyData, HotkeyRunnable action)
          Hotkey register methods
static HotkeyInfo registerHotkey(javax.swing.JComponent forComponent, HotkeyData hotkeyData, HotkeyRunnable action)
           
static HotkeyInfo registerHotkey(javax.swing.JComponent forComponent, HotkeyData hotkeyData, HotkeyRunnable action, boolean hidden)
           
static void removeContainerHotkeyCondition(javax.swing.JComponent container, HotkeyCondition hotkeyCondition)
           
static void removeContainerHotkeyConditions(javax.swing.JComponent container)
           
static void removeContainerHotkeyConditions(javax.swing.JComponent container, java.util.List<HotkeyCondition> hotkeyConditions)
           
static void setComponentHotkeyDisplayWay(javax.swing.JComponent component, TooltipWay tooltipWay)
          Sets component hotkey tip way
static void setTransferFocus(boolean transferFocus)
           
static void showComponentHotkeys()
          Shows all visible components hotkeys
static void showComponentHotkeys(java.awt.Component component)
           
protected static void showComponentHotkeys(java.awt.Window window)
           
static void unregisterHotkey(HotkeyInfo hotkeyInfo)
          Hotkey removal methods
static void unregisterHotkeys(javax.swing.JComponent component)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COMPONENT_HOTKEYS_LIST_KEY

public static final java.lang.String COMPONENT_HOTKEYS_LIST_KEY
Keys used to store custom data in JComponent.

See Also:
Constant Field Values

CONTAINER_HOTKEY_CONDITIONS_LIST_KEY

public static final java.lang.String CONTAINER_HOTKEY_CONDITIONS_LIST_KEY
See Also:
Constant Field Values

HOTKEYS_SEPARATOR

protected static final java.lang.String HOTKEYS_SEPARATOR
Separator used between multiply hotkeys displayed in a single line.

See Also:
Constant Field Values

HOTKEY_TEXT_PROVIDER

protected static final TextProvider<HotkeyInfo> HOTKEY_TEXT_PROVIDER
HotkeyInfo text provider.


HOTKEY_DISPLAY_FILTER

protected static final Filter<HotkeyInfo> HOTKEY_DISPLAY_FILTER
Displayed hotkeys filter.


sync

protected static final java.lang.Object sync
Synchronization object.


hotkeysEnabled

protected static boolean hotkeysEnabled
Global hotkeys block flag.


transferFocus

protected static boolean transferFocus
Pass focus to fired hotkey component.


hotkeys

protected static java.util.Map<javax.swing.JComponent,java.lang.ref.WeakReference<java.util.List<HotkeyInfo>>> hotkeys
Added hotkeys.


globalHotkeys

protected static java.util.List<HotkeyInfo> globalHotkeys
Global hotkeys list.


containerConditions

protected static java.util.Map<javax.swing.JComponent,java.lang.ref.WeakReference<java.util.List<HotkeyCondition>>> containerConditions
Conditions for top components which might.


initialized

protected static boolean initialized
Initialization mark.

Constructor Detail

HotkeyManager

public HotkeyManager()
Method Detail

initialize

public static void initialize()
Initializes hotkey manager.


copyComponentHotkeys

protected static java.util.Map<java.awt.Component,java.util.List<HotkeyInfo>> copyComponentHotkeys()
Returns a full copy of hotkeys map. Returned map is a HashMap instead of WeakHashMap used in manager and will keep stong references to hotkey components.

Returns:
full copy of hotkeys map

copyContainerConditions

protected static java.util.Map<javax.swing.JComponent,java.util.List<HotkeyCondition>> copyContainerConditions()
Returns a full copy of container conditions map. Returned map is a HashMap instead of WeakHashMap used in manager and will keep stong references to condition containers.

Returns:
full copy of container conditions map

hotkeyForEventExists

protected static boolean hotkeyForEventExists(java.awt.event.KeyEvent keyEvent)
Returns whether at least one hotkey for the specified key event exists or not.

Parameters:
keyEvent - key event to search hotkeys for
Returns:
true if at least one hotkey for the specified key event exists, false otherwise

processHotkeys

protected static void processHotkeys(java.awt.event.KeyEvent e)
Processes all available registered hotkeys.

Parameters:
e - key event

processHotkey

protected static void processHotkey(java.awt.event.KeyEvent e,
                                    HotkeyInfo hotkeyInfo)
Processes single hotkey.

Parameters:
e - key event
hotkeyInfo - hotkey information

meetsParentConditions

protected static boolean meetsParentConditions(java.awt.Component forComponent)

registerHotkey

public static HotkeyInfo registerHotkey(HotkeyData hotkeyData,
                                        HotkeyRunnable action)
Hotkey register methods


registerHotkey

public static HotkeyInfo registerHotkey(javax.swing.JComponent forComponent,
                                        HotkeyData hotkeyData,
                                        HotkeyRunnable action)

registerHotkey

public static HotkeyInfo registerHotkey(javax.swing.JComponent forComponent,
                                        HotkeyData hotkeyData,
                                        HotkeyRunnable action,
                                        boolean hidden)

registerHotkey

public static HotkeyInfo registerHotkey(java.awt.Component topComponent,
                                        javax.swing.JComponent forComponent,
                                        HotkeyData hotkeyData,
                                        HotkeyRunnable action)

registerHotkey

public static HotkeyInfo registerHotkey(java.awt.Component topComponent,
                                        javax.swing.JComponent forComponent,
                                        HotkeyData hotkeyData,
                                        HotkeyRunnable action,
                                        TooltipWay tooltipWay)

registerHotkey

public static HotkeyInfo registerHotkey(java.awt.Component topComponent,
                                        javax.swing.JComponent forComponent,
                                        HotkeyData hotkeyData,
                                        HotkeyRunnable action,
                                        boolean hidden)

registerHotkey

public static HotkeyInfo registerHotkey(java.awt.Component topComponent,
                                        javax.swing.JComponent forComponent,
                                        HotkeyData hotkeyData,
                                        HotkeyRunnable action,
                                        boolean hidden,
                                        TooltipWay tooltipWay)

registerHotkey

public static HotkeyInfo registerHotkey(javax.swing.AbstractButton forComponent,
                                        HotkeyData hotkeyData)
Button-specific hotkey register methods


registerHotkey

public static HotkeyInfo registerHotkey(javax.swing.AbstractButton forComponent,
                                        HotkeyData hotkeyData,
                                        boolean hidden)

registerHotkey

public static HotkeyInfo registerHotkey(javax.swing.AbstractButton forComponent,
                                        HotkeyData hotkeyData,
                                        TooltipWay tooltipWay)

registerHotkey

public static HotkeyInfo registerHotkey(java.awt.Component topComponent,
                                        javax.swing.AbstractButton forComponent,
                                        HotkeyData hotkeyData)

registerHotkey

public static HotkeyInfo registerHotkey(java.awt.Component topComponent,
                                        javax.swing.AbstractButton forComponent,
                                        HotkeyData hotkeyData,
                                        TooltipWay tooltipWay)

registerHotkey

public static HotkeyInfo registerHotkey(java.awt.Component topComponent,
                                        javax.swing.AbstractButton forComponent,
                                        HotkeyData hotkeyData,
                                        boolean hidden)

createAction

protected static HotkeyRunnable createAction(javax.swing.AbstractButton forComponent)

setComponentHotkeyDisplayWay

public static void setComponentHotkeyDisplayWay(javax.swing.JComponent component,
                                                TooltipWay tooltipWay)
Sets component hotkey tip way


addContainerHotkeyCondition

public static void addContainerHotkeyCondition(javax.swing.JComponent container,
                                               HotkeyCondition hotkeyCondition)
Sets top component additional hotkey trigger condition


removeContainerHotkeyCondition

public static void removeContainerHotkeyCondition(javax.swing.JComponent container,
                                                  HotkeyCondition hotkeyCondition)

removeContainerHotkeyConditions

public static void removeContainerHotkeyConditions(javax.swing.JComponent container,
                                                   java.util.List<HotkeyCondition> hotkeyConditions)

removeContainerHotkeyConditions

public static void removeContainerHotkeyConditions(javax.swing.JComponent container)

getContainerHotkeyConditionsCache

protected static java.util.List<HotkeyCondition> getContainerHotkeyConditionsCache(javax.swing.JComponent container)

getContainerHotkeyConditions

public static java.util.List<HotkeyCondition> getContainerHotkeyConditions(javax.swing.JComponent container)

unregisterHotkey

public static void unregisterHotkey(HotkeyInfo hotkeyInfo)
Hotkey removal methods


unregisterHotkeys

public static void unregisterHotkeys(javax.swing.JComponent component)

cacheHotkey

protected static void cacheHotkey(HotkeyInfo hotkeyInfo)
Hotkeys cache methods


clearHotkeyCache

protected static void clearHotkeyCache(HotkeyInfo hotkeyInfo)

clearHotkeysCache

protected static void clearHotkeysCache(java.util.List<HotkeyInfo> hotkeysInfo)

clearHotkeysCache

protected static void clearHotkeysCache(javax.swing.JComponent component)

getComponentHotkeysCache

protected static java.util.List<HotkeyInfo> getComponentHotkeysCache(javax.swing.JComponent forComponent)

getComponentHotkeys

public static java.util.List<HotkeyInfo> getComponentHotkeys(javax.swing.JComponent component)

showComponentHotkeys

public static void showComponentHotkeys()
Shows all visible components hotkeys


showComponentHotkeys

public static void showComponentHotkeys(java.awt.Component component)

showComponentHotkeys

protected static void showComponentHotkeys(java.awt.Window window)

installShowAllHotkeysAction

public static void installShowAllHotkeysAction(javax.swing.JComponent topComponent)
Installs "show all hotkeys" action on window or component


installShowAllHotkeysAction

public static void installShowAllHotkeysAction(javax.swing.JComponent topComponent,
                                               HotkeyData hotkeyData)

getComponentHotkeysString

public static java.lang.String getComponentHotkeysString(javax.swing.JComponent component)
All component hotkeys list


disableHotkeys

public static void disableHotkeys()
Global hotkey block


enableHotkeys

public static void enableHotkeys()

isTransferFocus

public static boolean isTransferFocus()
Should transfer focus to fired hotkey component or not


setTransferFocus

public static void setTransferFocus(boolean transferFocus)