source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/DisableShortcutsOnFocusGainedTextField.java

Last change on this file was 18285, checked in by Don-vip, 3 years ago

see #21443 - Extract shortcut disablement to interface, with default implementations for focusGained/focusLost (patch by taylor.smock)

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.event.FocusEvent;
5import java.util.ArrayList;
6import java.util.HashSet;
7import java.util.List;
8import java.util.Set;
9
10import javax.swing.Action;
11import javax.swing.text.Document;
12
13import org.openstreetmap.josm.actions.JosmAction;
14import org.openstreetmap.josm.tools.Pair;
15import org.openstreetmap.josm.tools.Shortcut;
16
17/**
18 * A JTextField that disabled all JOSM shortcuts composed of a single key without modifier (except F1 to F12),
19 * in order to avoid them to be triggered while typing.
20 * This allows to include text fields in toggle dialogs (needed for relation filter).
21 * @since 5696
22 */
23public class DisableShortcutsOnFocusGainedTextField extends JosmTextField implements DisableShortcutsOnFocusGainedComponent {
24
25 /**
26 * Constructs a new <code>TextField</code>. A default model is created,
27 * the initial string is <code>null</code>, and the number of columns is set to 0.
28 */
29 public DisableShortcutsOnFocusGainedTextField() {
30 // Contents can be set with parent methods
31 }
32
33 /**
34 * Constructs a new <code>TextField</code> initialized with the
35 * specified text. A default model is created and the number of columns is 0.
36 *
37 * @param text the text to be displayed, or <code>null</code>
38 */
39 public DisableShortcutsOnFocusGainedTextField(String text) {
40 super(text);
41 }
42
43 /**
44 * Constructs a new empty <code>TextField</code> with the specified number of columns.
45 * A default model is created and the initial string is set to <code>null</code>.
46 *
47 * @param columns the number of columns to use to calculate
48 * the preferred width; if columns is set to zero, the
49 * preferred width will be whatever naturally results from the component implementation
50 */
51 public DisableShortcutsOnFocusGainedTextField(int columns) {
52 super(columns);
53 }
54
55 /**
56 * Constructs a new <code>TextField</code> initialized with the
57 * specified text and columns. A default model is created.
58 *
59 * @param text the text to be displayed, or <code>null</code>
60 * @param columns the number of columns to use to calculate
61 * the preferred width; if columns is set to zero, the
62 * preferred width will be whatever naturally results from the component implementation
63 */
64 public DisableShortcutsOnFocusGainedTextField(String text, int columns) {
65 super(text, columns);
66 }
67
68 /**
69 * Constructs a new <code>JTextField</code> that uses the given text
70 * storage model and the given number of columns.
71 * This is the constructor through which the other constructors feed.
72 * If the document is <code>null</code>, a default model is created.
73 *
74 * @param doc the text storage to use; if this is <code>null</code>,
75 * a default will be provided by calling the
76 * <code>createDefaultModel</code> method
77 * @param text the initial string to display, or <code>null</code>
78 * @param columns the number of columns to use to calculate
79 * the preferred width &gt;= 0; if <code>columns</code>
80 * is set to zero, the preferred width will be whatever
81 * naturally results from the component implementation
82 * @throws IllegalArgumentException if <code>columns</code> &lt; 0
83 */
84 public DisableShortcutsOnFocusGainedTextField(Document doc, String text, int columns) {
85 super(doc, text, columns);
86 }
87
88 private final transient List<Pair<Action, Shortcut>> unregisteredActionShortcuts = new ArrayList<>();
89 private final Set<JosmAction> disabledMenuActions = new HashSet<>();
90
91 @Override
92 public void focusGained(FocusEvent e) {
93 super.focusGained(e);
94 DisableShortcutsOnFocusGainedComponent.super.focusGained(e);
95 }
96
97 @Override
98 public void focusLost(FocusEvent e) {
99 super.focusLost(e);
100 DisableShortcutsOnFocusGainedComponent.super.focusLost(e);
101 }
102
103 @Override
104 public List<Pair<Action, Shortcut>> getUnregisteredActionShortcuts() {
105 return this.unregisteredActionShortcuts;
106 }
107
108 @Override
109 public Set<JosmAction> getDisabledMenuActions() {
110 return this.disabledMenuActions;
111 }
112}
Note: See TracBrowser for help on using the repository browser.