com.realtime.crossfire.jxclient.gui.keybindings
Class KeyBindings

java.lang.Object
  extended by com.realtime.crossfire.jxclient.gui.keybindings.KeyBindings

public class KeyBindings
extends java.lang.Object

Manages a set of key bindings.


Field Summary
private  CommandCallback commandCallback
          The CommandCallback to use.
private  Commands commands
          The commands instance for executing commands.
private  java.io.File file
          The file for saving the bindings; null to not save.
private  java.util.Collection<KeyBinding> keybindings
          The active key bindings.
private  KeyCodeMap keyCodeMap
          The key code map to use.
private  Macros macros
          The Macros instance to use.
private  boolean modified
          Whether the contents of keybindings have been modified from the last saved state.
 
Constructor Summary
KeyBindings(java.io.File file, Commands commands, CommandCallback commandCallback, Macros macros)
          Creates a new instance.
 
Method Summary
private  void addKeyBinding(KeyBinding keyBinding)
          Adds (or replace) a key binding.
 void addKeyBindingAsKeyChar(char keyChar, CommandList cmdList, boolean isDefault)
          Adds a key binding for a key character.
 void addKeyBindingAsKeyCode(int keyCode, int modifiers, CommandList cmdList, boolean isDefault)
          Adds a key binding for a key code/modifiers pair.
private  void deleteKeyBinding(KeyBinding keyBinding)
          Removes a key binding.
 void deleteKeyBindingAsKeyChar(char keyChar)
          Removes a key binding for a key character.
 void deleteKeyBindingAsKeyCode(int keyCode, int modifiers)
          Removes a key binding for a key code/modifiers pair.
private static boolean executeKeyBinding(KeyBinding keyBinding)
          Executes a KeyBinding instance.
 java.io.File getFile()
          Returns the file for saving the bindings; null to not save.
private  KeyBinding getKeyBindingAsKeyChar(char keyChar)
          Finds a key binding associated to a key character.
private  KeyBinding getKeyBindingAsKeyCode(int keyCode, int modifiers)
          Finds a key binding associated to a key code/modifiers pair.
 boolean handleKeyPress(java.awt.event.KeyEvent e)
          Executes a "key press" event.
 boolean handleKeyTyped(java.awt.event.KeyEvent e)
          Executes a "key typed" event.
 void loadKeyBindings()
          Loads the key bindings from the given file.
 void parseKeyBinding(java.lang.String line, boolean isDefault)
          Parses and add a key binding.
 void saveKeyBindings()
          Saves the key bindings to the given file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

commandCallback

@NotNull
private final CommandCallback commandCallback
The CommandCallback to use.


commands

@NotNull
private final Commands commands
The commands instance for executing commands.


file

@Nullable
private final java.io.File file
The file for saving the bindings; null to not save.


keybindings

@NotNull
private final java.util.Collection<KeyBinding> keybindings
The active key bindings.


keyCodeMap

@Nullable
private KeyCodeMap keyCodeMap
The key code map to use. Set to null until first use.


macros

@NotNull
private final Macros macros
The Macros instance to use.


modified

private boolean modified
Whether the contents of keybindings have been modified from the last saved state.

Constructor Detail

KeyBindings

public KeyBindings(@Nullable
                   java.io.File file,
                   @NotNull
                   Commands commands,
                   @NotNull
                   CommandCallback commandCallback,
                   @NotNull
                   Macros macros)
Creates a new instance.

Parameters:
file - the file for saving the bindings; null to not save
commands - the commands instance for executing commands
commandCallback - the command callback to use
macros - the macros instance to use
Method Detail

addKeyBinding

private void addKeyBinding(@NotNull
                           KeyBinding keyBinding)
Adds (or replace) a key binding.

Parameters:
keyBinding - the key binding

addKeyBindingAsKeyChar

public void addKeyBindingAsKeyChar(char keyChar,
                                   @NotNull
                                   CommandList cmdList,
                                   boolean isDefault)
Adds a key binding for a key character.

Parameters:
keyChar - the key character for the key binding
cmdList - the commands to associate to the key binding
isDefault - whether the key binding is a "default" binding which should not be saved

addKeyBindingAsKeyCode

public void addKeyBindingAsKeyCode(int keyCode,
                                   int modifiers,
                                   @NotNull
                                   CommandList cmdList,
                                   boolean isDefault)
Adds a key binding for a key code/modifiers pair.

Parameters:
keyCode - the key code for the key binding
modifiers - the modifiers for the key binding
cmdList - the commands to associate to the key binding
isDefault - whether the key binding is a "default" binding which should not be saved

deleteKeyBinding

private void deleteKeyBinding(@Nullable
                              KeyBinding keyBinding)
Removes a key binding.

Parameters:
keyBinding - the key binding; may be null

deleteKeyBindingAsKeyChar

public void deleteKeyBindingAsKeyChar(char keyChar)
Removes a key binding for a key character.

Parameters:
keyChar - the key character of the key binding

deleteKeyBindingAsKeyCode

public void deleteKeyBindingAsKeyCode(int keyCode,
                                      int modifiers)
Removes a key binding for a key code/modifiers pair.

Parameters:
keyCode - the key code of the key binding
modifiers - the modifiers of the key binding

executeKeyBinding

private static boolean executeKeyBinding(@Nullable
                                         KeyBinding keyBinding)
Executes a KeyBinding instance.

Parameters:
keyBinding - the key binding to execute; may be null
Returns:
whether keyBinding is not null

getFile

@Nullable
public java.io.File getFile()
Returns the file for saving the bindings; null to not save.

Returns:
the file

getKeyBindingAsKeyChar

@Nullable
private KeyBinding getKeyBindingAsKeyChar(char keyChar)
Finds a key binding associated to a key character.

Parameters:
keyChar - the key character to look up
Returns:
the key binding, or null if no key binding is associated

getKeyBindingAsKeyCode

@Nullable
private KeyBinding getKeyBindingAsKeyCode(int keyCode,
                                                   int modifiers)
Finds a key binding associated to a key code/modifiers pair.

Parameters:
keyCode - the key code to look up
modifiers - the modifiers to look up
Returns:
the key binding, or null if no key binding is associated

handleKeyPress

public boolean handleKeyPress(@NotNull
                              java.awt.event.KeyEvent e)
Executes a "key press" event.

Parameters:
e - the event to execute
Returns:
whether a matching key binding was found

handleKeyTyped

public boolean handleKeyTyped(@NotNull
                              java.awt.event.KeyEvent e)
Executes a "key typed" event.

Parameters:
e - the event to execute
Returns:
whether a matching key binding was found

loadKeyBindings

public void loadKeyBindings()
                     throws java.io.IOException
Loads the key bindings from the given file.

Throws:
java.io.IOException - if the file cannot be read

parseKeyBinding

public void parseKeyBinding(@NotNull
                            java.lang.String line,
                            boolean isDefault)
                     throws InvalidKeyBindingException
Parses and add a key binding.

Parameters:
line - the key binding to parse
isDefault - whether the key binding is a "default" binding which should not be saved
Throws:
InvalidKeyBindingException - if the key binding is invalid

saveKeyBindings

public void saveKeyBindings()
                     throws java.io.IOException
Saves the key bindings to the given file.

Throws:
java.io.IOException - if the file cannot be written