com.alee.managers.hotkey
Class HotkeyManager

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

public final 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
protected static java.util.Map<java.awt.Container,java.util.List<HotkeyCondition>> containerConditions
          Conditions for top components which might.
protected static java.util.Map<java.awt.Component,java.util.List<HotkeyInfo>> hotkeys
          Added hotkeys.
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(java.awt.Container 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(java.awt.Component component)
           
protected static void clearHotkeysCache(java.util.List<HotkeyInfo> hotkeys)
           
protected static java.util.Map<java.awt.Container,java.util.List<HotkeyCondition>> copyContainerConditions()
           
protected static java.util.Map<java.awt.Component,java.util.List<HotkeyInfo>> copyHotkeys()
          Returns a full copy of hotkeys map.
protected static HotkeyRunnable createAction(javax.swing.AbstractButton forComponent)
           
static void disableHotkeys()
          Global hotkey block
static void enableHotkeys()
           
static java.util.Map<java.awt.Component,java.util.List<HotkeyInfo>> getAllHotkeys()
          All added hotkeys.
static java.util.List<HotkeyInfo> getComponentHotkeys(java.awt.Component component)
          Hotkeys retrieval methods
protected static java.util.List<HotkeyInfo> getComponentHotkeysCache(java.awt.Component component)
           
static java.lang.String getComponentHotkeysString(java.awt.Component component)
          All component hotkeys list
protected static java.lang.String getComponentHotkeysString(java.util.List<HotkeyInfo> infoList)
           
static java.util.List<HotkeyCondition> getContainerHotkeyConditions(java.awt.Container container)
           
protected static java.util.List<HotkeyCondition> getContainerHotkeyConditionsCache(java.awt.Container 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(java.awt.Component topComponent)
          Installs "show all hotkeys" action on window or component
static void installShowAllHotkeysAction(java.awt.Component 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 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, java.awt.Component forComponent, HotkeyData hotkeyData, HotkeyRunnable action)
           
static HotkeyInfo registerHotkey(java.awt.Component topComponent, java.awt.Component forComponent, HotkeyData hotkeyData, HotkeyRunnable action, boolean hidden)
           
static HotkeyInfo registerHotkey(java.awt.Component topComponent, java.awt.Component forComponent, HotkeyData hotkeyData, HotkeyRunnable action, boolean hidden, TooltipWay tooltipWay)
           
static HotkeyInfo registerHotkey(java.awt.Component topComponent, java.awt.Component forComponent, HotkeyData hotkeyData, HotkeyRunnable action, TooltipWay tooltipWay)
           
static HotkeyInfo registerHotkey(java.awt.Component forComponent, HotkeyData hotkeyData, HotkeyRunnable action)
           
static HotkeyInfo registerHotkey(java.awt.Component forComponent, HotkeyData hotkeyData, HotkeyRunnable action, boolean hidden)
           
static HotkeyInfo registerHotkey(HotkeyData hotkeyData, HotkeyRunnable action)
          Hotkey register methods
static void removeContainerHotkeyCondition(java.awt.Container container, HotkeyCondition hotkeyCondition)
           
static void removeContainerHotkeyConditions(java.awt.Container container)
           
static void setComponentHotkeyDisplayWay(java.awt.Component 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(java.awt.Component component)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

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<java.awt.Component,java.util.List<HotkeyInfo>> hotkeys
Added hotkeys.


containerConditions

protected static java.util.Map<java.awt.Container,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.


copyHotkeys

protected static java.util.Map<java.awt.Component,java.util.List<HotkeyInfo>> copyHotkeys()
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<java.awt.Container,java.util.List<HotkeyCondition>> copyContainerConditions()

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

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(java.awt.Component forComponent,
                                        HotkeyData hotkeyData,
                                        HotkeyRunnable action)

registerHotkey

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

registerHotkey

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

registerHotkey

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

registerHotkey

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

registerHotkey

public static HotkeyInfo registerHotkey(java.awt.Component topComponent,
                                        java.awt.Component 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(java.awt.Component component,
                                                TooltipWay tooltipWay)
Sets component hotkey tip way


addContainerHotkeyCondition

public static void addContainerHotkeyCondition(java.awt.Container container,
                                               HotkeyCondition hotkeyCondition)
Sets top component additional hotkey trigger condition


removeContainerHotkeyCondition

public static void removeContainerHotkeyCondition(java.awt.Container container,
                                                  HotkeyCondition hotkeyCondition)

removeContainerHotkeyConditions

public static void removeContainerHotkeyConditions(java.awt.Container container)

getContainerHotkeyConditions

public static java.util.List<HotkeyCondition> getContainerHotkeyConditions(java.awt.Container container)

getContainerHotkeyConditionsCache

protected static java.util.List<HotkeyCondition> getContainerHotkeyConditionsCache(java.awt.Container container)

unregisterHotkey

public static void unregisterHotkey(HotkeyInfo hotkeyInfo)
Hotkey removal methods


unregisterHotkeys

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

getComponentHotkeys

public static java.util.List<HotkeyInfo> getComponentHotkeys(java.awt.Component component)
Hotkeys retrieval methods


getAllHotkeys

public static java.util.Map<java.awt.Component,java.util.List<HotkeyInfo>> getAllHotkeys()
All added hotkeys. Make sure you know what you are doing if you want to modify this map from outside, otherwise your actions might change HotkeyManager behavior.


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> hotkeys)

clearHotkeysCache

protected static void clearHotkeysCache(java.awt.Component component)

getComponentHotkeysCache

protected static java.util.List<HotkeyInfo> getComponentHotkeysCache(java.awt.Component 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(java.awt.Component topComponent)
Installs "show all hotkeys" action on window or component


installShowAllHotkeysAction

public static void installShowAllHotkeysAction(java.awt.Component topComponent,
                                               HotkeyData hotkeyData)

getComponentHotkeysString

public static java.lang.String getComponentHotkeysString(java.awt.Component component)
All component hotkeys list


getComponentHotkeysString

protected static java.lang.String getComponentHotkeysString(java.util.List<HotkeyInfo> infoList)

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)