 |
Crossfire JXClient, Trunk
|
Go to the documentation of this file.
23 package com.realtime.crossfire.jxclient.gui.keybindings;
28 import java.awt.event.KeyEvent;
29 import java.io.BufferedReader;
30 import java.io.BufferedWriter;
31 import java.io.IOException;
32 import java.nio.charset.StandardCharsets;
33 import java.nio.file.Files;
34 import java.nio.file.NoSuchFileException;
35 import java.nio.file.Path;
36 import java.util.Collection;
37 import java.util.TreeSet;
38 import java.util.function.Predicate;
39 import java.util.stream.Collectors;
40 import org.jetbrains.annotations.NotNull;
41 import org.jetbrains.annotations.Nullable;
59 private final Collection<KeyBinding>
keybindings =
new TreeSet<>();
135 if (keyBinding !=
null) {
159 try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
161 final String line = reader.readLine();
169 System.err.println(
"ignoring invalid key binding ("+ex.getMessage()+
"): "+line);
173 }
catch (
final NoSuchFileException ignored) {
176 }
catch (
final IOException ex) {
179 System.err.println(
"Cannot read keybindings file "+file+
": "+ex.getMessage());
197 }
catch (
final IOException ex) {
198 System.err.println(
"Cannot write keybindings file "+
fileVersion2+
": "+ex.getMessage());
204 try (BufferedWriter bw = Files.newBufferedWriter(
fileVersion2, StandardCharsets.UTF_8)) {
207 if (keyBinding.isDefault()) {
214 final KeyEvent2 keyEvent = keyBinding.getKeyEvent2();
216 if (code == KeyEvent.VK_UNDEFINED) {
218 bw.write(Integer.toString(keyEvent.
getKeyChar()));
231 }
catch (
final IOException ex) {
232 System.err.println(
"Cannot write keybindings file "+
fileVersion2+
": "+ex.getMessage());
246 if (keyBinding.getKeyEvent2().equals(keyEvent)) {
262 if (line.startsWith(
"char ")) {
263 final String[] tmp = line.substring(5).split(
" +", 2);
264 if (tmp.length != 2) {
269 final char keyChar = (char)Integer.parseInt(tmp[0]);
273 }
catch (
final NumberFormatException ex) {
275 keyBindingException.initCause(ex);
276 throw keyBindingException;
278 }
else if (line.startsWith(
"code ")) {
279 final String[] tmp = line.substring(5).split(
" +", 3);
280 if (tmp.length != 3) {
293 keyBindingException.initCause(ex);
294 throw keyBindingException;
296 if (keyCode == KeyEvent.VK_UNDEFINED) {
303 }
catch (
final NumberFormatException ignored) {
322 if (keyBinding.matchesKeyEvent(e)) {
336 keyBinding.getCommands().execute();
345 public Collection<KeyBinding>
getBindings(@NotNull
final Predicate<KeyBinding> predicate) {
346 return keybindings.stream().filter(predicate).collect(Collectors.toSet());
final GUICommandFactory guiCommandFactory
The GUICommandFactory for creating commands.
static int stringToModifiers(@NotNull final String modifiers)
Converts a string representation to modifiers.
int getKeyCode(@NotNull final String keyName)
Returns the key code for a key name.
Represents a pressed or released key.
Indicates that a key code does not exist.
void addKeyBinding(@NotNull final KeyEvent2 keyEvent, @NotNull final CommandList cmdList, final boolean isDefault)
Adds a key binding for a key event.
KeyBinding getKeyBinding(@NotNull final KeyEvent2 keyEvent)
Finds a key binding associated to a key event.
final Path fileVersion1
The fallback file for loading the bindings if fileVersion2 does not exist;.
Manages a set of key bindings.
Abstract base class for key bindings.
static int convertModifiers(final int modifiers)
Converts old-style modifiers into the current format.
char getKeyChar()
Returns the key as a char.
boolean handleKeyPress(@NotNull final KeyEvent2 e)
Executes a "key press" event.
KeyBindings(@Nullable final Path fileVersion2, @Nullable final Path fileVersion1, @NotNull final GUICommandFactory guiCommandFactory)
Creates a new instance.
final Path fileVersion2
The file for loading and saving the bindings;.
void deleteKeyBinding(@NotNull final KeyEvent2 keyEvent)
Removes a key binding for a key event.
static String modifiersToString(final int modifiers)
Converts modifiers to string representation.
void deleteKeyBinding(@Nullable final KeyBinding keyBinding)
Removes a key binding.
Indicates that a key binding is invalid.
int getKeyCode()
Returns the key code.
void parseKeyBinding(@NotNull final String line, final boolean isDefault)
Parses and add a key binding.
void addKeyBinding(@NotNull final KeyBinding keyBinding)
Adds (or replace) a key binding.
GUICommand createCommandDecode(@NotNull String encodedCommandString)
Creates a new GUICommand instance from string representation.
int getModifiers()
Returns the modifiers.
KeyCodeMap keyCodeMap
The key code map to use.
final Collection< KeyBinding > keybindings
The active key bindings.
A list of GUICommand instances.
boolean modified
Whether the contents of keybindings have been modified from the last saved state.
String getKeyName(final int keyCode)
Returns the key name for a key code.
void loadKeyBindings()
Loads the key bindings from the given file.
void add(@NotNull final GUICommand guiCommand)
Adds a command to the end of this command list.
void saveKeyBindings()
Saves the key bindings to the given file.
Factory for creating GUICommand instances from string representation.
String encode(@NotNull final String command)
Encodes a key binding if necessary.
AND
List is executed if all entries can execute.
Collection< KeyBinding > getBindings(@NotNull final Predicate< KeyBinding > predicate)
Returns all key bindings matching a given predicate.
Maps between key codes integer constants and string representations.
static void executeKeyBinding(@NotNull final KeyBinding keyBinding)
Executes a KeyBinding instance.