|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
ObjectKeyAdapter
MapPaneKeyHandler
public class MapPaneKeyHandler
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 );
KeyInfo
,
AbstractMapPane#setKeyHandler(java.awt.event.KeyListener)
Nested Class Summary | |
---|---|
static class |
MapPaneKeyHandler.Action
Constants for supported actions. |
Constructor Summary | |
---|---|
MapPaneKeyHandler(MapPane mapPane)
Creates a new instance with the default key bindings for actions. |
Method Summary | |
---|---|
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 in newBindings . |
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 |
Methods inherited from class Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public MapPaneKeyHandler(MapPane mapPane)
mapPane
- the map pane associated with this handlerMethod Detail |
---|
public void setDefaultBindings()
public Map<KeyInfo,MapPaneKeyHandler.Action> getBindings()
Map
, so subsequent changes to it will not affect
this handler.
public KeyInfo getBindingForAction(MapPaneKeyHandler.Action action)
action
- the action
null
if there is no binding
IllegalArgumentException
- if action
is null
public void setBinding(KeyInfo keyInfo, MapPaneKeyHandler.Action action)
keyInfo
- the key bindingaction
- the action
IllegalArgumentException
- if either argument is null
public void setBindings(Map<KeyInfo,MapPaneKeyHandler.Action> newBindings)
newBindings
- new key bindings
IllegalArgumentException
- if newBindings
is null
public void setAllBindings(Map<KeyInfo,MapPaneKeyHandler.Action> newBindings)
newBindings
. This method
differs to setBindings(java.util.Map) in that any actions
which do not appear in the input map are disabled.
newBindings
- new key bindings
IllegalArgumentException
- if newBindings
is null
public void keyPressed(KeyEvent e)
keyPressed
in interface KeyListener
keyPressed
in class KeyAdapter
e
- input key event
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |