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-2015 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 javax.swing.JTextField;
25 import javax.swing.KeyStroke;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29 
34 public class KeyStrokeField extends JTextField {
35 
39  private static final long serialVersionUID = 1L;
40 
45  @NotNull
47 
51  @Nullable
52  private KeyStroke keyStroke;
53 
58  public KeyStrokeField(@Nullable final KeyStroke keyStroke) {
59  this.keyStroke = keyStroke;
60 
61  setFocusable(true);
62  final KeyListener keyListener = new KeyListener() {
63 
64  @Override
65  public void keyTyped(@NotNull final KeyEvent e) {
66  // ignore
67  }
68 
69  @Override
70  public void keyPressed(@NotNull final KeyEvent e) {
71  switch (e.getKeyCode()) {
72  case KeyEvent.VK_SHIFT:
73  case KeyEvent.VK_CONTROL:
74  case KeyEvent.VK_ALT:
75  case KeyEvent.VK_CAPS_LOCK:
76  case KeyEvent.VK_META:
77  case KeyEvent.VK_ALT_GRAPH:
78  // ignore modifier keys
79  break;
80 
81  default:
82  setKeyStroke(KeyStroke.getKeyStrokeForEvent(e));
83  break;
84  }
85  }
86 
87  @Override
88  public void keyReleased(@NotNull final KeyEvent e) {
89  // ignore
90  }
91 
92  };
93  addKeyListener(keyListener);
94 
95  getInputMap(WHEN_IN_FOCUSED_WINDOW).clear();
96  getInputMap(WHEN_IN_FOCUSED_WINDOW).setParent(null);
97  getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).clear();
98  getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).setParent(null);
99  getInputMap(WHEN_FOCUSED).clear();
100  getInputMap(WHEN_FOCUSED).setParent(null);
101 
102  updateKeyStroke();
103  }
104 
109  public void addKeyStrokeListener(@NotNull final KeyStrokeFieldListener listener) {
110  listeners.add(listener);
111  }
112 
117  @Nullable
118  public KeyStroke getKeyStroke() {
119  return keyStroke;
120  }
121 
126  @SuppressWarnings("NullableProblems")
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.getListeners()) {
135  listener.keyStrokeChanged(keyStroke);
136  }
137  }
138 
143  private void updateKeyStroke() {
144  setText(keyStroke == null ? "none" : keyStroke.toString());
145  }
146 
147 }
T [] getListeners()
Returns an array of all the listeners.
KeyStrokeField(@Nullable final KeyStroke keyStroke)
Creates a new instance.
Interface for listeners interested in KeyStrokeField related events.
KeyStroke keyStroke
The currently shown KeyStroke.
Base package of all Gridarta classes.
void updateKeyStroke()
Updates the shown text to reflect the current value of keyStroke.
void addKeyStrokeListener(@NotNull final KeyStrokeFieldListener listener)
Adds a KeyStrokeFieldListener to be notified about changes.
void add(@NotNull final T listener)
Adds a listener.
KeyStroke getKeyStroke()
Returns the currently shown KeyStroke.
A javax.swing.JComponent for selecting a KeyStroke.
final EventListenerList2< KeyStrokeFieldListener > listeners
The KeyStrokeFieldListeners to be notified.
Type-safe version of EventListenerList.
void setKeyStroke(@NotNull final KeyStroke keyStroke)
Updates the current key stroke.
static final long serialVersionUID
The serial version UID.