Crossfire JXClient, Trunk  R20561
KeybindingsManager.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.keybindings;
23 
27 import java.io.File;
28 import java.io.IOException;
29 import java.util.Collection;
30 import java.util.HashSet;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 
38 public class KeybindingsManager {
39 
43  @NotNull
45 
49  @NotNull
50  private final KeyBindings keyBindings;
51 
56  @Nullable
58 
63  @Nullable
65 
71  public KeybindingsManager(@NotNull final File keybindingsFile, @NotNull final GUICommandFactory guiCommandFactory) {
72  this.guiCommandFactory = guiCommandFactory;
73  keyBindings = new KeyBindings(keybindingsFile, guiCommandFactory);
74  }
75 
82  public boolean removeKeyBinding(final boolean perCharacter) {
83  if (perCharacter && characterKeyBindings == null) {
84  return false;
85  }
86 
87  keyBindingState = new KeyBindingState(characterKeyBindings, perCharacter ? null : keyBindings, null);
88  return true;
89  }
90 
95  public boolean windowClosing() {
96  if (keyBindingState == null) {
97  return false;
98  }
99 
100  keyBindingState = null;
101  return true;
102  }
103 
111  public boolean createKeyBinding(final boolean perCharacter, @NotNull final CommandList cmdList) {
112  final KeyBindings bindings = getKeyBindings(perCharacter);
113  if (bindings == null) {
114  return false;
115  }
116 
117  keyBindingState = new KeyBindingState(bindings, null, cmdList);
118  return true;
119  }
120 
128  @Nullable
129  private KeyBindings getKeyBindings(final boolean perCharacter) {
130  return perCharacter ? characterKeyBindings : keyBindings;
131  }
132 
139  public void loadPerCharacterBindings(@NotNull final CharSequence hostname, @NotNull final CharSequence character) {
140  try {
141  characterKeyBindings = new KeyBindings(Filenames.getKeybindingsFile(hostname, character), guiCommandFactory);
142  } catch (final IOException ex) {
143  characterKeyBindings = null;
144  System.err.println("Cannot read keybindings file for "+character+" on "+hostname+": "+ex.getMessage());
145  return;
146  }
147 
148  try {
149  characterKeyBindings.loadKeyBindings();
150  } catch (final IOException ex) {
151  assert characterKeyBindings != null;
152  System.err.println("Cannot read keybindings file "+characterKeyBindings.getFile()+": "+ex.getMessage());
153  }
154  }
155 
161  if (characterKeyBindings != null) {
162  try {
163  characterKeyBindings.saveKeyBindings();
164  } catch (final IOException ex) {
165  assert characterKeyBindings != null;
166  System.err.println("Cannot write keybindings file "+characterKeyBindings.getFile()+": "+ex.getMessage());
167  }
168  characterKeyBindings = null;
169  }
170  }
171 
175  public void saveKeybindings() {
176  try {
177  keyBindings.saveKeyBindings();
178  } catch (final IOException ex) {
179  System.err.println("Cannot write keybindings file "+keyBindings.getFile()+": "+ex.getMessage());
180  //noinspection UnnecessaryReturnStatement
181  return;
182  }
183  }
184 
188  public void loadKeybindings() {
189  try {
190  keyBindings.loadKeyBindings();
191  } catch (final IOException ex) {
192  System.err.println("Cannot read keybindings file "+keyBindings.getFile()+": "+ex.getMessage());
193  //noinspection UnnecessaryReturnStatement
194  return;
195  }
196  }
197 
202  public boolean keyReleased() {
203  if (keyBindingState == null) {
204  return false;
205  }
206 
207  if (!keyBindingState.keyReleased()) {
208  return false;
209  }
210 
211  keyBindingState = null;
212  return true;
213  }
214 
220  public boolean keyPressed(@NotNull final KeyEvent2 keyEvent) {
221  if (keyBindingState == null) {
222  return false;
223  }
224 
225  keyBindingState.keyPressed(keyEvent);
226  return true;
227  }
228 
234  public boolean escPressed() {
235  if (keyBindingState == null) {
236  return false;
237  }
238 
239  keyBindingState = null;
240  return true;
241  }
242 
248  public boolean handleKeyPress(@NotNull final KeyEvent2 e) {
249  //noinspection SimplifiableIfStatement
250  if (characterKeyBindings != null && characterKeyBindings.handleKeyPress(e)) {
251  return true;
252  }
253 
254  return keyBindings.handleKeyPress(e);
255  }
256 
265  public Iterable<KeyBinding> getBindingsForPartialCommand(@NotNull final String command, final boolean startOnly) {
266  final Collection<KeyBinding> matches = new HashSet<>();
267 
268  /*
269  * character-specific bindings override global ones, so need to check for
270  * duplicates
271  */
272  if (characterKeyBindings != null) {
273  matches.addAll(characterKeyBindings.getBindingsForPartialCommand(command, startOnly));
274  }
275  final Iterable<KeyBinding> global = keyBindings.getBindingsForPartialCommand(command, startOnly);
276  for (final KeyBinding candidate : global) {
277  boolean used = false;
278  for (final KeyBinding check : matches) {
279  if (check.equals(candidate)) {
280  used = true;
281  break;
282  }
283  }
284  if (!used) {
285  matches.add(candidate);
286  }
287  }
288 
289  return matches;
290  }
291 
292 }
KeyBindings characterKeyBindings
The key bindings for the current user.
KeyBindings getKeyBindings(final boolean perCharacter)
Returns the active key bindings.
Iterable< KeyBinding > getBindingsForPartialCommand(@NotNull final String command, final boolean startOnly)
Searches bindings having a command text starting with the specified value.
Utility class to return references to settings files.
Definition: Filenames.java:34
boolean removeKeyBinding(final boolean perCharacter)
Starts to remove a key binding.
void keyPressed(@NotNull final KeyEvent2 keyEvent)
Records a binding by key code.
boolean windowClosing()
Should be called when the main window is closing.
boolean createKeyBinding(final boolean perCharacter, @NotNull final CommandList cmdList)
Starts creating a new key binding.
Represents a pressed or released key.
Definition: KeyEvent2.java:33
boolean handleKeyPress(@NotNull final KeyEvent2 e)
Processes a key pressed event.
KeybindingsManager(@NotNull final File keybindingsFile, @NotNull final GUICommandFactory guiCommandFactory)
Creates a new instance.
void saveKeyBindings()
Saves the key bindings to the given file.
void loadKeybindings()
Loads the key bindings from the backing file.
final GUICommandFactory guiCommandFactory
The GUICommandFactory for creating commands.
static File getKeybindingsFile(@Nullable final CharSequence hostname, @Nullable final CharSequence character)
Returns the keybindings file.
Definition: Filenames.java:122
Manages the state for the key binding dialog.
void saveKeybindings()
Saves the key bindings to the backing file.
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.
KeyBindingState keyBindingState
The current key binding state.
void loadKeyBindings()
Loads the key bindings from the given file.
void unloadPerCharacterBindings()
Unloads (clears and saves) the per-character key bindings.
boolean keyPressed(@NotNull final KeyEvent2 keyEvent)
Processes a key pressed event.
File getFile()
Returns the file for saving the bindings;.
boolean handleKeyPress(@NotNull final KeyEvent2 e)
Executes a "key press" event.
void loadPerCharacterBindings(@NotNull final CharSequence hostname, @NotNull final CharSequence character)
Loads the per-character key bindings.