Package org.geotools.swing.event
Class MapPaneKeyHandler
- Object
-
- KeyAdapter
-
- MapPaneKeyHandler
-
- All Implemented Interfaces:
KeyListener
,EventListener
public class MapPaneKeyHandler extends KeyAdapter
Handles keyboard events for a map pane. This is the default handler for classes derived from AbstractMapPane. It provides for keyboard-controlled scrolling and zooming of the display. The default key bindings for actions should be suitable for most keyboards.While the Java Swing toolkit provides its own mechanism for linking key events to actions, this class is somewhat easier to use and provides a model that could be implemented in other toolkits such as SWT. However, you are free to ignore this class and use your own key handler instead since the map pane classes only require that the handler implements the KeyListener interface.
Key bindings for an individual action can be set like this:
Multiple bindings can be set with the setBindings(Map) or setAllBindings(Map) methods:// Bind left-scroll action to the 'h' key (for Vim fans) KeyInfo key = new KeyInfo(KeyEvent.VK_H, 0); mapPaneKeyHandler.setBinding(key, MapPaneKeyHandler.Action.SCROLL_LEFT);
Map<KeyInfo, MapPaneKeyHandler.Action> bindings = new HashMap<KeyInfo, MapPaneKeyHandler.Action>(); bindings.put(new KeyInfo(KeyEvent.VK_H, 0), MapPaneKeyHandler.Action.SCROLL_LEFT); bindings.put(new KeyInfo(KeyEvent.VK_L, 0), MapPaneKeyHandler.Action.SCROLL_RIGHT); bindings.put(new KeyInfo(KeyEvent.VK_K, 0), MapPaneKeyHandler.Action.SCROLL_UP); bindings.put(new KeyInfo(KeyEvent.VK_J, 0), MapPaneKeyHandler.Action.SCROLL_DOWN); mapPaneKeyHandler.setBindings( bindings );
- Since:
- 8.0
- Author:
- Michael Bedward
- See Also:
KeyInfo
,AbstractMapPane.setKeyHandler(KeyListener)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MapPaneKeyHandler.Action
Constants for supported actions.
-
Constructor Summary
Constructors Constructor Description MapPaneKeyHandler(MapPane mapPane)
Creates a new instance with the default key bindings for actions.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description KeyInfo
getBindingForAction(MapPaneKeyHandler.Action action)
Gets the current key binding for the given action.Map<KeyInfo,MapPaneKeyHandler.Action>
getBindings()
Gets the current key bindings.void
keyPressed(KeyEvent e)
Handles a key-pressed event.void
setAllBindings(Map<KeyInfo,MapPaneKeyHandler.Action> newBindings)
Sets the bindings to those specified innewBindings
.void
setBinding(KeyInfo keyInfo, MapPaneKeyHandler.Action action)
Sets the key binding for a single action.void
setBindings(Map<KeyInfo,MapPaneKeyHandler.Action> newBindings)
Sets one or more key bindings for actions.void
setDefaultBindings()
Sets all key bindings to their default value.-
Methods inherited from class KeyAdapter
keyReleased, keyTyped
-
-
-
-
Constructor Detail
-
MapPaneKeyHandler
public MapPaneKeyHandler(MapPane mapPane)
Creates a new instance with the default key bindings for actions.- Parameters:
mapPane
- the map pane associated with this handler
-
-
Method Detail
-
setDefaultBindings
public void setDefaultBindings()
Sets all key bindings to their default value.
-
getBindings
public Map<KeyInfo,MapPaneKeyHandler.Action> getBindings()
Gets the current key bindings. The bindings are copied into the destinationMap
, so subsequent changes to it will not affect this handler.- Returns:
- the current key bindings
-
getBindingForAction
public KeyInfo getBindingForAction(MapPaneKeyHandler.Action action)
Gets the current key binding for the given action. The object returned is a copy.- Parameters:
action
- the action- Returns:
- the key binding; or
null
if there is no binding - Throws:
IllegalArgumentException
- ifaction
isnull
-
setBinding
public void setBinding(KeyInfo keyInfo, MapPaneKeyHandler.Action action)
Sets the key binding for a single action.- Parameters:
keyInfo
- the key bindingaction
- the action- Throws:
IllegalArgumentException
- if either argument isnull
-
setBindings
public void setBindings(Map<KeyInfo,MapPaneKeyHandler.Action> newBindings)
Sets one or more key bindings for actions. This method can be used to set a subset of the key bindings while leaving others unchanged.- Parameters:
newBindings
- new key bindings- Throws:
IllegalArgumentException
- ifnewBindings
isnull
-
setAllBindings
public void setAllBindings(Map<KeyInfo,MapPaneKeyHandler.Action> newBindings)
Sets the bindings to those specified innewBindings
. This method differs to setBindings(java.util.Map) in that any actions which do not appear in the input map are disabled.- Parameters:
newBindings
- new key bindings- Throws:
IllegalArgumentException
- ifnewBindings
isnull
-
keyPressed
public void keyPressed(KeyEvent e)
Handles a key-pressed event.- Specified by:
keyPressed
in interfaceKeyListener
- Overrides:
keyPressed
in classKeyAdapter
- Parameters:
e
- input key event
-
-