Crossfire JXClient, Trunk  R20561
KeyCodeKeyBinding.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 
25 import java.awt.event.KeyEvent;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28 
33 public class KeyCodeKeyBinding extends KeyBinding {
34 
38  @NotNull
39  private final KeyEvent2 keyEvent;
40 
48  public KeyCodeKeyBinding(@NotNull final KeyEvent2 keyEvent, @NotNull final CommandList commands, final boolean isDefault) {
49  super(commands, isDefault);
50  this.keyEvent = keyEvent;
51  }
52 
57  @NotNull
59  return keyEvent;
60  }
61 
65  @Override
66  public boolean equals(@Nullable final Object obj) {
67  if (!(obj instanceof KeyCodeKeyBinding)) {
68  return false;
69  }
70 
71  final KeyCodeKeyBinding keyBinding = (KeyCodeKeyBinding)obj;
72  return keyBinding.keyEvent.equalsKeyCode(keyEvent);
73  }
74 
78  @Override
79  public int hashCode() {
80  return keyEvent.hashCode();
81  }
82 
86  @Override
87  public boolean matchesKeyCode(@NotNull final KeyEvent2 keyEvent) {
88  return this.keyEvent.equalsKeyCode(keyEvent);
89  }
90 
94  @Override
95  public boolean matchesKeyChar(final char keyChar) {
96  return false;
97  }
98 
102  @NotNull
103  @Override
104  public String getBindingDescription() {
105  final StringBuilder sb = new StringBuilder();
106  final int modifiers = keyEvent.getModifiers();
107  if (modifiers != KeyEvent2.NONE) {
108  //noinspection MagicConstant
109  sb.append(KeyEvent.getKeyModifiersText(modifiers)).append("+");
110  }
111  sb.append(KeyEvent.getKeyText(keyEvent.getKeyCode()));
112  return sb.toString();
113  }
114 
115 }
boolean isDefault()
Returns whether the key binding is a "default" binding which should not be saved. ...
boolean equalsKeyCode(@NotNull final KeyEvent2 keyEvent)
Returns whether this key event matches the same key code as another key event.
Definition: KeyEvent2.java:129
final CommandList commands
The associated CommandList.
Definition: KeyBinding.java:40
Represents a pressed or released key.
Definition: KeyEvent2.java:33
KeyCodeKeyBinding(@NotNull final KeyEvent2 keyEvent, @NotNull final CommandList commands, final boolean isDefault)
Creates a KeyBinding that matches by key code/modifiers pair.
static final int NONE
The mask for "no modifier".
Definition: KeyEvent2.java:38
A KeyBinding that matches by key code/modifiers pair.