22 package com.realtime.crossfire.jxclient.gui.keybindings;
27 import java.io.BufferedWriter;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.io.InputStreamReader;
34 import java.io.LineNumberReader;
35 import java.io.OutputStreamWriter;
36 import java.util.Collection;
37 import java.util.HashSet;
38 import java.util.stream.Collectors;
39 import org.jetbrains.annotations.NotNull;
40 import org.jetbrains.annotations.Nullable;
58 private final Collection<KeyBinding>
keybindings =
new HashSet<>();
124 keybindings.remove(keyBinding);
125 keybindings.add(keyBinding);
129 }
catch (
final IOException ex) {
130 System.err.println(
"Cannot write keybindings file "+file+
": "+ex.getMessage());
147 if (keyBinding != null) {
148 keybindings.remove(keyBinding);
152 }
catch (
final IOException ex) {
153 System.err.println(
"Cannot write keybindings file "+file+
": "+ex.getMessage());
170 try (
final FileInputStream fis =
new FileInputStream(file)) {
171 try (
final InputStreamReader isr =
new InputStreamReader(fis,
"UTF-8")) {
172 try (
final LineNumberReader lnr =
new LineNumberReader(isr)) {
174 final String line = lnr.readLine();
182 System.err.println(
"ignoring invalid key binding ("+ex.getMessage()+
"): "+line);
188 }
catch (
final FileNotFoundException ignored) {
191 }
catch (
final IOException ex) {
205 if (file == null || !modified) {
209 if (keybindings.size() <= 0) {
210 if (!file.delete()) {
211 throw new IOException(
"cannot delete file");
216 try (
final FileOutputStream fos =
new FileOutputStream(file)) {
217 try (
final OutputStreamWriter osw =
new OutputStreamWriter(fos,
"UTF-8")) {
218 try (
final BufferedWriter bw =
new BufferedWriter(osw)) {
219 for (
final KeyBinding keyBinding : keybindings) {
220 if (keyBinding.isDefault()) {
223 if (keyCodeMap == null) {
227 final KeyCodeKeyBinding keyCodeKeyBinding = (KeyCodeKeyBinding)keyBinding;
237 throw new AssertionError(
"Cannot encode "+keyBinding.getClass().getName());
252 for (
final KeyBinding keyBinding : keybindings) {
253 if (keyBinding.matchesKeyCode(keyEvent)) {
268 for (
final KeyBinding keyBinding : keybindings) {
269 if (keyBinding.matchesKeyChar(keyChar)) {
285 if (line.startsWith(
"char ")) {
292 final String[] tmp = line.substring(5).split(
" +", 2);
293 if (tmp.length != 2) {
298 final char keyChar = (char)Integer.parseInt(tmp[0]);
302 }
catch (
final NumberFormatException ex) {
304 keyBindingException.initCause(ex);
305 throw keyBindingException;
307 }
else if (line.startsWith(
"code ")) {
308 final String[] tmp = line.substring(5).split(
" +", 3);
309 if (tmp.length != 3) {
313 if (keyCodeMap == null) {
322 keyBindingException.initCause(ex);
323 throw keyBindingException;
328 modifiers = Integer.parseInt(tmp[1]);
329 }
catch (
final NumberFormatException ex) {
331 keyBindingException.initCause(ex);
332 throw keyBindingException;
350 if (keyBindingCode != null) {
356 if (keyBindingChar != null) {
369 keyBinding.getCommands().execute();
381 return keybindings.stream().filter(binding -> (startOnly && binding.getCommandString().startsWith(command)) || (!startOnly && binding.getCommandString().contains(command))).collect(Collectors.toSet());
GUICommand createCommandDecode(@NotNull String encodedCommandString)
Creates a new GUICommand instance from string representation.
A KeyBinding that matches by key character.
KeyBindings(@Nullable final File file, @NotNull final GUICommandFactory guiCommandFactory)
Creates a new instance.
void parseKeyBinding(@NotNull final String line, final boolean isDefault)
Parses and add a key binding.
KeyEvent2 getKeyEvent2()
Returns the key to match.
final File file
The file for saving the bindings;.
final Collection< KeyBinding > keybindings
The active key bindings.
void addKeyBindingAsKeyCode(@NotNull final KeyEvent2 keyEvent, @NotNull final CommandList cmdList, final boolean isDefault)
Adds a key binding for a key code/modifiers pair.
KeyBinding getKeyBindingAsKeyChar(final char keyChar)
Finds a key binding associated to a key character.
boolean modified
Whether the contents of keybindings have been modified from the last saved state. ...
Represents a pressed or released key.
String getKeyName(final int keyCode)
Returns the key name for a key code.
void add(@NotNull final GUICommand guiCommand)
Adds a command to the end of this command list.
Maps between key codes integer constants and string representations.
void saveKeyBindings()
Saves the key bindings to the given file.
void deleteKeyBinding(@Nullable final KeyBinding keyBinding)
Removes a key binding.
final GUICommandFactory guiCommandFactory
The GUICommandFactory for creating commands.
Indicates that a key binding is invalid.
int getKeyCode()
Returns the key code.
KeyBinding getKeyBindingAsKeyCode(final KeyEvent2 keyEvent)
Finds a key binding associated to a key code/modifiers pair.
static void executeKeyBinding(@NotNull final KeyBinding keyBinding)
Executes a KeyBinding instance.
int getModifiers()
Returns the modifiers.
KeyCodeMap keyCodeMap
The key code map to use.
Factory for creating GUICommand instances from string representation.
Collection< KeyBinding > getBindingsForPartialCommand(@NotNull final String command, final boolean startOnly)
Search bindings having a command text starting with the specified value.
void deleteKeyBindingAsKeyCode(@NotNull final KeyEvent2 keyEvent)
Removes a key binding for a key code/modifiers pair.
void addKeyBindingAsKeyChar(final char keyChar, @NotNull final CommandList cmdList, final boolean isDefault)
Adds a key binding for a key character.
String getCommandString()
Returns the commands as a string.
void loadKeyBindings()
Loads the key bindings from the given file.
Abstract base class for key bindings.
AND
List is executed if all entries can execute.
String encode(@NotNull final String command)
Encodes a key binding if necessary.
int getKeyCode(@NotNull final String keyName)
Returns the key code for a key name.
A list of GUICommand instances.
File getFile()
Returns the file for saving the bindings;.
A KeyBinding that matches by key code/modifiers pair.
Manages a set of key bindings.
boolean handleKeyPress(@NotNull final KeyEvent2 e)
Executes a "key press" event.
void addKeyBinding(@NotNull final KeyBinding keyBinding)
Adds (or replace) a key binding.
Indicates that a key code does not exist.