Gridarta Editor
KeyStrokeField.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.dialog.shortcuts;
21 
22 import java.awt.event.KeyEvent;
23 import java.awt.event.KeyListener;
24 import java.util.Collection;
25 import java.util.concurrent.CopyOnWriteArrayList;
26 import javax.swing.JTextField;
27 import javax.swing.KeyStroke;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
35 public class KeyStrokeField extends JTextField {
36 
40  private static final long serialVersionUID = 1L;
41 
46  @NotNull
47  private final Collection<KeyStrokeFieldListener> listeners = new CopyOnWriteArrayList<>();
48 
52  @Nullable
53  private KeyStroke keyStroke;
54 
59  public KeyStrokeField(@Nullable final KeyStroke keyStroke) {
60  this.keyStroke = keyStroke;
61 
62  setFocusable(true);
63  final KeyListener keyListener = new KeyListener() {
64 
65  @Override
66  public void keyTyped(@NotNull final KeyEvent e) {
67  // ignore
68  }
69 
70  @Override
71  public void keyPressed(@NotNull final KeyEvent e) {
72  switch (e.getKeyCode()) {
73  case KeyEvent.VK_SHIFT:
74  case KeyEvent.VK_CONTROL:
75  case KeyEvent.VK_ALT:
76  case KeyEvent.VK_CAPS_LOCK:
77  case KeyEvent.VK_META:
78  case KeyEvent.VK_ALT_GRAPH:
79  // ignore modifier keys
80  break;
81 
82  default:
83  setKeyStroke(KeyStroke.getKeyStrokeForEvent(e));
84  break;
85  }
86  }
87 
88  @Override
89  public void keyReleased(@NotNull final KeyEvent e) {
90  // ignore
91  }
92 
93  };
94  addKeyListener(keyListener);
95 
96  getInputMap(WHEN_IN_FOCUSED_WINDOW).clear();
97  getInputMap(WHEN_IN_FOCUSED_WINDOW).setParent(null);
98  getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).clear();
99  getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).setParent(null);
100  getInputMap(WHEN_FOCUSED).clear();
101  getInputMap(WHEN_FOCUSED).setParent(null);
102 
103  updateKeyStroke();
104  }
105 
110  public void addKeyStrokeListener(@NotNull final KeyStrokeFieldListener listener) {
111  listeners.add(listener);
112  }
113 
118  @Nullable
119  public KeyStroke getKeyStroke() {
120  return keyStroke;
121  }
122 
127  private void setKeyStroke(@NotNull final KeyStroke keyStroke) {
128  if (this.keyStroke == keyStroke) {
129  return;
130  }
131 
132  this.keyStroke = keyStroke;
133  updateKeyStroke();
134  for (final KeyStrokeFieldListener listener : listeners) {
135  listener.keyStrokeChanged(keyStroke);
136  }
137  }
138 
143  private void updateKeyStroke() {
144  setText(keyStroke == null ? "none" : keyStroke.toString());
145  }
146 
147 }
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeField.getKeyStroke
KeyStroke getKeyStroke()
Returns the currently shown KeyStroke.
Definition: KeyStrokeField.java:119
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeFieldListener
Interface for listeners interested in KeyStrokeField related events.
Definition: KeyStrokeFieldListener.java:30
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeField.setKeyStroke
void setKeyStroke(@NotNull final KeyStroke keyStroke)
Updates the current key stroke.
Definition: KeyStrokeField.java:127
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeField.updateKeyStroke
void updateKeyStroke()
Updates the shown text to reflect the current value of {}.
Definition: KeyStrokeField.java:143
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeField.KeyStrokeField
KeyStrokeField(@Nullable final KeyStroke keyStroke)
Creates a new instance.
Definition: KeyStrokeField.java:59
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeField.listeners
final Collection< KeyStrokeFieldListener > listeners
The KeyStrokeFieldListeners to be notified.
Definition: KeyStrokeField.java:47
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeField.addKeyStrokeListener
void addKeyStrokeListener(@NotNull final KeyStrokeFieldListener listener)
Adds a KeyStrokeFieldListener to be notified about changes.
Definition: KeyStrokeField.java:110
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeField
A javax.swing.JComponent for selecting a KeyStroke.
Definition: KeyStrokeField.java:35
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeField.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: KeyStrokeField.java:40
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeField.keyStroke
KeyStroke keyStroke
The currently shown KeyStroke.
Definition: KeyStrokeField.java:53