Package org.geotools.swing.event
Class MapPaneKeyHandler
Object
KeyAdapter
MapPaneKeyHandler
- All Implemented Interfaces:
KeyListener
,EventListener
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:
// 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);
Multiple bindings can be set with the setBindings(Map) or setAllBindings(Map) methods:
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Constants for supported actions. -
Constructor Summary
ConstructorsConstructorDescriptionMapPaneKeyHandler
(MapPane mapPane) Creates a new instance with the default key bindings for actions. -
Method Summary
Modifier and TypeMethodDescriptionGets the current key binding for the given action.Gets the current key bindings.void
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
Sets all key bindings to their default value.Methods inherited from class KeyAdapter
keyReleased, keyTyped
-
Constructor Details
-
MapPaneKeyHandler
Creates a new instance with the default key bindings for actions.- Parameters:
mapPane
- the map pane associated with this handler
-
-
Method Details
-
setDefaultBindings
public void setDefaultBindings()Sets all key bindings to their default value. -
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
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
Sets the key binding for a single action.- Parameters:
keyInfo
- the key bindingaction
- the action- Throws:
IllegalArgumentException
- if either argument isnull
-
setBindings
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
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
Handles a key-pressed event.- Specified by:
keyPressed
in interfaceKeyListener
- Overrides:
keyPressed
in classKeyAdapter
- Parameters:
e
- input key event
-